关于Paint重画问题。。

qqchilaidefeng 2010-12-11 11:12:08
这个是,我用图形的颜色和背景的颜色相同的方法,但我觉得这种方法使代码太长了。有没有直接的重画方法?
也就是在画第二个图形的时候,先把第一个图形给覆盖掉


import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class huantu extends Applet implements ActionListener{
Graphics g;
Panel panel=new Panel();
Button button1=new Button("画直线");
Button button2=new Button("画矩形");
Button button3=new Button("画多边形");
Button button4=new Button("画圆");
Button button5=new Button("画扇形");
public void init(){
add(button1);
add(button2);
add(button3);
add(button4);
add(button5);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
g = getGraphics();
if(e.getSource()==button1)
paint(g,1);
else if(e.getSource()==button2)
paint(g,2);
else if(e.getSource()==button3)
paint(g,3);
else if(e.getSource()==button4)
paint(g,4);
else if(e.getSource()==button5)
paint(g,5);
}

public void paint(Graphics g,int i){
if(i==1){
g.setColor(Color.white); //覆盖矩形
g.drawRect(50, 50, 50, 100);
g.fillRect(130, 50, 50, 100);
g.fillRect(200, 50, 100, 50);
int x1[]={20,60,100,130,80}; // 覆盖多边形
int y1[]={60,80,150,40,60};
int x2[]={220,260,300,330,280};
int y2[]={60,80,150,40,60};
g.drawPolygon(x1, y1, 5);
g.fillPolygon(x2, y2, 5);
g.drawOval(60, 60, 60, 60); //覆盖圆
g.fillOval(180, 60, 60, 60);
g.fillArc(60, 80, 80, 80, 30, 70); // 覆盖扇形
g.fillArc(200, 80, 80, 80, 60, 140);
g.setColor(Color.red); //画直线
g.drawLine(30, 60, 80, 60);
g.drawLine(100, 60, 150, 100);
g.drawLine(160, 100, 270, 60);
}
else if(i==2){
g.setColor(Color.white); //覆盖直线
g.drawLine(30, 60, 80, 60);
g.drawLine(100, 60, 150, 100);
g.drawLine(160, 100, 270, 60);
int x1[]={20,60,100,130,80}; //覆盖多边形
int y1[]={60,80,150,40,60};
int x2[]={220,260,300,330,280};
int y2[]={60,80,150,40,60};
g.drawPolygon(x1, y1, 5);
g.fillPolygon(x2, y2, 5);
g.drawOval(60, 60, 60, 60); // 覆盖圆
g.fillOval(180, 60, 60, 60);
g.fillArc(60, 80, 80, 80, 30, 70); //覆盖扇形
g.fillArc(200, 80, 80, 80, 60, 140);
g.setColor(Color.green); //画矩形
g.drawRect(50, 50, 50, 100);
g.fillRect(130, 50, 50, 100);
g.fillRect(200, 50, 100, 50);
}
else if(i==3){
g.setColor(Color.white); //覆盖直线
g.drawLine(30, 60, 80, 60);
g.drawLine(100, 60, 150, 100);
g.drawLine(160, 100, 270, 60);
g.drawRect(50, 50, 50, 100); //覆盖矩形
g.fillRect(130, 50, 50, 100);
g.fillRect(200, 50, 100, 50);
g.drawOval(60, 60, 60, 60); //覆盖圆
g.fillOval(180, 60, 60, 60);
g.fillArc(60, 80, 80, 80, 30, 70); // 覆盖扇形
g.fillArc(200, 80, 80, 80, 60, 140);
g.setColor(Color.blue); //画多边形
int x1[]={20,60,100,130,80};
int y1[]={60,80,150,40,60};
int x2[]={220,260,300,330,280};
int y2[]={60,80,150,40,60};
g.drawPolygon(x1, y1, 5);
g.fillPolygon(x2, y2, 5);
}
else if(i==4){
g.setColor(Color.white); //覆盖直线
g.drawLine(30, 60, 80, 60);
g.drawLine(100, 60, 150, 100);
g.drawLine(160, 100, 270, 60);
g.drawRect(50, 50, 50, 100); //覆盖矩形
g.fillRect(130, 50, 50, 100);
g.fillRect(200, 50, 100, 50);
int x1[]={20,60,100,130,80}; //覆盖多边形
int y1[]={60,80,150,40,60};
int x2[]={220,260,300,330,280};
int y2[]={60,80,150,40,60};
g.drawPolygon(x1, y1, 5);
g.fillPolygon(x2, y2, 5);
g.fillArc(60, 80, 80, 80, 30, 70); //覆盖扇形
g.fillArc(200, 80, 80, 80, 60, 140);
g.setColor(Color.pink); //画圆
g.drawOval(60, 60, 60, 60);
g.fillOval(180, 60, 60, 60);
}
else if(i==5){
g.setColor(Color.white); //覆盖直线
g.drawLine(30, 60, 80, 60);
g.drawLine(100, 60, 150, 100);
g.drawLine(160, 100, 270, 60);
g.drawRect(50, 50, 50, 100); //覆盖矩形
g.fillRect(130, 50, 50, 100);
g.fillRect(200, 50, 100, 50);
int x1[]={20,60,100,130,80}; //覆盖多边形
int y1[]={60,80,150,40,60};
int x2[]={220,260,300,330,280};
int y2[]={60,80,150,40,60};
g.drawPolygon(x1, y1, 5);
g.fillPolygon(x2, y2, 5);
g.drawOval(60, 60, 60, 60); //覆盖圆
g.fillOval(180, 60, 60, 60);
g.setColor(Color.orange); //画扇形
g.fillArc(60, 80, 80, 80, 30, 70);
g.fillArc(200, 80, 80, 80, 60, 140);
}
}
}

...全文
128 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
magong 2010-12-11
  • 打赏
  • 举报
回复
不需要覆盖,在每次画图之前清除就行了,如下

g.clearRect(0, 0, this.getWidth(), this.getHeight() );
qqchilaidefeng 2010-12-11
  • 打赏
  • 举报
回复
谢了啊。。

50,528

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧