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);
Related
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);
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 ) );
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.
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));
I'm new to java.
I want to make my image to act as the button in my project. Like the windows phone tile. My frame background is black. And I arranged 6 buttons. I also added an image but it comes with some white spaces around the image as borders. Here is the code i used:
ImageIcon image = new ImageIcon(getClass().getResource("yellowimage.png");
JButton button = new JButton(image);
I dont want those empty spaces. I just want the button to be exactly the image and wanted to type "click me" over the button. How can I do this ?
Try this:
JButton button = new JButton(image);
button.setContentAreaFilled(false);
button.setBorder(null);
You can also change the button image when it is pressed with:
button.setPressedIcon(pressedIcon);
You can try this to make JButton with an image.
Icon yourIcon = new ImageIcon("yourFile.gif");
JButton button2 = new JButton(yourIcon);
You can minimize the border around the image with setMargin();
ImageIcon image = new ImageIcon(getClass().getResource("yellowimage.png");
JButton button = new JButton(image);
button.setMargin(new Insets(1, 1, 1, 1));
Try this..
JButton button = new JButton();
ImageIcon icon = new ImageIcon(
ClassName.class.getResource("image.png"));
button.setIcon(icon);
button.setBackground(new Color(0,0,0,0));
button.setBorderPainted(false);