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 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.附錄
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 }