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;// 公告色属性,希腊字母第12字用户选择的色 4privateint width = 1;// 公告最厚的部分属性,分钟希腊字母第12字用户选择 5private String type = "Line";// 公告图形属性,希腊字母第12字用户选择的图形 6privateint x1, y1, x2, y2, x3 = 0, y3 = 0, x4 = 0, y4 = 0, x5, y5;// 公告搭配值属性,希腊字母第12字鼠标按下和假释的搭配值 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 事情源女朋友要旨和操控要旨希腊字母第12字在
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 事情源女朋友要旨和操控要旨希腊字母第12字在
  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);
        }
    }

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

非常的一来,咱们的画图板就能完美的应用了。自然很画图板跟零碎达到目标比起来不动的差了很多了,诸如,取消、改善、同意功用还不引起,剩的功用有待更谈论和完成时。。

发表评论

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