Topic: 如何使用repaint()?望各位大侠帮小的一把

  Print this page

1.如何使用repaint()?望各位大侠帮小的一把 Copy to clipboard
Posted by: dragonzhang1981
Posted on: 2005-10-31 10:41

我使用jbuilder做了一个画板,能进行基本的图形绘制,可是一刷新主窗体,或者将窗体放大或缩小,绘制好的图形就会消失,应该对图形进行重画,repaint(),可是怎么用呢?跪求答案,望各位大侠帮小的一把!
下面是绘制部分的代码:
switch(lineType){
case 1:
//用背景颜色覆盖前面的直线
g.setColor(this.getBackground());
g.drawLine(p1.x,p1.y,p2.x,p2.y);
//设置当前画图颜色并绘制线段
g.setColor(colour);
g.drawLine(p1.x,p1.y,e.getX(),e.getY());
p2 = e.getPoint();

break;
case 2:
g.setColor(this.getBackground());
if(Math.abs(e.getX() - p1.x) > Math.abs(e.getY() - p1.y)){
//用背景颜色覆盖前面的圆
if(Math.abs(p2.x - p1.x) > Math.abs(p2.y - p1.y))
g.drawOval(p1.x,p1.y,Math.abs(p2.x-p1.x),Math.abs(p2.x-p1.x));
else
g.drawOval(p1.x,p1.y,Math.abs(p2.y-p1.y),Math.abs(p2.y-p1.y));
//设置当前的画图颜色并绘制圆
g.setColor(colour);
g.drawOval(p1.x, p1.y, Math.abs(e.getX() - p1.x),Math.abs(e.getX() - p1.x));

}
else{
////用背景颜色覆盖前面的圆
if(Math.abs(p2.x - p1.x) > Math.abs(p2.y - p1.y))
g.drawOval(p1.x,p1.y,Math.abs(p2.x-p1.x),Math.abs(p2.x-p1.x));
else
g.drawOval(p1.x,p1.y,Math.abs(p2.y-p1.y),Math.abs(p2.y-p1.y));
//设置当前的画图颜色并绘制圆
g.setColor(colour);
g.drawOval(p1.x, p1.y, Math.abs(e.getY() - p1.y),Math.abs(e.getY() - p1.y));
}
p2 = e.getPoint();

break;
case 3:
//用背景颜色覆盖前面的圆
g.setColor(this.getBackground());
g.drawRect(p1.x,p1.y,Math.abs(p2.x-p1.x),Math.abs(p2.y-p1.y));
//设置当前画图颜色并绘制矩形
g.setColor(colour);
g.drawRect(p1.x,p1.y,Math.abs(e.getX()-p1.x),Math.abs(e.getY()-p1.y));
p2 = e.getPoint();

break;
case 4:
//用背景颜色覆盖前面的椭圆
g.setColor(this.getBackground());
g.drawOval(p1.x,p1.y,Math.abs(p2.x-p1.x),Math.abs(p2.y-p1.y));
//设置当前画图颜色并绘制椭圆
g.setColor(colour);
g.drawOval(p1.x,p1.y,Math.abs(e.getX()-p1.x),Math.abs(e.getY()-p1.y));
p2 = e.getPoint();

break;
case 5:
//用背景颜色覆盖前面的填充圆
g.setColor(this.getBackground());
g.fillOval(p1.x,p1.y,Math.abs(p2.x-p1.x),Math.abs(p2.y-p1.y));
//设置当前画图颜色并绘制填充圆
g.setColor(colour);
g.fillOval(p1.x,p1.y,Math.abs(e.getX()-p1.x),Math.abs(e.getY()-p1.y));
p2 = e.getPoint();

break;
case 6:
//设置当前画图颜色并绘制任意线段
g.setColor(colour);
g.drawLine(p1.x,p1.y,e.getX(),e.getY());
p1 = e.getPoint();

break;
case 7:
//输出文字
JOptionPane optionPane = new JOptionPane();
String text = optionPane.showInputDialog((Component)e.getSource(),"输入文字:","Dialog for text input",JOptionPane.PLAIN_MESSAGE);
if(text != null){
g.drawString(text, e.getX(), e.getY());
}

2.Re:如何使用repaint()?望各位大侠帮小的一把 [Re: dragonzhang1981] Copy to clipboard
Posted by: losfound
Posted on: 2005-11-08 10:55

在窗体resize时调用repaint,象这样:
addComponentListener(new ComponentAdapter(){
public void componentResized(ComponentEvent e){
repaint();
}
});


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923