Repaint issue with JPanel inside another JPanel
I'm drawing shapes inside this JPanel
, which is also inside another main JPanel
. At repaint()
it only draws the shapes for one millisecond and then they disappear. They don't stay painted, why?
My paintComponent
method is something like this
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i < reportElements.size(); i++) {
this.reportElements.get(i).display((Graphics2D) pageComponents.get(i).getGraphics());
}
}
When the parent is a JEditorPane
with setEditable()
enabled, it works and we can see the shapes but when it's a JPanel
, after a millisecond al开发者_Go百科l I see is empty panels.
you should paint to the Graphics object of the JPanel for it to be permanent... In your example, that is the Graphics g
...
in this case what happens to those pageComponents, the small JPanels? The parent panel is not gonna draw the shapes on them is it?
I'm not sure I undertand your comment. Your main panel should contain child panels. The child panels should be added to the main panel using a layout manager. Then when Swing decides to repaint the main panel, it will also repaint all the child panels and in turn the child panels will repaint there shapes.
For what its worth Custom Painting Approaches has a working example of drawing shapes on a panel.
ultrajohn is dead on. You need to use the Graphics
you have been passed. Read on for the why...
Java 1.6 introduced the RepaintManager
that supports the optimizing of repaint requests. It has some subtle effects on painting.
In this case, you're working with multiple Graphics2D objects: the g
passed into paintComponent
and the value returned by the getGraphics
call.
The repaint manager has handed you g
on which to paint. Note: this does not paint on the screen, but on a temporary buffer (assuming the default double buffering).
In the paintComponent
call you are painting to the graphics obtained from getGraphics
of the various components. This is bypassing the repaint manager and painting directly to the unbuffered display.
When paintChildren
returns, the RepaintManager
kicks in to handle updating the double buffer. It paints the blank temporary buffer over the displayed buffer, effectively erasing what was painted through the graphics object obtained from getGraphics
It might be that paintChildren
or paintBorder
overdraws whatever you've drawn in your method. Perhaps try overriding paint
instead. There you have full control over what will be painted on the component, and you can decide yourself whether to further call paintComponent
, paintChildren
or paintBorder
at all.
精彩评论