0

I am trying to give my interface a new function, but I have encountered some obstacles. I want to enlarge image on JLabel when mouseEnters. Here is how my JLabels looks:

int sacle = 50 //Size of my JLabel Icon
int zoom = 10 // How much the icon should enlarge
imageIcon = new ImageIcon(new ImageIcon(myClass.class.getResource(Picture))
           .getImage().getScaledInstance(scale, scale, Image.SCALE_SMOOTH));
JLabel stackIsGreat = new JLabel();
stackIsGreat.setIcon(imageIcon);
//and I add multiple of such JLabels`

And the code goes on and on. I wanted to creat a function and add it to mouseListener, so all will behave the same. I wanted to achive that with:

//inside external method
activeLabel = (javax.swing.JLabel)(e.getSource());
ImageIcon temp = (ImageIcon) activeLabel.getIcon();

But there is no way I know I could get use of this, because java says I need Image to create my enlarged ImageIcon

ImageIcon enlarged = new ImageIcon((Image).getScaledInstance(scale + zoom, scale + zoom, Image.SCALE_SMOOTH))

How can I retrive the image used to crate the JLabel from code. Any help would be appreciated.

J. D.
  • 3
  • 2
  • It might be the answer, but there may be better way `Image image = ((ImageIcon) activeLabel.getIcon()).getImage(); ImageIcon imageIconTemp = new ImageIcon( image.getScaledInstance( sizeW + zoom, sizeH + zoom, Image.SCALE_SMOOTH));` – J. D. Sep 16 '16 at 18:59

1 Answers1

2

I want to enlarge image on JLabel when mouseEnters.

Instead of creating your own MouseListener you could use a JButton to give you the rollover effect:

Something like:

JButton button = new JButton(...);
button.setBorderPainted( false );
ImageIcon icon = (ImageIcon)button.getIcon();
Image image = icon.getImage();
Image scaled = image.getScaledImage(...);
button.setRolloverIcon( new ImageIcon( scaled ) );
camickr
  • 321,443
  • 19
  • 166
  • 288
  • Does work great on button. (the rollover metod gets rid of multiple scaling issuses) I just had to change it from .getScaledImage(...) to .getScaledInstance(). Too bad it still shows as button, maybe because of Look and Feel. I could not set it to be indistinguishable from JPanel in its background. – J. D. Sep 16 '16 at 19:33
  • *"I could not set it to be indistinguishable from JPanel in its background."* It just requires some tweaking of the button. See [this answer](http://stackoverflow.com/a/10862262/418556) for 'invisible' buttons. – Andrew Thompson Sep 17 '16 at 00:27