好久以前就让做个画板了,一直没好好做,这几天学了文件的保存之后,突然就想把画板做一下。
画板,无非就是画线画圆画各种图形。当然,我的画板很简陋,只能画线画圆画矩形。
我在写这个画板的时候遇到了几个比较重大的问题
1、监听器之间如果有互相影响的参数,怎么传?
我发现我解决不了这个问题。后来,我想到强哥当时讲文件的时候,曾经写过一个既能画圆又能画直线的代码。他直接把监听器写在类里面(没有新建一个类),我就试了试这种方法,发现就不用传这个令人头疼的参数了。
但是我还是不知道,如果分开写成两个类要怎么解决这个问题。
2、怎么给按钮加监听器
按钮我是将图片放在数组里,循环添加的。然后我把监听器也写在了循环里。问题当然不是监听器怎么加,而是电脑怎么知道我按的是哪个按钮。。。
开始的时候我以为。。e.getActionCommand.equals(图片名称)就行了。但是按了半天的按钮都没反应。(太对不起熊哥了。。这个熊哥好像讲过)。后来我输出e.getActionCommand发现什么东西也没有、、、然后、、给每个按钮set了一个ActionCommand就好了。。
3、保存
其实保存的时候没有太大的问题。。写着写着。。他就能保存了~~~好开心
对了。我本来想在打开和保存的监听器里写一个这样的东西。
就是一点打开,就能再弹出一个窗体,然后我们可以输入打开的路径,然后用getText()获取里面的字符串,将字符串传给FileInputStream 的对象。后来发现他会报错。。估计是程序是顺序执行的原因。。我木有深究、、就放弃了这个想法、、、
4、代码很乱
暂时还没解决、、而且好多地方没注释
5 、 补充:刚在截图的时候突然发现还没有实现退出这个功能、、、
下面是代码、、有点多。貌似有500多行的样子、
package Paint; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; //画板类 public class ppanel extends JPanel{ //窗体 JFrame jf = new JFrame(); //画布 Graphics g; //工具标签 初始工具 直线 int D1=1; //颜色 初始颜色黑色 Color color=Color.BLACK; //坐标 int x1,x2,y1,y2; //存储图形的队列 List<Shape> lineList = new ArrayList<Shape>(); //监听器 ActionListener ac = new ActionListener() { public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals("打开")){ // JFrame open = new JFrame("打开"); // open.setLayout(new FlowLayout()); // JLabel jl1 = new JLabel("请输入地址:"); // open.add(jl1); // open.setSize(200, 150); // open.setLocationRelativeTo(null); // // JTextField j1 = new JTextField(10); // j1.setText("G:/mypic.dat"); // open.add(j1,BorderLayout.CENTER); // open.setVisible(true); // // String st = jl1.getText(); try { FileInputStream fis = new FileInputStream("G:/mypic.dat"); DataInputStream dis = new DataInputStream(fis); int c = dis.readInt(); for(int i =0;i<c;i++){ int d = dis.readInt(); if(d==1){ int xx1=dis.readInt(); int xx2=dis.readInt(); int yy1=dis.readInt(); int yy2=dis.readInt(); int co = dis.readInt(); g.setColor(new Color(co)); g.drawLine(xx1, yy1, xx2, yy2); }else if(d==2){ int xx1=dis.readInt(); int xx2=dis.readInt(); int yy1=dis.readInt(); int yy2=dis.readInt(); int co = dis.readInt(); g.setColor(new Color(co)); g.drawRect(Math.min(xx1, xx2), Math.min(yy1, yy2), Math.abs(xx2-xx1), Math.abs(yy1-yy2)); }else if(d==3){ int xx1=dis.readInt(); int xx2=dis.readInt(); int yy1=dis.readInt(); int yy2=dis.readInt(); int co = dis.readInt(); g.setColor(new Color(co)); g.fillRect(Math.min(xx1, xx2), Math.min(yy1, yy2), Math.abs(xx2-xx1), Math.abs(yy1-yy2)); }else if(d==4){ int xx1=dis.readInt(); int xx2=dis.readInt(); int yy1=dis.readInt(); int yy2=dis.readInt(); int co = dis.readInt(); g.setColor(new Color(co)); g.drawOval(Math.min(xx1, xx2), Math.min(yy1, yy2), Math.abs(xx2-xx1), Math.abs(yy1-yy2));} else if(d==5){ int xx1=dis.readInt(); int xx2=dis.readInt(); int yy1=dis.readInt(); int yy2=dis.readInt(); int co = dis.readInt(); g.setColor(new Color(co)); g.fillOval(Math.min(xx1, xx2), Math.min(yy1, yy2), Math.abs(xx2-xx1), Math.abs(yy1-yy2));} else if(d==6){ int xx1=dis.readInt(); int xx2=dis.readInt(); int yy1=dis.readInt(); int yy2=dis.readInt(); int co = dis.readInt(); g.setColor(new Color(co)); g.drawOval(Math.min(xx1, xx2), Math.min(yy1, yy2), Math.abs(xx2-xx1), Math.abs(xx2-xx1)); }else if(d==7){ int xx1=dis.readInt(); int xx2=dis.readInt(); int yy1=dis.readInt(); int yy2=dis.readInt(); int co = dis.readInt(); g.setColor(new Color(co)); g.fillOval(Math.min(xx1, xx2), Math.min(yy1, yy2), Math.abs(xx2-xx1), Math.abs(xx2-xx1)); }else if(d==8){ int xx1=dis.readInt(); int xx2=dis.readInt(); int yy1=dis.readInt(); int yy2=dis.readInt(); int co = dis.readInt(); g.setColor(new Color(co)); g.fillRect(xx1, yy1, 10, 10); } } fis.close(); dis.close(); } catch (Exception e2) { e2.printStackTrace(); } System.out.println("打开!"); } if(e.getActionCommand().equals("保存")){ try { FileOutputStream fos = new FileOutputStream("G:/mypic.dat"); DataOutputStream dos = new DataOutputStream(fos); dos.writeInt(lineList.size()); for(int i =0;i<lineList.size();i++){ Shape s = lineList.get(i); dos.writeInt(s.type); dos.writeInt(s.x1); dos.writeInt(s.x2); dos.writeInt(s.y1); dos.writeInt(s.y2); dos.writeInt(s.color.getRGB()); } fos.close(); dos.close(); } catch (Exception e2) { e2.printStackTrace(); } System.out.println("保存!"); } if(e.getActionCommand().equals("退出")){ System.out.println("退出!"); } if(e.getActionCommand().equals("画笔")){ D1 = 0; System.out.println("画笔!"); } if(e.getActionCommand().equals("直线")){ D1 = 1; System.out.println("直线!"); } if(e.getActionCommand().equals("矩形")){ D1 = 2; System.out.println("矩形!"); } if(e.getActionCommand().equals("矩形(实)")){ D1 = 3; System.out.println("矩形(实)!"); } if(e.getActionCommand().equals("椭圆")){ D1 = 4; System.out.println("椭圆!"); } if(e.getActionCommand().equals("椭圆(实)")){ D1 = 5; System.out.println("椭圆(实)!"); } if(e.getActionCommand().equals("圆")){ D1 = 6; System.out.println("圆!"); } if(e.getActionCommand().equals("圆(实)")){ D1 = 7; System.out.println("圆(实)!"); } if(e.getActionCommand().equals("橡皮")){ D1 = 8; System.out.println("橡皮!"); } if(e.getActionCommand().equals("添加文字")){ D1 = 9; System.out.println("添加文字!"); } if(e.getActionCommand().equals("关于")){ JOptionPane.showMessageDialog(null," 作者:周一帆 \nQQ:121727544"); } }}; public static void main(String[] agrs){ ppanel f = new ppanel(); f.draw(); } public void draw(){ //设置属性 jf.setTitle("画图板"); jf.setSize(700, 600); jf.setLocationRelativeTo(null); jf.setDefaultCloseOperation(3); //菜单 menu(); //工具 tool(); //画板 jf.add(this,BorderLayout.CENTER); //设置可见 jf.setVisible(true); this.g = this.getGraphics(); //监听器 this.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { x2=e.getX(); y2=e.getY(); System.out.println(D1); if(D1==1){ g.setColor(color); g.drawLine(x1, y1, x2, y2); Shape s = new Shape(); s.x1=x1;s.x2=x2;s.y1=y1;s.y2=y2; s.color=color; s.type=1; lineList.add(s); } if(D1==2){ g.setColor(color); g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y1-y2)); Shape s = new Shape(); s.x1=x1;s.x2=x2;s.y1=y1;s.y2=y2; s.color=color; s.type=2; lineList.add(s); } if(D1==3){ g.setColor(color); g.fillRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y1-y2)); Shape s = new Shape(); s.x1=x1;s.x2=x2;s.y1=y1;s.y2=y2; s.color=color; s.type=3; lineList.add(s); } if(D1==4){ g.setColor(color); g.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y1-y2)); Shape s = new Shape(); s.x1=x1;s.x2=x2;s.y1=y1;s.y2=y2; s.color=color; s.type=4; lineList.add(s); } if(D1==5){ g.setColor(color); g.fillOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y1-y2)); Shape s = new Shape(); s.x1=x1;s.x2=x2;s.y1=y1;s.y2=y2; s.color=color; s.type=5; lineList.add(s); } if(D1==6){ g.setColor(color); g.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(x2-x1)); Shape s = new Shape(); s.x1=x1;s.x2=x2;s.y1=y1;s.y2=y2; s.color=color; s.type=6; lineList.add(s); } if(D1==7){ g.setColor(color); g.fillOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(x2-x1)); Shape s = new Shape(); s.x1=x1;s.x2=x2;s.y1=y1;s.y2=y2; s.color=color; s.type=7; lineList.add(s);} if(D1==8){ g.setColor(Color.WHITE); g.fillRect(x1, y1, 10, 10); Shape s = new Shape(); s.x1=x1;s.x2=x2;s.y1=y1;s.y2=y2; s.color=color; s.type=8; lineList.add(s); } if(D1==9){ System.out.print("添加文字!"); } } @Override public void mousePressed(MouseEvent e) { x1=e.getX(); y1=e.getY(); } @Override public void mouseExited(MouseEvent e) {} @Override public void mouseEntered(MouseEvent e) {} @Override public void mouseClicked(MouseEvent e) {} }); } //画布重绘 public void paint(Graphics g){ super.paint(g); this.setBackground(Color.WHITE); } //设置菜单栏 public void menu(){ //菜单 JMenuBar jmb = new JMenuBar(); //菜单项 JMenu jm1 = new JMenu("文件"); JMenu jm2 = new JMenu("工具"); JMenu jm3 = new JMenu("帮助"); //菜单子项 JMenuItem jmi11 = new JMenuItem("打开"); JMenuItem jmi12 = new JMenuItem("保存"); JMenuItem jmi13 = new JMenuItem("退出"); JMenuItem jmi20 = new JMenuItem("画笔"); JMenuItem jmi21 = new JMenuItem("直线"); JMenuItem jmi22 = new JMenuItem("矩形"); JMenuItem jmi23 = new JMenuItem("矩形(实)"); JMenuItem jmi24 = new JMenuItem("椭圆"); JMenuItem jmi25 = new JMenuItem("椭圆(实)"); JMenuItem jmi26 = new JMenuItem("圆"); JMenuItem jmi27 = new JMenuItem("圆(实)"); JMenuItem jmi28 = new JMenuItem("橡皮"); JMenuItem jmi29 = new JMenuItem("添加文字"); JMenuItem jmi31 = new JMenuItem("关于"); jm1.add(jmi11);jm1.add(jmi12);jm1.add(jmi13); jm2.add(jmi20);jm2.add(jmi21);jm2.add(jmi22);jm2.add(jmi23);jm2.add(jmi24); jm2.add(jmi25);jm2.add(jmi26);jm2.add(jmi27);jm2.add(jmi28);jm2.add(jmi29); jm3.add(jmi31); jmb.add(jm1); jmb.add(jm2); jmb.add(jm3); jf.setJMenuBar(jmb); jmi11.addActionListener(ac); jmi12.addActionListener(ac); jmi13.addActionListener(ac); jmi20.addActionListener(ac); jmi21.addActionListener(ac); jmi22.addActionListener(ac); jmi23.addActionListener(ac); jmi24.addActionListener(ac); jmi25.addActionListener(ac); jmi26.addActionListener(ac); jmi27.addActionListener(ac); jmi28.addActionListener(ac); jmi31.addActionListener(ac); } //工具 public void tool(){ JPanel jp = new JPanel(); jp.setLayout(new FlowLayout(FlowLayout.CENTER,20,10)); JLabel jl = new JLabel("工 具"); jl.setFont(new Font("楷体",Font.BOLD,25)); jp.add(jl); String[] array ={"画图/画笔.jpg","画图/直线.jpg","画图/矩形.jpg","画图/矩形1.jpg","画图/椭圆.jpg","画图/椭圆1.jpg", "画图/圆.jpg","画图/圆1.jpg","画图/橡皮.jpg","画图/文字.jpg"}; for(int i =0;i<array.length;i++){ ImageIcon ima = new ImageIcon(array[i]); JButton jb = new JButton(ima); jb.setPreferredSize(new Dimension(24,24)); jp.add(jb); if(i==0){jb.setActionCommand("画笔");} if(i==1){jb.setActionCommand("直线");} if(i==2){jb.setActionCommand("矩形");} if(i==3){jb.setActionCommand("矩形1");} if(i==4){jb.setActionCommand("椭圆");} if(i==5){jb.setActionCommand("椭圆1");} if(i==6){jb.setActionCommand("圆");} if(i==7){jb.setActionCommand("圆1");} if(i==8){jb.setActionCommand("橡皮");} if(i==9){jb.setActionCommand("文字");} //监听器 jb.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals("画笔")){ D1 = 0; System.out.println("画笔!"); } if(e.getActionCommand().equals("直线")){ D1 = 1; System.out.println("直线!"); } if(e.getActionCommand().equals("矩形")){ D1 = 2; System.out.println("矩形!"); } if(e.getActionCommand().equals("矩形1")){ D1 = 3; System.out.println("矩形1!"); } if(e.getActionCommand().equals("椭圆")){ D1 = 4; System.out.println("椭圆!"); } if(e.getActionCommand().equals("椭圆1")){ D1 = 5; System.out.println("椭圆1!"); } if(e.getActionCommand().equals("圆")){ D1 = 6; System.out.println("圆!"); } if(e.getActionCommand().equals("圆1")){ D1 = 7; System.out.println("圆1!"); } if(e.getActionCommand().equals("橡皮")){ D1 = 8; System.out.println("橡皮!"); } if(e.getActionCommand().equals("文字")){ D1 = 9; System.out.println("文字!"); } } }); } JLabel jl1 = new JLabel("颜 色"); jl1.setFont(new Font("楷体",Font.BOLD,25)); jp.add(jl1); JPanel jp1 = new JPanel(); jp1.setPreferredSize(new Dimension(40,100)); String[] array1 ={"颜色/black.jpg","颜色/white.jpg","颜色/blue.jpg","颜色/red.jpg", "颜色/green.jpg","颜色/yellow.jpg","颜色/b.jpg","颜色/p.jpg",}; for(int i = 0;i<8;i++){ ImageIcon ima = new ImageIcon(array1[i]); JButton jb = new JButton(ima); jb.setPreferredSize(new Dimension(12,12)); jp1.add(jb); if (i==0) {jb.setActionCommand("黑色");} if (i==1) {jb.setActionCommand("白色");} if (i==2) {jb.setActionCommand("蓝色");} if (i==3) {jb.setActionCommand("红色");} if (i==4) {jb.setActionCommand("绿色");} if (i==5) {jb.setActionCommand("黄色");} if (i==6) {jb.setActionCommand("灰色");} if (i==7) {jb.setActionCommand("粉色");} jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals("黑色")){color=Color.BLACK;} if(e.getActionCommand().equals("白色")){color=Color.WHITE;} if(e.getActionCommand().equals("蓝色")){color=Color.BLUE;} if(e.getActionCommand().equals("红色")){color=Color.RED;} if(e.getActionCommand().equals("绿色")){color=Color.GREEN;} if(e.getActionCommand().equals("黄色")){color=Color.YELLOW;} if(e.getActionCommand().equals("灰色")){color=Color.GRAY;} if(e.getActionCommand().equals("粉色")){color=Color.PINK;} } }); } jp.add(jp1); jp.setPreferredSize(new Dimension(100,0)); jf.add(jp,BorderLayout.EAST); } }
主界面:
工具栏:
文件栏
其实帮助里面、、还有一个关于作者、、
相关推荐
python学习资源
jfinal-undertow 用于开发、部署由 jfinal 开发的 web 项目
基于Andorid的音乐播放器项目设计(国外开源)实现源码,主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。
python学习资源
python学习资源
python学习一些项目和资源
【毕业设计】java-springboot+vue家具销售平台实现源码(完整前后端+mysql+说明文档+LunW).zip
HTML+CSS+JavaScarip开发的前端网页源代码
python学习资源
【毕业设计】java-springboot-vue健身房信息管理系统源码(完整前后端+mysql+说明文档+LunW).zip
成绩管理系统C/Go。大学生期末小作业,指针实现,C语言版本(ANSI C)和Go语言版本
1_基于大数据的智能菜品个性化推荐与点餐系统的设计与实现.docx
【毕业设计】java-springboot-vue交流互动平台实现源码(完整前后端+mysql+说明文档+LunW).zip
内容概要:本文主要探讨了在高并发情况下如何设计并优化火车票秒杀系统,确保系统的高性能与稳定性。通过对比分析三种库存管理模式(下单减库存、支付减库存、预扣库存),强调了预扣库存结合本地缓存及远程Redis统一库存的优势,同时介绍了如何利用Nginx的加权轮询策略、MQ消息队列异步处理等方式降低系统压力,保障交易完整性和数据一致性,防止超卖现象。 适用人群:具有一定互联网应用开发经验的研发人员和技术管理人员。 使用场景及目标:适用于电商、票务等行业需要处理大量瞬时并发请求的业务场景。其目标在于通过合理的架构规划,实现在高峰期保持平台的稳定运行,保证用户体验的同时最大化销售额。 其他说明:文中提及的技术细节如Epoll I/O多路复用模型以及分布式系统中的容错措施等内容,对于深入理解大规模并发系统的构建有着重要指导意义。
基于 OpenCV 和 PyTorch 的深度车牌识别
【毕业设计-java】springboot-vue教学资料管理系统实现源码(完整前后端+mysql+说明文档+LunW).zip
此数据集包含有关出租车行程的详细信息,包括乘客人数、行程距离、付款类型、车费金额和行程时长。它可用于各种数据分析和机器学习应用程序,例如票价预测和乘车模式分析。
把代码放到Word中,通过开发工具——Visual Basic——插入模块,粘贴在里在,把在硅基流动中申请的API放到VBA代码中。在Word中,选择一个问题,运行这个DeepSeekV3的宏就可以实现在线问答
【毕业设计】java-springboot+vue机动车号牌管理系统实现源码(完整前后端+mysql+说明文档+LunW).zip
【毕业设计】java-springboot-vue交通管理在线服务系统的开发源码(完整前后端+mysql+说明文档+LunW).zip