aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Painting an image on a button Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Painting an image on a button" Watch "Painting an image on a button" New topic
Author

Painting an image on a button

Andy Joness
Greenhorn

Joined: Sep 15, 2009
Posts: 24
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
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Can you show us some of your code?
Andy Joness
Greenhorn

Joined: Sep 15, 2009
Posts: 24
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
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

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

Joined: Sep 15, 2009
Posts: 24
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
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

I'm sorry, I can't replicate your problem. Please post an SSCCE demonstrating what you mean, please include the image you are using.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4640
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Painting an image on a button