java 用Java重绘图形

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5289194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:26:31  来源:igfitidea点击:

Redrawing graphics in Java

javaswingjpanelgraphics2d

提问by Squeazer

I'm just getting into graphics in Java and I have a problem. I created a JFrame window (NetBeans Designer) with a JPanel panel and I drew some graphics on it. Then I added a JButton that changed a variable, which would change the X position of a square on JPanel.

我刚刚接触 Java 图形,但遇到了问题。我创建了一个带有 JPanel 面板的 JFrame 窗口(NetBeans Designer),并在其上绘制了一些图形。然后我添加了一个改变变量的 JButton,这将改变 JPanel 上正方形的 X 位置。

On button press this code would execute:

在按钮按下此代码将执行:

drawObject.setX(150);
drawObject.repaint();

drawObject is an instance of this class:

drawObject 是此类的一个实例:

public class sola extends JPanel {

    private int x = 10;

    @Override
    public void paintComponent(Graphics g){
        super.paintComponents(g);
        super.setBackground(Color.WHITE);

        g.setColor(Color.ORANGE);
        g.fill3DRect(x, 160, 100, 50, true);
    }

    public void setX(int xX){
        x = xX;
    }
}

Now, when I press the JButton, the rectangle does move to the new position, however it is still visible in the old position. Only when i resize the window does it refresh and the old rectangle disappears. How can i solve this problem, so that when i press the button, the rectangle is only visible in the new position?

现在,当我按下 JButton 时,矩形会移动到新位置,但它在旧位置仍然可见。只有当我调整窗口大小时它才会刷新并且旧的矩形消失。我该如何解决这个问题,以便当我按下按钮时,矩形仅在新位置可见?

采纳答案by Hovercraft Full Of Eels

It's

它是

super.paintComponent(g);

not

不是

super.paintComponents(g);  // note the s at the edn

Big difference between the two! The first one tells your JPanel to do all the housekeeping functions normally performed by the paintComponent method, including repainting the background (key for your project). The second, the one your calling doesn't do any of the above functionality. So my advice is to get rid of the trailing s in your super call.

两者区别很大!第一个告诉您的 JPanel 执行通常由paintComponent 方法执行的所有内务处理功能,包括重新绘制背景(项目的关键)。第二个,您调用的那个不执行上述任何功能。所以我的建议是去掉 super 调用中的尾随 s。

回答by Chris

You could use repaint()method to do tis.

您可以使用repaint()方法来做 tis。

If you use the paintComponent() on the panel. You should IMHO take care of the painting in the whole panel. There is no code in your example which takes care about deleting the old painted rectangles.

如果您使用面板上的paintComponent()。恕我直言,您应该处理整个面板中的绘画。您的示例中没有代码负责删除旧的绘制矩形。

What i recommend is creating an own Component for your rectangles. (You could extend from Component) then you can override the paintComponentmethod of these classes as you did in your panel. Because the Panel should act as a container component. Not as drawing the rectangles itsself.

我推荐的是为您的矩形创建一个自己的组件。(您可以从 Component 扩展)然后您可以paintComponent像在面板中一样覆盖这些类的方法。因为面板应该充当容器组件。不像绘制矩形本身。

Know add instances of these components to a normal JPanel. This should then update as expected.

知道将这些组件的实例添加到普通 JPanel 中。这应该会按预期更新。

回答by Bernd Elkemann

You can use the following methods from JComponent: ( http://download.oracle.com/javase/6/docs/api/javax/swing/JComponent.html)

您可以使用 JComponent 中的以下方法:(http://download.oracle.com/javase/6/docs/api/javax/swing/JComponent.html

void    repaint(long tm, int x, int y, int width, int height)
 Adds the specified region to the dirty region list if the component is showing.
void    repaint(Rectangle r)
 Adds the specified region to the dirty region list if the component is showing.

You can call those before redraw()

你可以打电话给那些之前 redraw()