File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 19541
    
  16

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ComponentListener to change image size with screen size.
 
Similar Threads
Canvas & Scrollbras integrated
Skinnable ProgressBar and resize mechanism
Problems convering Applets to Applications
Something about anonymous classes and variables
AWT Applet to Application conversion