Golightly
Golightly

Reputation: 11

java.awt.Graphics2D repaint() method

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?

Upvotes: 0

Views: 151

Answers (1)

WJS
WJS

Reputation: 40034

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

Upvotes: 2

Related Questions