图形内容在最大化上被删除
我正在开发一个 GUI 绘画应用程序。但问题是,在最小化然后最大化时,帧内容会被擦除。我知道问题是什么,就是最大化后再次调用油漆并且框架的内容没有正确处理。但即使知道错误后我也无法解决该行为。
这是负责此行为的主要代码:
public void paint(Graphics g) {
/*From minimized to maximized*/
System.err.println("Inside paint now ");
try{
if(color==colour.red)
g.setColor(Color.red);
else if(color==colour.green)
g.setColor(Color.green);
else if(color==colour.pink)
g.setColor(Color.PINK);
else if(color==colour.cyan)
g.setColor(Color.CYAN);
else if(color==colour.magenta)
g.setColor(Color.MAGENTA);
else if(color==colour.orange)
g.setColor(Color.ORANGE);
else if(color==colour.white)
g.setColor(Color.white);
else if(color==colour.black)
g.setColor(Color.BLACK);
else if(color==colour.yellow)
{
g.setColor(Color.YELLOW);
System.out.println("Colour selected ");
}
else if(stat==state.line)
{
Point p=getMousePosition();
x=p.x;y=p.y;
System.out.append("In here ");
System.out.append("the point is "+x+" "+y);
if(prevx==0 && prevy==0)
{
g.drawLine(x, y, x, y);
// System.out.append("the point is "+x+" "+y);
}
else
g.drawLine(x, y, prevx, prevy);
prevx=x;
prevy=y;
System.out.println("Line selected ");
}
else if(stat==state.rectangle)
{
if(x<prevx)
{
x=x+prevx;
prevx=x-prevx;
x=x-prevx;
}
if(y<prevy)
{
y=y+prevy;
prevy=y-prevy;
y=y-prevy;
}
g.drawRect(prevx,prevy,x-prevx,y-prevy);
System.out.println("Rectangle selected ");
}
else if(stat==state.circle)
{
if(x<prevx)
{
x=x+prevx;
prevx=x-prevx;
x=x-prevx;
}
if(y<prevy)
{
y=y+prevy;
prevy=y-prevy;
y=y-prevy;
}
g.drawOval(prevx,prevy,x-prevx,y-prevy);
System.out.println("Circle selected ");
}
else if(stat==state.ruler)
{
g.drawLine(x, y, prevx, prevy);
System.out.println("Ruler selected ");
}
else if(stat==state.eraser)
{
g.setColor(getBackground());
g.fillRect(x-10, y-10, 20, 20);
System.out.println("Eraser selected ");
}
else
{
System.out.append("nothing done");
}
}
catch(Exception e)
{
}
}
I am developing a GUI painting application. But the problem is that the frame contents get erased on minimizing and then maximizing. I know what the problem is, it's that paint is called again after maximizing and the frame's contents are not handled properly. But I could not solve the behavior even after knowing the error.
Here is the main code responsible for this behavior:
public void paint(Graphics g) {
/*From minimized to maximized*/
System.err.println("Inside paint now ");
try{
if(color==colour.red)
g.setColor(Color.red);
else if(color==colour.green)
g.setColor(Color.green);
else if(color==colour.pink)
g.setColor(Color.PINK);
else if(color==colour.cyan)
g.setColor(Color.CYAN);
else if(color==colour.magenta)
g.setColor(Color.MAGENTA);
else if(color==colour.orange)
g.setColor(Color.ORANGE);
else if(color==colour.white)
g.setColor(Color.white);
else if(color==colour.black)
g.setColor(Color.BLACK);
else if(color==colour.yellow)
{
g.setColor(Color.YELLOW);
System.out.println("Colour selected ");
}
else if(stat==state.line)
{
Point p=getMousePosition();
x=p.x;y=p.y;
System.out.append("In here ");
System.out.append("the point is "+x+" "+y);
if(prevx==0 && prevy==0)
{
g.drawLine(x, y, x, y);
// System.out.append("the point is "+x+" "+y);
}
else
g.drawLine(x, y, prevx, prevy);
prevx=x;
prevy=y;
System.out.println("Line selected ");
}
else if(stat==state.rectangle)
{
if(x<prevx)
{
x=x+prevx;
prevx=x-prevx;
x=x-prevx;
}
if(y<prevy)
{
y=y+prevy;
prevy=y-prevy;
y=y-prevy;
}
g.drawRect(prevx,prevy,x-prevx,y-prevy);
System.out.println("Rectangle selected ");
}
else if(stat==state.circle)
{
if(x<prevx)
{
x=x+prevx;
prevx=x-prevx;
x=x-prevx;
}
if(y<prevy)
{
y=y+prevy;
prevy=y-prevy;
y=y-prevy;
}
g.drawOval(prevx,prevy,x-prevx,y-prevy);
System.out.println("Circle selected ");
}
else if(stat==state.ruler)
{
g.drawLine(x, y, prevx, prevy);
System.out.println("Ruler selected ");
}
else if(stat==state.eraser)
{
g.setColor(getBackground());
g.fillRect(x-10, y-10, 20, 20);
System.out.println("Eraser selected ");
}
else
{
System.out.append("nothing done");
}
}
catch(Exception e)
{
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的投票是 mkrhrts 的评论,即当您应该覆盖
paintComponent
时,您却覆盖了paint
。覆盖
paint
是不好的做法,并且您似乎没有执行以下任何操作来确保它不会中断:super.paint()
paintComponent
、paintBorder
或paintChildren
查看 paint 方法的文档。
My vote is mkrhrts's comment, that you're overriding
paint
when you're supposed to overridepaintComponent
.Overriding
paint
is bad practice, and you don't appear to be doing any of the following to ensure it doesn't break:super.paint()
paintComponent
,paintBorder
, orpaintChildren
Check out the documentation for the paint method.