首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

十分简单的画板

2013-08-04 
非常简单的画板????????????? 好久以前就让做个画板了,一直没好好做,这几天学了文件的保存之后,突然就想把

非常简单的画板

?
???????????? 好久以前就让做个画板了,一直没好好做,这几天学了文件的保存之后,突然就想把画板做一下。
?
???????????? 画板,无非就是画线画圆画各种图形。当然,我的画板很简陋,只能画线画圆画矩形。

?

?我在写这个画板的时候遇到了几个比较重大的问题

?

?????????????? 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() {@Overridepublic 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("添加文字!");}}@Overridepublic void mousePressed(MouseEvent e) {x1=e.getX();y1=e.getY();}@Overridepublic void mouseExited(MouseEvent e) {}@Overridepublic void mouseEntered(MouseEvent e) {}@Overridepublic 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() {@Overridepublic 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);}}

?

主界面:

十分简单的画板

?

?

?

工具栏:

?

?

?

十分简单的画板

?

文件栏

十分简单的画板

?

?

其实帮助里面、、还有一个关于作者、、十分简单的画板

热点排行