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

Painting an image on a button

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've created a class which paints an image on a button, working fine. However when I set the boundaries of the button so the size/position changes, the image gets stretched and basically looks messed up. I've tried calling repaint() but to no avail. Is there some way I can fix this? Cheers
 
Saloon Keeper
Posts: 15712
367
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you show us some of your code?
 
Andy Joness
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The class that paints the image


Then I set the bounds and try to repaint, but the image doesn't get repainted with the new dimensions.


 
Stephan van Hulst
Saloon Keeper
Posts: 15712
367
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm afraid you're contradicting yourself. In your first post you said the images stretches, which is what you would expect when the button resizes. In your last post you say the image doesn't get repainted with the new dimensions.

What's the problem, and how do you want it to behave?
 
Andy Joness
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want the image to scale according to the component size rather than become distorted.

E.g. I have an image 200x200px
If I create a new button that is 200x200 and paint the image, it looks fine.
If I create a new button that is 100x100 and paint the image, it looks fine and the image scales down.
If I create a new button that is 100x100, then resize that button using setBounds to 200x200, the image doesn't scale, it stretches and looks bad. I need a way of repainting that image on the resized button.
 
Stephan van Hulst
Saloon Keeper
Posts: 15712
367
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry, I can't replicate your problem. Please post an SSCCE demonstrating what you mean, please include the image you are using.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want the image to scale in a colinear fashion, you have to compute the new height and width.

By the way, there's absolutely no need to siubclass JButton and override paintComponent just to draw an image. You can set an Icon to a JButton instead. The icon's paintIcon can take care of the scaling, and if the size of the button is independent of its preferredSize, the icon can return 0 for its width and height.
 
Ruth Stout was famous for gardening naked. Just like this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic