PaintComponent 删除之前绘制的组件

发布于 2024-12-22 12:25:27 字数 956 浏览 1 评论 0原文

我正在编写一个简单的绘画程序。 我创建了一个JPanel,并重写了“public void paintComponent(Graphics g)”,我还创建了适当的Listeners。问题是,每次我画一个新形状时,我以前的形状就会消失,有谁知道我如何才能将以前的形状保留在原来的位置?我可以将 super.paintComponent(g) 拿走,但是 Jpanellayout 将会扭曲。 任何建议都将受到高度赞赏。 :) 这是我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

桃扇骨 2024-12-29 12:25:27

要绘制多个“矩形”、“方形”或“圆形”对象,需要将它们添加到集合中(例如 ArrayList‌ )以及每次调用 paintComponent(Graphics),迭代集合并绘制每个集合。

或者在 中绘制形状BufferedImage 并绘制图像。

..当我绘制越来越多的形状时,这会不会影响效率?因为每次我调用 repaint() 时,paintComponent 都必须绘制许多形状;

我可以给出三个答案。他们在这里:

  1. 是的。
  2. 但不,必须有数千才会导致明显的减速。
  3. 如果性能出现问题。使用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 to paintComponent(Graphics), iterate the collection and draw each one.

Either that or draw the shapes in a BufferedImage and draw the image instead.

..wouldnt this affect the efficiency as I draw more and more shapes? because then the paintComponent has to draw many shapes everytime I call repaint();

There are 3 answers that I can give to that. Here they are:

  1. Yes it would.
  2. But no, there would have to be thousands before causing visible slow-down.
  3. In the event that performance is a problem. Use the 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.

..also I need to write an undo function and a List will be useful in that case too

It does sound like the list is the way to go for this use-case.

短叹 2024-12-29 12:25:27

安德鲁·汤普森的答案是做你想做的事的“典型”方式。但是,如果您想更深入地了解 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 calls ComponentUI.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文