PaintComponent 删除之前绘制的组件
我正在编写一个简单的绘画程序。 我创建了一个JPanel
,并重写了“public void paintComponent(Graphics g)
”,我还创建了适当的Listeners
。问题是,每次我画一个新形状时,我以前的形状就会消失,有谁知道我如何才能将以前的形状保留在原来的位置?我可以将 super.paintComponent(g)
拿走,但是 Jpanel
的 layout
将会扭曲。 任何建议都将受到高度赞赏。 :) 这是我的paintComponent方法:
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int width = xend-xstart;
int height = yend - ystart;
if(width<0)
width *= -1;
if(height <0)
height *= -1;
if(color!= null && shape !=null){
if(fill.isSelected())
{
g.setColor(color);
if(shape.equals("Rectangle"))
g.fillRect(xstart, ystart, width, height);
if(shape.equals("Square"))
g.fillRect(xstart, ystart, width, width);
if(shape.equals("Circle"))
g.fillOval(xstart,ystart,width ,width);
}
}
}
I am writing a simple painting program.
I have created a JPanel
, and have over written "public void paintComponent(Graphics g)
", I have also created the appropriate Listeners
. The problem is that everytime I draw a new shape, my previous one vanishes, Does anyone know how I can keep the previous shapes in their place? I can take the super.paintComponent(g)
away, but then the Jpanel's
layout
will be distorted.
Any suggestion is highly appreciated. :)
this is my paintComponent method:
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int width = xend-xstart;
int height = yend - ystart;
if(width<0)
width *= -1;
if(height <0)
height *= -1;
if(color!= null && shape !=null){
if(fill.isSelected())
{
g.setColor(color);
if(shape.equals("Rectangle"))
g.fillRect(xstart, ystart, width, height);
if(shape.equals("Square"))
g.fillRect(xstart, ystart, width, width);
if(shape.equals("Circle"))
g.fillOval(xstart,ystart,width ,width);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要绘制多个“矩形”、“方形”或“圆形”对象,需要将它们添加到集合中(例如
ArrayList
)以及每次调用paintComponent(Graphics)
,迭代集合并绘制每个集合。或者在
中绘制形状BufferedImage
并绘制图像。我可以给出三个答案。他们在这里:
BufferedImage
。这样,对于每个新添加的形状,只会再向图像绘制一个形状,无论之前渲染了多少个..百万。。听起来列表确实是此用例的最佳选择。
To draw multiple "Rectangle", "Square" or "Circle" objects it will be necessary to add them to a collection (e.g.
ArrayList
) and each call topaintComponent(Graphics)
, iterate the collection and draw each one.Either that or draw the shapes in a
BufferedImage
and draw the image instead.There are 3 answers that I can give to that. Here they are:
BufferedImage
. That way only one more shape is drawn to the image for each newly added shape, no matter how many ..millions have previously been rendered to it.It does sound like the list is the way to go for this use-case.
安德鲁·汤普森的答案是做你想做的事的“典型”方式。但是,如果您想更深入地了解 Swing...
super.paintComponent() 所做的事情之一(最终)是访问
JComponent.paintComponent()
,它调用ComponentUI.update()
。 Javadoc 说(斜体是我添加的):“默认情况下,此方法将用其背景颜色填充指定的组件(如果其 opaque 属性为 true),然后立即调用 Paint。”
因此,请尝试调用
setOpaque(false)
。然而,这通常会导致其他问题,例如当您确实想要删除之前绘制的内容时。Andrew Thompson's answer is the "typical" way to do what you want. However, if you want to plunge deeper into the depths of Swing...
One of the things that super.paintComponent() does (eventually) is get to
JComponent.paintComponent()
, which callsComponentUI.update()
. The Javadocs say (italics added by me):"By default this method will fill the specified component with its background color (if its opaque property is true) and then immediately call paint."
So, try calling
setOpaque(false)
. However, that often leads to other issues, such as when you really do want to erase what was drawn before.