File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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

ComponentListener to change image size with screen size.

Jake Cirino

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

Joined: Oct 27, 2005
Posts: 20276

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.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: ComponentListener to change image size with screen size.
It's not a secret anymore!