aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Reduce size of image Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Reduce size of image" Watch "Reduce size of image" New topic
Author

Reduce size of image

soumya kalla
Greenhorn

Joined: Jun 05, 2006
Posts: 12
Hai,

I want to add a background image to my login panel. So i used a JLabel to do that, but I am not able to reduce the size of the image. Can anyone please suggest something. Here is my code

JPanel backpanel1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
JLabel picture1 = new JLabel();
picture1.setIcon(new ImageIcon(bulkLoadGUI.getClass().getClassLoader().getResource(
RESPATH + "image1.jpg")));
// picture1.setBounds(2,2,2,2);

// picture1.setsize(20,20);
backpanel1.add(picture1);


Please reply ASAP.

Thanks,
Soumya
soumya kalla
Greenhorn

Joined: Jun 05, 2006
Posts: 12
I am sorry i got the answer from somebody else's postings.

Thanks,
Soumya
soumya kalla
Greenhorn

Joined: Jun 05, 2006
Posts: 12
I just realized that the whole image is not being displayed using setPreferredSize(width,height). Please suggest something.

Soumya
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
So what was the answer? Please always ost a final post to any thread so other people can see your solution and use it for their own problems.
A link to the other thread would, of course, be a satisfactory alternative.
soumya kalla
Greenhorn

Joined: Jun 05, 2006
Posts: 12
I used setPreferredSize(with,height) but it did not work. The whole image is not being displayed using it. Here is my code

JPanel backpanel1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
JLabel picture1 = new JLabel();
picture1.setIcon(new ImageIcon(bulkLoadGUI.getClass().getClassLoader().getResource(
RESPATH + "image1.jpg")));
picture1.setPreferredSize(new Dimension(100,100));
// g.drawImage(img, 0, 0, this);

backpanel1.add(picture1);

I would really appreciate if you could suggest something.

Soumya
soumya kalla
Greenhorn

Joined: Jun 05, 2006
Posts: 12
Here is a link to the other thread

http://www.coderanch.com/t/343057/GUI/java/attaching-mouselistener-imagepanel
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
Thank you for posting fuller details. Unfortunately, I can never get images to work properly myself. Sorry.
I don't think cinderella SS got the scaling to work either.

There is a scale() method in the java.awt.Graphics class. You might be able to use that to fit your image, but the arithmetic can get a bit complicated if the panel is resized.

Anybody got any better ideas?
CR
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Java provides us with so many options that it's hard to say too much about the ways you can do something. Of course you can add an ImageIcon to a JLabel and add it to a JPanel; that's easy. But when you start talking about a background image thoughts naturally turn toward a graphics approach.
It's better to not use a separate component for the background image; you won't be able to add any other components on top of it. Draw your image on the component that will host your login components. Then you can use the component as you would any other: add components, set borders, etc.
Since you're adding components to your login panel you don't need to be concerned with the size of the image, the panels layout manager will calculate the preferred size from the components and layout constraints. Setting the preferred size of a graphic component is important when it has no child components (which the layout manager can query to determine the preferred size).
Here is a general approach to drawing a background image on a component with some options to scale or not scale.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reduce size of image