Hey guys, I'm having trouble with this program I'm making. When I run it at school, everything works fine. However, when I run it at home, the program glitches up kinda like when your computer freezes up and you start seeing double windows when you try to move it? It's really hard to explain, so I took pictures to try my best to explain what is going on.
When I first start up the program it looks like this:
Now, when I click somewhere to draw a shape (part of what the program is supposed to do), it glitches up like this:
This isn't even a part of the program, I can paint over this:
Also, when I try to move the window itself, for example, let's say I move below the desktop so I can only see half of it, it will erase some of it:
Like I said, this is a really hard problem to explain, and considering that it works at school, I don't think it has to deal anything with the code itself. Both Java versions on computers are updated.
You can create a new BufferedImage, use getGraphics to get its Graphics object, and do all your drawing to that.
Then use g.drawImage(img, 0, 0, this) in the paintComponent method to copy the contents to the panel's Graphics object. Be careful to check in the paintComponent method that the two images are still the same size, i.e. deal with the case where the window has been resized.
Here's an example of double-buffering that I copied from a website that might help:
(when I tried a similar example I had problems getting createImage() to work - it won't work if your panel isn't visible, so instead I just used something along the lines of Image bi = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB) instead.)