Question

So I wrote a small program for a class. I designed it as a JApplet inside an undecorated JFrame, not in a browser. Other than that, it's a simple drawing program of sorts. You click two points to draw the selected shapes, then it calls a repaint. The problem I'm having is that when you draw while the program has been moved to my secondary monitor, the entire JApplet seems to disappear, only displaying the drawn shape. It only disappears after the 2nd point is selected, so I presume it does this on repaint().

My secondary monitor is using the exact same brand and resolution, even color profile.

Any other technical details, I'm using Java 1.7 (Can't recall which update off the top of my head), Windows 8 Enterprise 64x, using Eclipse's Run button to test.

Thanks in advance for any help!

Was it helpful?

Solution

I am indeed calling getGraphics(); in the init() method of the JApplet..

That is the problem. The Graphics object is a transient thing that will be repainted the very next time the JVM thinks there is any need to do so. That might be triggered by:

  • Changing the size or location of the window.
  • Covering it with another program and then removing the covering app.
  • Adding new components or changing values that are displayed.

See Performing Custom Painting for more details on how to do what you are attempting to achieve. OTOH Swing has a JLabel that can show a BufferedImage. You can use the BufferedImage in the way you want. When it is updated, call repaint() on the label to see the effect.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top