简体   繁体   中英

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 all I see is empty panels.

您应该将其绘制为JPanel的Graphics对象,以使其永久...在您的示例中,这是Graphics g ...

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM