JavaSwing中的重绘

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 }

 

转载于:https://www.cnblogs.com/pmz-blog/p/11106597.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值