How to get Image (obj) from ImageIcon - java

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.

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 ) );

Related

How to set an offset to AWT Image or Swing ImageIcon?

I want to initialize an ImageIcon with an offset. The resizing is already working properly, but how can I tell the resized image icon to map to a certain x and y coordinate?
I have a map in my project and the attached snippet is the initialization of the image icon.
The image icon is then printed to a JLabel with a static size and position.
ImageIcon imageIcon = createImageIcon("/mapEnd1.png", "Map");
Image image = imageIcon.getImage();
Image newimg = image.getScaledInstance(600, 800, java.awt.Image.SCALE_SMOOTH);
imageIcon = new ImageIcon(newimg);

Fit size of an ImageIcon to a JButton [duplicate]

This question already has answers here:
Resizing icon to fit on JButton in Java?
(4 answers)
Closed 6 years ago.
Hi how can I fit the size of a ImageIcon to a JButton? I want to adjust the size of the ImageIcon to the size of the Button
JFrame frame2 = new JFrame("Tauler Joc");
JPanel panell = new JPanel();
ImageIcon icon = new ImageIcon("king.jpg");
JButton jb= new JButton(icon);
jb.setBounds(200,200,700,700);
panell.add(jb);
frame2.add(panell);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
You can do it this way by simply adding a little method to your project:
private static Icon resizeIcon(ImageIcon icon, int resizedWidth, int resizedHeight) {
Image img = icon.getImage();
Image resizedImage = img.getScaledInstance(resizedWidth, resizedHeight, java.awt.Image.SCALE_SMOOTH);
return new ImageIcon(resizedImage);
}
Now, to use this method in your example code:
JFrame frame2 = new JFrame("Tauler Joc");
JPanel panell = new JPanel();
ImageIcon icon = new ImageIcon("king.jpg");
JButton jb= new JButton();
jb.setBounds(200,200,700,700);
panell.add(jb);
// Set image to size of JButton...
int offset = jb.getInsets().left;
jb.setIcon(resizeIcon(icon, jb.getWidth() - offset, jb.getHeight() - offset));
frame2.add(panell);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
If you just want the image and no border just set the offset variable to 0 or get rid of the offset variable altogether.

How to set an ImageIcon to a JButton and resize the picture according to the button's size?

I've been wondering how can I set an ImageIcon to a button using the Image path and set it to a JButton?
I can resize the image's size according to the buttons size:
frontViewImageFile = fc.getSelectedFile();
MainFrame.btnFrontView.setIcon(new ImageIcon(ImageIO.read(
frontViewImageFile).getScaledInstance(150, 150, Image.SCALE_SMOOTH)));
But the image came from a file chooser and I can use getScaledInstance method to resize the picture.
How can i do this with image path since method getScaledInstance is undefined for the type String?
ImageIcon icon = ...;
JButton b = ...;
Image im = icon.getImage();
Image im2 = im.getScaledInstance(b.getWidth(), b.getHeight(), ...);
b.setIcon(new ImageIcon(im2));

How Do I Set An Icon For JLabel

I want to set an icon for a jlabel, can somebody give me example syntax
I have tried this :
JLabel icon = new JLabel();
ImageIcon chromo = createImageIcon("res/icon.png");
panel.add(icon);
icon.setIcon(chromo);
After I tried this the label didn't show up on the panel at all.
Try this:
JLabel icon = new JLabel();
ImageIcon chromo = createImageIcon("res/icon.png");
icon.setIcon(chromo);
panel.add(icon);
And don't forget to check your layout, if it is absolute then you need to add bounds to it.
icon.setBounds(startX, startY, width, height);

Java Swing: popup : display small icon in menu

I have a right click popup menu that works fine.
I now want to add a little image next to the text.
I managed to display the image in the popupmenu but its the full size version.
what I want to know is:
is there a way to automatically reize the icon to be the right size ( same as the text ) or do I have to resize the image?
Jason
You have to resize the Icon to be whatever size you want it to be. Something like:
ImageIcon icon = new ImageIcon( ... );
Image image = icon.getImage().getScaledInstance(64, 64, Image.SCALE_SMOOTH);
JLabel label = new JLabel( new ImageIcon( image ) );

Categories