This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of DevSecOps Adventures: A Game-Changing Approach with Chocolate, LEGO, and Coaching Games and have Dana Pylayeva on-line!
See this thread for details.
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Weird displaying issues between computers?

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22809
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3324
32
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
This is awkward. I've grown a second evil head. I'm going to need a machete and a tiny ad ...
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic