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);
        }
    }

提示一下:不要遗忘向子组件组分添加事变侦听器!!!!

这样地一来,人们的画图板就能优秀的运用了。自然这般画图板跟体系打中比起来黑金色、黑色差了很多了,譬如,取消、改装、供养功用还不可购得的,剩的功用有待推动详细地检查和极好的。。

发表评论

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