How does one resize an image that will be placed in a JLabel?
ImageIcon icon = ImageIcon("img.gif");
JLabel label = new JLabel(icon);
Related
I am creating a profile page. At the left, I have added a jlabel containing a background image to my jpanel. Now over this panel, I want to add another jlabel for the icon. However, it is not showing up. Please help me.
P.s: This is only part of the code
public CustomerProfile()
{
super("Customer Profile");
setLayout(new BorderLayout());
//PROFILE DETAILS JPANEL
jpProfile = new JPanel();
add(jpProfile, BorderLayout.WEST);
//BACKGROUND IMAGE OF THE PROFILE JPANEL
ImageIcon background_img = new ImageIcon("background.jpg");
Image img = background_img.getImage();
Image tempImg = img.getScaledInstance(350, 600, Image.SCALE_SMOOTH);
background_img = new ImageIcon(tempImg);
background = new JLabel("",background_img,JLabel.CENTER);
jpProfile.add(background);
//PROFILE ICON
ImageIcon profImg = new ImageIcon("female.png");
jlProfileIcon = new JLabel();
jlProfileIcon.setIcon(profImg);
//ADDING PROFILE ICON TO JPANEL
jpProfileIcon = new JPanel();
jpProfileIcon.add(jlProfileIcon);
//jpProfile.setOpaque(false);
//jpProfileIcon.setOpaque(false);
jpProfileIcon.add(jlProfileIcon);
background.add(jpProfileIcon);
}
By default only a JPanel uses a layout manager.
The JLabel does not use a layout manager so the size/location of any component added to the label is not changed. The default size of a component is (0, 0) so there is nothing to paint.
Try:
background = new JLabel("",background_img,JLabel.CENTER);
background.setLayout( new BorderLayout() ); // added
…
background.add(jlProfileIcon);
The following code is not needed:
//jpProfileIcon = new JPanel();
//jpProfileIcon.add(jlProfileIcon);
That is you don't need to create a JPanel, just to add the label to it.
A better option is to paint the background image onto a panel and then you can just add your label normally to the panel. See: Background Panel for a class that implements this functionality.
Need help scaling an image in a JPanel of which you don't know the pathname.
So read the image from a JPanel and then scale the image to the JPanel size.
The code so far:
// Get the components inside the JPanel (subjpanels)
Component[] total_grid_fields = centerpanel.getComponents();
// Loop trough the JPanels
for (int i = 0; i < total_grid_fields.length-1; i++) {
// Check if it is a Jpanel
if(total_grid_fields[i] instanceof JPanel){
JPanel gridField = ((JPanel)total_grid_fields[0]);
// Get the width and height of the JPanel
int new_image_width = gridField.getWidth()-10;
int new_image_heigth = gridField.getHeight()-10;
// Get the components inside the JPanel
Component[] gridlabel = gridField.getComponents();
// Check if it is a JLabel
if(gridlabel[0] instanceof JLabel){
// Define a JLabel
JLabel newgridlabel = ((JLabel)gridlabel[0]);
// Get the placed ImageIcon
ImageIcon placeimage = (ImageIcon) newgridlabel.getIcon();
// Get the image
Image image = placeimage.getImage();
// Scale the image
Image newimg = image.getScaledInstance(new_image_width, new_image_heigth, java.awt.Image.SCALE_SMOOTH); // scale it the smooth way
// add new scaled image to ImageIcon
placeimage = new ImageIcon(newimg);
//add ImageIcon to Jlabel
img = new JLabel(placeimage);
// add Jlabel to Jpanel
newgridlabel.add(img);
// repaint the JFrame
frame.repaint();
}
}
}
Hope somebody can help...
My program captures the screen's images, resizes and represents them in a JoptionPane icon filed so that the user can decide weather or not to save them.
The prob is that after performing Joption.cancel, the icon shows the previous image the next time the user captures the screen image. in Joption.ok case it works fine.
Any idea why this happens? the snapshots themselves are well rendered(every snapshot captures the very current screen image but the icon shows the previous one once cancel has been clicked) .
rszedSnp=ImageMagick.resize(origSnp_name,30);
ImageIcon icon=new ImageIcon(rszedSnp);
String userIput = (String)JOptionPane.showInputDialog(
new JFrame(),
"Save as:\n",
"taking screen shot",
JOptionPane.PLAIN_MESSAGE,
icon ,
null,
origSnp_name);
I've resolved the problem by using JDialog, with an implemented JPanel containing a JLabel holding the image, instead of using an image icon in the JOptionPane#showInputDialog function. .
BufferedImage img = ImageIO.read(new File(image));
resize(img);
JLabel lbl = new JLabel(new ImageIcon(img));
lbl.setVisible(true);
JPanel imgPan = new JPanel();
imgPan.add(new JScrollPane(lbl))
JDialog dialog = new JDialog();
JPanel globalPan = new JPanel(new GridLayout(2,2));
globalPan.add(imgPan);
dialog.add( globalPan );
dialog.pack();
dialog.setVisible(true);
i have an URL image, and i want to display it in a panel.
How can i do it ?
One way to achieve this would be to use the URL class to grab the image from the web, create your ImageIcon object and then add it onto your JPanel,
Code is untested, but should demonstrate what you need to do.
URL img = new URL("http://www.example.com/whatever.jpg");
ImageIcon image = new ImageIcon(img);
JLabel label = new JLabel("", image, JLabel.CENTER);
JPanel panel = new JPanel(new BorderLayout());
panel.add( label, BorderLayout.CENTER );
What would be the most appropriate image type to display a jpg image (loaded from a local folder) on a JPanel?
Cheers.
ImageIcon image = new ImageIcon("image/pic1.jpg");
JLabel label = new JLabel("", image, JLabel.CENTER);
JPanel panel = new JPanel(new BorderLayout());
panel.add( label, BorderLayout.CENTER );
You could use a javax.swing.ImageIcon and add it to a JLabel using setIcon() method, then add the JLabel to the JPanel.
I'd probably use an ImageIcon and set it on a JLabel which I'd add to the JPanel.
Here's Sun's docs on the subject matter.
I would use a Canvas that I add to the JPanel, and draw the image on the Canvas.
But Canvas is a quite heavy object, sine it is from awt.
You could also use
ImageIcon background = new ImageIcon("Background/background.png");
JLabel label = new JLabel();
label.setBounds(0, 0, x, y);
label.setIcon(background);
JPanel panel = new JPanel();
panel.setLayout(null);
panel.add(label);
if your working with a absolut value as layout.