JAVA 画图板实现(基本画图功能+界面UI)二、功能实现及重绘实现 – iTryagain

上个引见了使联系的达到预期的目的办法。,在如此视频博客中,我们家将解说每个使系牢之物的达到预期的目的和引见

率先,我们家必须做的事添加事变监督机制,因而成绩来了。,事变源情人是谁?必要添加关系代词办法?E是什么

1、我们家必要点击使系牢之物,获取说起butto的倒转术传达,色传达,粗略而特点的传达。同样,此刻,事变源情人是使系牢之物,监听办法执意addActionListener(ActionListener e),事变使联系是actionListene

2、本着您在for上按下和清偿的座位绘制数字,在这种情况下,事变源情人是窗体,监听办法执意addMouseListener(MouseListener e),事变使联系是mouseListene

 1publicclass DrawListener extends MouseAdapter implements ActionListener {
 2 3private Color color = Color.black;// 国家的色属性,仓库用户选择的色 4privateint width = 1;// 国家的厚属性,特点仓库用户选择 5private String type = "Line";// 国家的图形属性,仓库用户选择的图形 6privateint x1, y1, x2, y2, x3 = 0, y3 = 0, x4 = 0, y4 = 0, x5, y5;// 国家的带有同等性质的值属性,仓库鼠标按下和清偿的带有同等性质的值 7private Graphics2D g;// 国家的画笔类属性,绘制集会。,现时要在集会上绘制图形,从集会检索图形 8private DrawMain dm;// 国家的绘数字单集会属性 9private JTextField text;// 获取倒转术框愿意的10privatedouble H = 100;// 三宫之一组等腰绝顶11privateint num = 0;
12private List list;
13private ImageIcon i = new ImageIcon("C:\\Users\\long452a\\Desktop\\");
1415/**16     * 认可办法
17     * 
18     * @param dm drawing progra的估计集会情人
19*/20public DrawListener(DrawMain dm, JTextField text, List 列表) {
21this.dm = dm;
22this.text = text;
23this.list = list;
24    }
2526/**27     * 单击Butto时的事变处置办法
28     * 
29     * @param 事变源情人传达和手感传达仓库在
30*/31publicvoid actionPerformed(ActionEvent e) {
32    }
3334/**35     * Invoked when the mouse button has been clicked (按下 and 解除) on a
36     * 集会。
37*/38publicvoid mouseClicked(MouseEvent e) {
39    }
4041/**42     * Invoked when a mouse button has been pressed on a 集会。
43*/44publicvoid mousePressed(MouseEvent e) {
45    }
4647/**48     * Invoked when a mouse button has been released on a 集会。
49*/50publicvoid mouseReleased(MouseEvent e) {
51    }
5253publicvoid mouseDragged(MouseEvent e) {
54    }
55 }

因而我们家班是建的。,这是怎样写的

  1/**  2     * 单击Butto时的事变处置办法
  3     * 
  4     * @param 事变源情人传达和手感传达仓库在
  5*/  6publicvoid actionPerformed(ActionEvent e) {
  7         String text = ();
  8if (("")) {
  9             JButton button = (JButton) ();
 10             color = ();
 11         } elseif (("1") || ("3") || ("5")) {
 12             width = (倒转术)
 13         } else {
 14             type = text;
 15        }
 16// (色 + ">>" + width + ">>" + 典型) 17    }
 18 19/** 20     * Invoked when the mouse button has been clicked (按下 and 解除) on a
 21     * 集会。
 22*/ 23publicvoid mouseClicked(MouseEvent e) {
 24         x4 = x2;
 25         y4 = y2;
 26    }
 27 28/** 29     * Invoked when a mouse button has been pressed on a 集会。
 30*/ 31publicvoid mousePressed(MouseEvent e) {
 32         x1 = () + 7;
 33         y1 = () + 183;
 34if (y1 < 183)
 35             y1 = 183;
 36         g = (Graphics2D) ();// 从窗体获取画笔情人 37         (RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);// 设置画笔抗锯齿状突起 38         g.setColor(色);// 设置画笔色 39         g.setStroke(new BasicStroke(width));// 设置刷线厚度 40 41    }
 42 43/** 44     * Invoked when a mouse button has been released on a 集会。
 45*/ 46publicvoid mouseReleased(MouseEvent e) {
 47         x2 = () + 7;
 48         y2 = () + 183;
 49if (y2 < 183)
 50             y2 = 183;
 51if (("iso_Tri")) {
 52if (1) == x2页) {
 53                g.drawLine(1), y1, x2, y2);
 54                 g.drawLine(1), y1, x1 + (int) H, (y2 + y1) / 2);
 55                 g.drawLine(2×2), y2, x1 + (int) H, (y2 + y1) / 2);
 56                 list.add(new Shape(1), y1, x2, y2, width, color, type, i, dm, 倒转术)
 57                 list.add(new Shape(1), y1, x1 + (int) H, (y2 + y1) / 2, width, color, type, i, dm, 倒转术)
 58                 list.add(new 估计(x2, y2, x1 + (int) H, (y2 + y1) / 2, width, color, type, i, dm, 倒转术)
 59             } elseif (y1 == y2) {
 60                g.drawLine(1), y1, x2, y2);
 61                 g.drawLine(1), y1, (1) + x2页) / 2, y1 + (int) H);
 62                 g.drawLine(2×2), y2, (1) + x2页) / 2, y1 + (int) H);
 63                 list.add(new Shape(1), y1, x2, y2, width, color, type, i, dm, 倒转术)
 64                 list.add(new Shape(1), y1, x1 + (1) + x2页) / 2, y1 + (int) H, width, color, type, i, dm, 倒转术)
 65                 list.add(new 估计(x2, y2, x1 + (1) + x2页) / 2, y1 + (int) H, width, color, type, i, dm, 倒转术)
 66             } else {
 67double a = ((double) (2×2) - x1) / (double) (y2 - y1));
 68double x3 = (double) (1) + x2页) / 2 + H * (一)
 69double y3 = (double) (y1 + y2) / 2 - H * (一)
 70                g.drawLine(1), y1, x2, y2);
 71                 g.drawLine(1), y1, (int) x3, (int) y3);
 72                 g.drawLine(2×2), y2, (int) x3, (int) y3);
 73                 list.add(new Shape(1), y1, x2, y2, width, color, type, i, dm, 倒转术)
 74                 list.add(new Shape(1), y1, x1 + (int) x3, (int) y3, width, color, type, i, dm, 倒转术)
 75                 list.add(new 估计(x2, y2, (int) x3, (int) y3, width, color, type, i, dm, 倒转术)
 76            }
 77         } elseif ((多角形)) {
 78if (数字 == 0) {
 79                g.drawLine(1), y1, x2, y2);
 80                 list.add(new Shape(1), y1, x2, y2, width, color, type, i, dm, 倒转术)
 81                 x5 = x2;
 82                 y5 = y2;
 83            }
 84             num++;
 85if (数字 == 1) {
 86                 x3 = x1;
 87                 y3 = y1;
 88            }
 89if (2×2) == x4 && y2 == y4) {
 90                g.drawLine(1), y1, x3, y3);
 91                 list.add(new Shape(1), y1, x3, y3, width, color, type, i, dm, 倒转术)
 92                 num = 0;
 93             } else {
 94                g.drawLine(2×2), y2, x5, y5);
 95                 list.add(new 估计(x2, y2, x5, y5, width, color, type, i, dm, 倒转术)
 96                 x5 = x2;
 97                 y5 = y2;
 98            }
 99         } else {
100             Shape s = new Shape(1), y1, x2, y2, width, color, type, i, dm, 倒转术)
101            s.draw(g);
102            list.add(s);
103        }
104    }
105106publicvoid mouseDragged(MouseEvent e) {
107if ((彩色铅笔)) {
108             x2 = () + 7;//嗨的+7 +183 它叫出狱了。,这幅画可以用鼠标折叠狱
109             y2 = () + 183;
110if (y2 < 183)
111                 y2 = 183;
112             Shape s = new Shape(1), y1, x2, y2, width, color, type, i, dm, 倒转术)
113            s.draw(g);
114            list.add(s);
115             x1 = x2;
116             y1 = y2;
117         } elseif ((删去)) {
118             x2 = () + 7;
119             y2 = () + 183;
120if (y2 < 183)
121                 y2 = 183;
122             Shape s = new Shape(1), y1, x2, y2, width, Color.WHITE, type, i, dm, 倒转术)
123            s.draw(g);
124            list.add(s);
125             x1 = x2;
126             y1 = y2;
127         } elseif ((喷枪)) // 财政困难128        {
129             Random rand = new Random();// 范例化随机数位clas的情人130int size = (50);// 随机决定DRA的点数131             x2 = () + 7;
132             y2 = () + 183;
133for (int j = 0; j < size; j++) {
134// 0到7暗中50次135int x = (10);
136int y = (10);
137// 使多样化(x1),y1的带有同等性质的值),达到预期的目的在(1),y1)环绕点138                 Shape s = new 估计(x2 + x, y2 + y, x2 + x, y2 + y, width, color, type, i, dm, 倒转术)
139                s.draw(g);
140                list.add(s);
141                 x1 = x2;
142                 y1 = y2;
143            }
144        }
145     }

  详细看一眼鳕,你可能性早已留意到了。,我在绘制时到达了每一估计情人。,它还将估计情人保在列表中。,为什么要同样做?你们可以去其他的的视频博客上贴每一粗略达到预期的目的的画图板编码,画几条垂线,于是使转动窗体的一定尺寸的,是否画的垂线不见了?那要怎样做才干使这些图形防腐处理上去呢?这就必要用到重绘了。

  纵然,我们家必要了解,为什么图形会突然不见?竟,电脑中间的使联系是。天然地集会也要涂漆,更改集会一定尺寸的时,原合成的不克不及遵守眼前的显示想要,到这地步必要重行绘制集会,但是此刻集会上的图形并不能胜任的重行再画一次,为什么?集会已默许为重绘办法,绘制(图形 g),因而我们家必要重写如此办法。,纵然我们家怎样能让电脑了解优于画了什么呢?这是就必要把每回画的愿意的记载到队列或大厦中,重行绘制时,可以本着队列或大厦停止。。天然地地,估计不赞成被产生出狱,以便于防腐处理唱片。。

估计是这么写的

 1publicclass Shape {
 2 3privateint x1, y1, x2, y2, width;
 4private Color color;
 5private String type;
 6private ImageIcon i;
 7private DrawMain dm;
 8private JTextField text;
 910public Shape(int x1, int y1, int x2, int y2, int width, Color color, String type, ImageIcon i, DrawMain dm,
11            JTextField 倒转术) {
12this.x1 = x1;
13this.y1 = y1;
14this.x2 = x2;
15this.y2 = y2;
16this.width = width;
17this.color = color;
18this.type = type;
19this.i = i;
20this.dm = dm;
21this.text = text;
22    }
232425publicvoid draw(Graphics2D g) {
26        g.setColor(色);
27         g.setStroke(new BasicStroke(width));
28if (以下略语线路) || (彩色铅笔) || ("iso_Tri") || (多角形)
29                 || (用力打) || (喷枪))) {
30            g.drawLine(1), y1, x2, y2);
31         } elseif ((椭圆红细胞增多症)) {
32             g.fillOval((1), x2页), (y1, y2), (2×2) - x1), (y2 - y1));
33         } elseif (略语rect)) {
34             g.drawRect((1), x2页), (y1, y2), (2×2) - x1), (y2 - y1));
35         } elseif (("RoundRect")) {
36             g.drawRoundRect((1), x2页), (y1, y2), (2×2) - x1), (y2 - y1),
37                     (2×2) - x1) / 3, (y2 - y1) / 3);
38         } elseif (("fill3DRect")) {
39             g.fill3DRect((1), x2页), (y1, y2), (2×2) - x1), (y2 - y1), true);
40         } elseif (("fillArc")) {
41double r = ((2×2) - x1) * (2×2) - x1) + y1 * y1) / (2 * y1);
42double m = ((double) (2×2) - x1) / r);
43             g.fillArc((1), x2页), (y1, y2), (2×2) - x1), (int) r, 0, (int) (m * 180 / ));
44         } elseif ((图像)) {
45             g.drawImage((), (1), x2页), (y1, y2), (2×2) - x1), (y2 - y1), 德国评分)
46         } elseif ((倒转术)) {
47            g.drawString((), x1, y1);
48        }
49    }
5051 }

现时此外每一成绩,色彩将会写在哪里?在先前写的DrawMain中

private List list = new ArrayList();

    publicvoid 色彩(图形 克) {
        super.paint(克);
        Graphics2D g = (Graphics2D) gr;
        (RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        for (int i = 0; i < (); i++) {
            Shape shape = (i);
            (g);
        }
    }

提示一下:不要遗忘向集会添加事变侦听器!!!!

这么一来,我们家的画图板就能无瑕可谪应用了。自然如此画图板跟零碎中间的比起来仍然差了很多了,诸如,取消、重组、防腐处理功用还不表现,剩的功用有待推动努力和至上的。。

发表评论

电子邮件地址不会被公开。 必填项已用*标注