• Post Reply Bookmark Topic Watch Topic
  • New Topic

Weird displaying issues between computers?  RSS feed

 
Ben Jass
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you overridde paintComponent and forget to call super.paintComponent as the first line?
 
Ben Jass
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks man, that helped. One thing though, now that I call super.paintComponent(g), I can only draw one thing at a time? Is there any simple fixes too this?
 
Rob Spoor
Sheriff
Posts: 21117
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keep a List of things that need to be painted, then paint everything in that List.
 
Ben Jass
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I was thinking that, I was just curious if there was any more efficient ways of doing so.
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)

 
Rob Camick
Ranch Foreman
Posts: 2821
15
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was just curious if there was any more efficient ways of doing so.


It depends on your exact requirement.

Custom Painting Approaches gives working examples of both approaches suggested here as well as some pros/cons of each approach.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!