• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

ComponentListener to change image size with screen size.

 
Greenhorn
Posts: 17
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:



 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22684
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
And then the entire population worshiped me like unto a god. Well, me and this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic