Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Image Resize on window Resize

 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Please have a look at the following code



In here what I am trying to do is to re size the image applied to the JLabel on window resize. But my image is not getting resized. Please help..
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone help me please?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
have the image drawn via the label's paintComponent where the width/height is a % of the frame's size
(or perhaps % of label's container's size, if it also increases/decreases along with frame)
 
Rob Camick
Ranch Hand
Posts: 2613
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out Background Panel for an example that dynamically resizes the image as the size of the panel changes.
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems like this is harder than I thought. Never mind, I will give a try, thanks to all of you for the replies
 
Rob Camick
Ranch Hand
Posts: 2613
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its not that hard. Its one line of code to draw an image at the size of the container.

The code I gave you does lots of other stuff as well that you don't need.
 
Randall Twede
Ranch Hand
Posts: 4467
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i had the opposite question. window resize based on image size. but i will save it for another post.
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will have a look now
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randall Twede wrote:i had the opposite question. window resize based on image size. but i will save it for another post.

Since the answer is brief, I'll provide it here (sorry Yohan!). In the (J)component that paints the image, override getPreferredSize to return the size of the image. Then call pack() on the window it's shown in.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic