This week's book giveaway is in the General Computing forum.
We're giving away four copies of Learning Regular Expressions and have Ben Forta on-line!
See this thread for details.
Win a copy of Learning Regular Expressions this week in the General Computing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Weird displaying issues between computers?  RSS feed

 
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.
 
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?
 
Sheriff
Posts: 21461
96
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.)

 
Rancher
Posts: 2942
19
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!