Content of JDesktopPane dissappears when resized - Java Swing
I have been working on Java Swing for a while. I paint something(draw some basic shapes like circle,rectangle etc) on a JDesktopPane and once I resize the frame window that contains jDesktopPane or drag some other windo开发者_如何学编程w over this frame, then the drawn shapes disappear. I use an object of the BufferedImage class so as to save the image. So Is there any way of preventing the shapes getting disappeared or repaint it when they disappear?
You need to make sure you are saving what you paint and repainting it each time in the paintComponent()
method. This method is called automatically whenever a repaint is needed (for example: because of a resize).
I can only provide a guess since you've decided not to post the necessary code, but my suggestions are:
- Don't get your Graphics object by calling getGraphics on a component. Instead,
- Be sure to do your drawing in a class that subclasses a JComponent or one of its children (such as a JPanel).
- draw your BufferedImage in the JComponent's paintComponent method immediately after calling super.paintComponent() in the same method. Use the Graphics object that is provided by the JVM and passed into the method's parameter.
- To be sure that your paintComponent method's signature is correct, place an @Override annotation immediately before it. Otherwise if it is not correct, the JVM will probably not call it when you expect it to.
- But also, please when asking a question here, try to give us enough information so we don't have to guess. Since your problem is graphics related, it would make sense to post your graphics-related code, right?
精彩评论