1. redraw concept
If you do not redraw, when the interface shape and size change occurs, the interface has been drawn above the graphics will disappear. After the redraw, graphics will be drawn before the reduction.
2. The method of redrawing call
When the screen size is changed, it will automatically call the paint method. Display call repaint method automatically calls the paint method.
3. Specific usage
Each component has a paint method or form, want to paint redraw method which component, which component of paint on the rewrite.
4. About repaint method
Because of their class itself does not repaint method calls repaint method calls the repaint method of the parent class. repaint method will call the parent class method paint subclass.
The anonymous inner classes
MouseAdapter mouseListener = new MouseAdapter(){
};
Anonymous inner classes, inheritance has actually taken place, and the transition has taken place up, written on the inside of the method is equivalent to rewrite.
6. Precautions
Do not create more than one form, use the same form.
7. Summary
All methods are invoked by the object, direct write repait () method, but also directly from this class of objects called. Similarly, all the attributes of an object are called.
Explore the ability to be improved.
8. Code
DrawUI.java
1 import java.awt.FlowLayout; 2 import java.awt.Graphics; 3 import java.awt.event.ActionEvent; 4 import java.awt.event.ActionListener; 5 import java.awt.event.MouseAdapter; 6 import java.awt.event.MouseEvent; 7 8 import javax.swing.JButton; 9 import javax.swing.JFrame; 10 11 public class DrawUI extends JFrame{ 12 Graphics g; 13 Shape[] shapes = new Shape[500]; 14 int count = 0; 15 String type; 16 17 ActionListener actionLisener = new ActionListener() { 18 19 @Override 20 public void actionPerformed(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 = new MouseAdapter(){ 35 int x1,y1,x2,y2; 36 public void mousePressed(MouseEvent e) { 37 x1 = e.getX(); 38 y1 = e.getY(); 39 } 40 41 public void mouseReleased(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] = new Shape(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] = new Shape(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 void init() { 67 JFrame jf = this; 68 jf.setSize(1500,1000); 69 jf.setLocationRelativeTo(null); 70 jf.setDefaultCloseOperation(3); 71 jf.setLayout(new FlowLayout()); 72 JButton btn1 = new JButton("画直线"); 73 JButton btn2 = new JButton("画圆"); 74 JButton btn3 = new JButton("清屏"); 75 JButton btn4 = new JButton("按钮重绘") { 76 public void paint(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 void paint(Graphics g) { 100 super.paint(g); 101 for(int i=0;i<count;i++) { 102 Shape shape = shapes[i]; 103 if(shape.type.equals("画直线")) { 104 g.drawLine(shape.x1, shape.y1,shape.x2, shape.y2); 105 // System.out.println("画直线"); 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 void main(String[] args) { 116 new DrawUI().init(); 117 } 118 119 }
Shape.java
1 public class Shape { 2 int x1,y1,x2,y2; 3 String type; 4 int width; 5 int height; 6 7 public Shape(int x1, int y1,String type) { 8 super(); 9 this.x1 = x1; 10 this.y1 = y1; 11 this.type = type; 12 } 13 14 }
9. Appendix
A.java
1 package p; 2 3 public class A { 4 public void repaint() { 5 System.out.println("Arepaint"); 6 paint(); 7 } 8 9 public void paint() { 10 System.out.println("Apaint"); 11 } 12 }
B.java
1 package p; 2 3 public class B extends A{ 4 5 public void paint() { 6 System.out.println("Bpaint"); 7 } 8 }
Test.java
1 package p; 2 3 public class Test { 4 5 public static void main(String[] args){ 6 A a = new B(); 7 a.repaint(); //输出 Arepaint Bpaint
9 } 10 11 }