Difference between manual resize and repaint, invalidate, validate etc etc?
Using Jdk 1.5 and under windows XP I am drawing directly in a JPanel.
And thus creating a drawing of my own when the system calls:
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
Line2D lineInArrow =
new Line2D.Double(. . . );
g2.draw(lineInArrow);
Ellipse2D ellipse = new Ellipse2D.Double(. . . );
g2.draw(ellipse);
}
The program of course has much more detail but that is the essence of it.
Now, it turns out that my drawings sometimes become rather messed up and needs to be redrawn so I have tried calling. repaint(), invalidate(), validate(), paintImmediately() in many different combinations and several times.
As the system is a more complex than just a JPanel - the JPanel is
contained in a JScrollPane for instance and all is set in a JDialog - I have also tried calling the different parts with these methods and in many different combinations.
Even calling the parts repeatedly from another Thread so that the methods get called every second or so does not help.
NOTHING HAPPENS to the messed up drawing
UNLES I DO A MANUAL RESIZE of the window. Then the drawing becomes OK!!!
Yes, you guessed right, I have also tried resizing from the program
by calls such as:
Dimension currentSize = this.getSize();
this.setSize(100, 100);
this.setSize(currentSize);
repaint();
The window flickers - gets smaller and then gets back to its original size.
But the drawing, if messed up, is still messed up in exactly the same way.
SO WHAT IS HAPPENING WHEN I DO A MANUAL RESIZE BUT NEVER HAPPENS WHEN I DO ANY OF THE ABOVE?
And, can I somehow initiate this - whatever it is - by programming rather than by a manual resizing of the window?
# 1
What do you mean when you say that the drawing is messed up?
If you want further help post a Short, Self Contained, Compilable, Example (SSCCE):
http://homepage1.nifty.com/algafield/sscce.html
And use code formatting (code tags) when posting code.
http://forum.java.sun.com/help.jspa?sec=formatting
# 2
With messed up I simply mean that it is not properly redrawn.
For example a rectangle that should have been drawn is not shown - not drawn. Or a String is not drawn in its proper place.
But when I do a manual resize (using the mouse) things are drawn correctly. Sometimes I have to do more than one resize to get everything drawn correctly.
I might also add that I have tried some other more or less weird strategis to get this "manual resize like painting" activated such as:
Calling the paintComponent method directly:
Graphics g = this.mainDrawPanel.getGraphics();
mainDrawPanel.paintComponent(g);
Trying to make the system understand that something DO have changed:
JButton aDummyButton = new JButton("Hi there - did you see me?");
mainDrawPanel.add(aDummyButton);
this.repaint();
mainDrawPanel.remove(aDummyButton);
this.repaint();
Nothing makes anything happen - except the manual resize of the window.
So what is so special about a manual resize of a window? What actually goes on when this happens? - That does NOT happen in all the other attempts?
# 4
I am not sure what you are getting at.
There is nothing wrong with the method calls (g2.draw() etc). Things
are ordered to be drawn in the correct size and position etc.
It just does not happen until I do a manual resize.
Thus, the point of inquiry must start from there: What actually happens
when I do the manual resize?
# 6
Pay attention to the graphics context! You do:
Graphics2D g2 = (Graphics2D)g;
So you use the graphics you receive from outside. But it is
shared across other panels, for performance reason. It is
very important to leave its state unchanged when your
drawing phase ends. If you prefer, you can even create
a clone with:
Graphics2D g2 = (Graphics2D)g.create();
In this case, at the end, you should release system resources
with:
g2.dispose();