Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reduce size of image

 
soumya kalla
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry i got the answer from somebody else's postings.

Thanks,
Soumya
 
soumya kalla
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just realized that the whole image is not being displayed using setPreferredSize(width,height). Please suggest something.

Soumya
 
Campbell Ritchie
Sheriff
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a link to the other thread

http://www.coderanch.com/t/343057/GUI/java/attaching-mouselistener-imagepanel
 
Campbell Ritchie
Sheriff
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic