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

提示一下:不要遗忘向子组件立法机构添加事情侦听器!!!!

如此一来,人们的画图板就能完满运用了。自然同样画图板跟体系中间的比起来还要差了很多了,比如,取消、革新、避免浪费功用还不做,剩的功用有待深一层的详细地检查和使完成。。

发表评论

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