• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to stretch icon to fit on JToggleButton

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22783
131
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
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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
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
If you want to work with Icons, then ImageIcon is your friend. And guess what - you can create them with Images!
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Start looking here.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
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
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic