• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to stretch icon to fit on JToggleButton

 
Rabbia Najam
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
i want to stretch my button's icon to fit it on the whole button when i increase the size of button through coding.
e.g if i have a button size 51,51 and an icon size 50,50.....it is exactly fit on the button.....but what if i change the size of the button 100,100 and wants that the image 50,50 stretch according to the new size of the button and exactly fit on it.
Plzzz help me
Thanks
Bye
 
Rob Spoor
Sheriff
Pie
Posts: 20546
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Image has a method called getScaledInstance. You can use that to scale your image. Resizing can slow your application though, so do it as sparsely as possible.

You can use a ComponentListener to find out when the button is resized.


Moving to Swing.
 
Rabbia Najam
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
but can you please show me a small example to scale an icon of a button.according to button size.
bye
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it would be far more beneficial to you, for you to have a shot at it yourself.

google the keyword
look for code snippets
try to understand what the snippets are doing
read the apidocs/tutorials for explanations

and if you can't get it working, post your attempt here and we'll try to
point you to the fix
 
Rob Spoor
Sheriff
Pie
Posts: 20546
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to work with Icons, then ImageIcon is your friend. And guess what - you can create them with Images!
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start looking here.
 
Maneesh Godbole
Saloon Keeper
Posts: 11064
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Prime:
Image has a method called getScaledInstance. You can use that to scale your image. Resizing can slow your application though, so do it as sparsely as possible.


Performance wise, how does this compare to over riding the Button's paintComponents(Graphics g) and drawing the Image?
 
Rob Spoor
Sheriff
Pie
Posts: 20546
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be honest, I have no idea. You would have to check that out with some tests.

Still, my guess is that stretching it through paintComponent will not be slower. It can sometimes use the graphics card for that, and even if it can't, it should still be just as fast / slow as scaling manually.

And of course, after you've scaled it, it needs to be drawn again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic