1.重绘概念
如果未重绘,当界面发生形状大小改变的时候,界面上面已经画的图形会消失。重绘后,会将之前画的图形还原。
2.重绘调用的方法
当界面大小发生改变的时候,会自动回调paint方法。显示调用repaint方法会自动调用paint方法。
3.具体用法
每个组件或者窗体都有paint方法,想重绘哪个组件的paint方法,就重写哪个组件的paint。
4.关于repaint方法
由于自己类本身没有repaint方法,调用repaint方法会调用父类的repaint方法。父类的repaint方法会调用子类的paint方法。
5.匿名内部类
MouseAdapter mouseListener = new MouseAdapter(){
};
匿名内部类,实际上已经发生了继承,并且已经发生了向上转型,写在里面的方法相当于重写。
6.注意事项
不要创建多个窗体,使用同一个窗体。
7.总结
一切方法都是由对象调用的,直接写repait()方法,也是直接由该类的this对象调用的。同理,一切属性都是由对象调用的。
探索能力有待提升。
8.代码
DrawUI.java
1 importjava.awt.FlowLayout;2 importjava.awt.Graphics;3 importjava.awt.event.ActionEvent;4 importjava.awt.event.ActionListener;5 importjava.awt.event.MouseAdapter;6 importjava.awt.event.MouseEvent;7
8 importjavax.swing.JButton;9 importjavax.swing.JFrame;10
11 public class DrawUI extendsJFrame{12 Graphics g;13 Shape[] shapes = new Shape[500];14 int count = 0;15 String type;16
17 ActionListener actionLisener = newActionListener() {18
19 @Override20 public voidactionPerformed(ActionEvent e) {21 //TODO Auto-generated method stub
22 type =e.getActionCommand();23 System.out.println("type "+type);24 if("清屏".equals(type)) {25 shapes = new Shape[500];26 count = 0;27 System.out.println("清屏"+count);28 repaint();29 }30 }31
32 };33
34 MouseAdapter mouseListener = newMouseAdapter(){35 intx1,y1,x2,y2;36 public voidmousePressed(MouseEvent e) {37 x1 =e.getX();38 y1 =e.getY();39 }40
41 public voidmouseReleased(MouseEvent e) {42 x2 =e.getX();43 y2 =e.getY();44
45 if("画直线".equals(type)) {46 g.drawLine(x1, y1, x2, y2);47 shapes[count] = newShape(x1,y1,type);48 shapes[count].x2 =x2;49 shapes[count].y2 =y2;50 System.out.println("count "+count);51 count++;52 }else if("画圆".equals(type)) {53 g.drawOval(x2, y2, 40, 40);54 shapes[count] = newShape(x2,y2,type);55 shapes[count].width = 40;56 shapes[count].height = 40;57 System.out.println("count "+count);58 count++;59 }60
61 }62 };63
64
65
66 public voidinit() {67 JFrame jf = this;68 jf.setSize(1500,1000);69 jf.setLocationRelativeTo(null);70 jf.setDefaultCloseOperation(3);71 jf.setLayout(newFlowLayout());72 JButton btn1 = new JButton("画直线");73 JButton btn2 = new JButton("画圆");74 JButton btn3 = new JButton("清屏");75 JButton btn4 = new JButton("按钮重绘") {76 public voidpaint(Graphics g) {77 super.paint(g);78 g.drawLine(0, 0, 10, 20);79 if(count>2)80 g.drawOval(0, 0, 30, 40);81 }82 };83 jf.add(btn1);84 jf.add(btn2);85 jf.add(btn3);86 jf.add(btn4);87
88 jf.setVisible(true);89
90 g =jf.getGraphics();91
92 jf.addMouseListener(mouseListener);93 btn1.addActionListener(actionLisener);94 btn2.addActionListener(actionLisener);95 btn3.addActionListener(actionLisener);96 }97
98
99 public voidpaint(Graphics g) {100 super.paint(g);101 for(int i=0;i
106 }else if(shape.type.equals("画圆")){107 g.drawOval(shape.x1, shape.y1, shape.width, shape.height);108 //System.out.println("画圆");
109 }110 }111 }112
113
114
115 public static voidmain(String[] args) {116 newDrawUI().init();117 }118
119 }
Shape.java
1 public classShape {2 intx1,y1,x2,y2;3 String type;4 intwidth;5 intheight;6
7 public Shape(int x1, inty1,String type) {8 super();9 this.x1 =x1;10 this.y1 =y1;11 this.type =type;12 }13
14 }
9.附录
A.java
1 packagep;2
3 public classA {4 public voidrepaint() {5 System.out.println("Arepaint");6 paint();7 }8
9 public voidpaint() {10 System.out.println("Apaint");11 }12 }
B.java
1 packagep;2
3 public class B extendsA{4
5 public voidpaint() {6 System.out.println("Bpaint");7 }8 }
Test.java
1 packagep;2
3 public classTest {4
5 public static voidmain(String[] args){6 A a = newB();7 a.repaint(); //输出Arepaint Bpaint
9 }10
11 }