Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1326719
  • 博文数量: 131
  • 博客积分: 9950
  • 博客等级: 中将
  • 技术积分: 1492
  • 用 户 组: 普通用户
  • 注册时间: 2006-06-05 18:32
文章分类

全部博文(131)

文章存档

2008年(3)

2007年(37)

2006年(91)

我的朋友

分类: Java

2007-07-02 04:13:58

import java.awt.*;
import java.awt.event.*;

class MyCanvas extends Canvas{
    int n = -1;
    MyCanvas(){
        setSize(150,120);
        setBackground(Color.pink);
        }
    public void paint(Graphics g){
        g.setColor(Color.red);
        g.drawString("部分清除时我们将消失",10,12);
        g.drawString("我们正在学习repaint方法",10,80);
        }
    public void setN(int n){
        this.n = n;
        }
    public void update(Graphics g){
        int width = 0,heigth = 0;
        width = getSize().width;
        heigth = getSize().height;
        if(n==0){
            g.clearRect(0,0,width,heigth);
            //paint(g);//如果取消该注释,updata的功能就与父类相同
            }
        else if(n==1){
            g.clearRect(0,0,width,40);
            }
        }
    }
    
    public class Test40 extends java.applet.Applet implements ActionListener{
        Button b1 ,b2;
        MyCanvas canvas;
        public void init(){
            canvas = new MyCanvas();
            b1=new Button("全部清除");b2=new Button("部分清除");
            b1.addActionListener(this);b2.addActionListener(this);
            add(b1);add(b2);add(canvas);
            }
        public void actionPerformed(ActionEvent e){
            if(e.getSource()==b1){
                canvas.setN(0);
                canvas.repaint();
                }
            if(e.getSource()==b2){
                canvas.setN(1);
                canvas.repaint();
                }
            }
        }

       
public void update(Graphics g)更新容器。该方法将 update 方法转发给任意一个是这个容器子组件的轻量级组件。如果重新实现此方法,那么应该调用 super.update(g) 方法,从而可以正确地呈现轻量级组件。如果通过 g 中的当前剪切设置完全剪切某个子组件,则不会将 update() 转发给这个子组件。

       覆盖:
       类 Component 中的 update
       参数:
      g - 指定的 Graphics 窗口
==============================================
public void clearRect(int x,
                    int y,
                  int width,
                int height)重写 Graphics.clearRect。

指定者:
类 Graphics 中的 clearRect
参数:
x - 要清除矩形的 x 坐标。
y - 要清除矩形的 y 坐标。
width - 要清除矩形的宽度。
height - 要清除矩形的高度

阅读(2257) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~