This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ComponentListener to change image size with screen size.

 
Jake Cirino
Greenhorn
Posts: 17
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, I'm developing a game right now, and I wan't it to resize the background (and eventually all the images) when the screen size changes.

I cant figure out how to make it actually paint though, I know the thing works because I've printed the variables before.

Heres what I got so far:



 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
put the background panel in a parent
that has an appropriate layoutManager
and it will all be done for you.
 
Rob Spoor
Sheriff
Pie
Posts: 20552
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never loop like that. You probably don't know that all painting is done on the Event Dispatcher Thread (EDT). Also, event listeners are also called on the same EDT. That means that the repaint flag will never change because the event listener is never called because the painting never ends.

You don't need a loop inside your paintComponent method. Just paint the image, nothing more. All you need to do is make sure the component is painted when it is resized. Fortunately for you, that automatically happens.

So in short:
1) Drop your ComponentListener.
2a) Extend the component and override its paintComponent method to draw the image. Make sure you call super.paintComponent(g) as the first line.
2b) Alternatively, use a special Border for the painting of the image:
The clip is used so that any other borders around this one won't be painted over.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic