java.awt.Graphics2D repaint()方法

发布于 2025-01-09 18:24:23 字数 1388 浏览 6 评论 0原文

我遇到了两个不同的问题,一个是一般的 Graphics2D 问题,另一个是专门的 repaint() 方法。

首先是 repaint() 问题。我有一个根据用户选择的时间间隔(可以是 1 秒到几分钟)进行更新的图。问题是,一旦调用了 repaint() 方法,它就会在代码的每次迭代中不断地调用自己,而不是只在我想要的时候调用。有谁知道这是为什么以及如何阻止它?我尝试将代码放在带有标志的 if 语句中,但是一旦该标志变为 false,无论标志是否为 true,PlotUpdate 类都将永远不会再次运行。现在,我在执行整个代码之前将标志设置为在某些情况下“返回”。它使整个代码无法执行,但仍然不太理想。

一般的 Graphics2D 问题是,每当我更改 JPanel 上的 JButton 等对象的可见性时,我会重建两个单独的 Graphics2D 类,页面上的其他内容不会执行任何操作。如果在进程运行和绘图过程中发生这种情况,它将重建整个图形并从字面上删除所有先前绘制的点。我尝试使用 getPaint() 和 setPaint() 方法在删除所有内容之前尝试捕获数据,但无济于事。

这是一个大型项目的一小部分,因此确切的代码非常复杂,但这里是一个细分。

我有一个始终运行的主类,在该类中我声明了一个 PlotUpdate 类的实例,该实例在用户选择的时间间隔上调用。

我的 PlotUpdate 类的主旨

public class Project{
    protected static PlotUpdate plot1;
    
    public static void main (Strings[] args){
        PlotUpdate plot1 = new PlotUpdate();

        if (updateFlag){
            plot1.paintComponent(g);
        }
    }
}

是:

if (!Project.updateFlag) // exit strategy to keep the entire code from executing for no reason
    {
     return;
    }
    public class PlotUpdate extends JPanel{
            public void paintComponent(Graphics g);
            Graphics2D g1 = (Graphics2D)g;
            super.paintComponent(g);
            g1.setPaint(new Color(230,0,0));
            g1.draw(new Line2D.Double(x1,y1,x2,y2));
            repaint();
    }

有什么想法吗?有人吗?

I am experiencing two separate issues one with Graphics2D in general and one with the repaint() method specifically.

First the repaint() issue. I have a plot that updates based upon a user chosen time interval which can be anywhere from 1 second to several minutes. The problem is that once the repaint() method has been called, it continuously recalls itself at every iteration of the code instead of just when I want it. Does anyone know why this is and how to stop it? I tried placing my code within an if statement with a flag but once that flag went to false the PlotUpdate class would never run again regardless of a true flag or not. Now I have flags set up to "return" in certain situations before the entire code is executed. It keeps the entire code from executing but it is still less than ideal.

The general Graphics2D issue is that whenever I change the visibility of an object such as a JButton on my JPanel the two separate Graphics2D classes I have get rebuilt, nothing else on the page does. If this happens while the process is running and the plotting is in process, it rebuilds the entire graphic and literally erases all of the previously plotted points. I have tried using the getPaint() and setPaint() methods to try catch the data before it erases everything but to no avail.

This is a small part of a large project so the exact code is quite intricate but here is a breakdown.

I have my main class that is always running and in that class I declare an instance of my PlotUpdate class that is called on the user chosen interval.

The gist of my main

public class Project{
    protected static PlotUpdate plot1;
    
    public static void main (Strings[] args){
        PlotUpdate plot1 = new PlotUpdate();

        if (updateFlag){
            plot1.paintComponent(g);
        }
    }
}

The gist of my PlotUpdate class is:

if (!Project.updateFlag) // exit strategy to keep the entire code from executing for no reason
    {
     return;
    }
    public class PlotUpdate extends JPanel{
            public void paintComponent(Graphics g);
            Graphics2D g1 = (Graphics2D)g;
            super.paintComponent(g);
            g1.setPaint(new Color(230,0,0));
            g1.draw(new Line2D.Double(x1,y1,x2,y2));
            repaint();
    }

Any thoughts? Anyone?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

晨敛清荷 2025-01-16 18:24:23

快速观察是不要将 repaint 放在 paintComponent() 中。 它会锁定你的 gui。当您更新了需要重新绘制的项目时,请在其他地方调用它。并且永远不要直接调用paintComponent。始终使用repaint将其放置在EDT(事件调度线程)上。

此外,如果您有一个要在 paintComponent 中绘制的图形,则必须完整地绘制它。这是因为 super.paintComponent(g) 将按其应有的方式清除先前绘制的对象。

例如,

List<Point> points = ... some list of points
for (int i = 0; i < points.size()-1; i++) {
    g1.drawLine(points.getX(i), points.getY(i), points.getX(i+1), points.getY(i+1));
}

每次调用 repaint 时,您可能都会向 points 添加一个新点。但每次都必须将它们全部绘制出来。

欲了解更多详细信息
查看 2D 图形自定义绘画

Quick observation is do not put repaint in paintComponent(). It will lock up your gui. Call it elsewhere when you have updated items that need to be repainted. And never call paintComponent directly. Always use repaint to place it on the EDT (Event Dispatch Thread).

Also if you have a graph that is to be drawn in paintComponent, it must be drawn its entirety. This is because super.paintComponent(g) will clear the previously painted object(s) as it should.

E.g

List<Point> points = ... some list of points
for (int i = 0; i < points.size()-1; i++) {
    g1.drawLine(points.getX(i), points.getY(i), points.getX(i+1), points.getY(i+1));
}

Each time you call repaint you may have added a new point to points. But they must all be drawn each time.

For more detailed information
check out 2D Graphics and Custom Painting in the Java Tutorials

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