This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes ComponentListener to change image size with screen size. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "ComponentListener to change image size with screen size." Watch "ComponentListener to change image size with screen size." New topic
Author

ComponentListener to change image size with screen size.

Jake Cirino
Greenhorn

Joined: Jul 27, 2012
Posts: 17

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

Joined: Jun 09, 2003
Posts: 4632
put the background panel in a parent
that has an appropriate layoutManager
and it will all be done for you.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
Don't get me started about those stupid light bulbs.
 
subject: ComponentListener to change image size with screen size.
 
Similar Threads
Canvas & Scrollbras integrated
Problems convering Applets to Applications
Skinnable ProgressBar and resize mechanism
AWT Applet to Application conversion
Something about anonymous classes and variables