0

Say I've got an image in a JLabel. Here's how I'm currently adding it:

p=resizeImage(p,lbl.getWidth(),lbl.getHeight());
lbl.setIcon(p);

I'd like to resize this as the label is resized.

  • How can I accomplish this?
  • how can I resize while maintaining proportion?

I could write a small function to get the image width to height ratio, get the label width and height, find the smaller of the two(proportionally) and calculate the new height and width manually. That would not be an issue, but I wonder if there's a cleaner way.

apomene
  • 14,282
  • 9
  • 46
  • 72
silentwinter
  • 107
  • 2
  • 12

1 Answers1

1

You could listen to resize-events of the label or, what I would prefer, paint the image directly (replacing the JLabel with JComponent).

Mot
  • 28,248
  • 23
  • 84
  • 121
  • That worked perfectly. I used the resize events approach: lbl.addComponentListener(new ComponentListener(){ public void componentResized(ComponentEvent evt) { resize();} Now just have to edit my resize method to make it maintain proportions – silentwinter Apr 13 '13 at 13:52
  • @Zima: Some examples or proportional scaling are shown [here](http://stackoverflow.com/q/15961412/230513). – trashgod Apr 13 '13 at 14:15