JLabel + images - java

I'm applying an image to a JLabel using the command .setIcon() and then adding the label to a panel
public browser() throws IOException {
JLabel lblimg;
Image img;
ImageIcon ico;
img = ImageIO.read(new File("<FilePath>"));
ico = new ImageIcon(img);
lblimg.setIcon(ico);
lblimg.setBounds(300,90,120,120);
add(lblimg);
}
but this doesn't resize the image inside the label, this way just a slice of the image will appear if it is bigger than the label size.
Does anyone knows a method to insert an image to a label background, resizing the image into it?

Create a BufferedImage and get scaled instance, set its width and height to the width and height to that of the label. Now if you even resize the label, the image will cover the label.
For Example: lblimg.setBounds(300, 90, 300, 120);
BufferedImage bimg =ImageIO.read(new File("file path"));
ico = new ImageIcon(bimg.getScaledInstance(lblimg.getWidth(), lblimg.getHeight(), Image.SCALE_SMOOTH));
lblimg.setIcon(ico);
The following image has been scaled according to the label's width and height.
lblimg.setBounds(300, 90, 100, 50);

Here's a method I wrote some years back to resize an image in Java.
public ImageIcon picturePrep(ImageIcon icon) {
final int DESIRED_WIDTH = 880;
double imageWidth = icon.getIconWidth();
int imageHeight = icon.getIconHeight();
imageWidth = DESIRED_WIDTH/imageWidth;
imageHeight = (int) (imageWidth * imageHeight);
Image img = icon.getImage();
return new ImageIcon(img.getScaledInstance(DESIRED_WIDTH, imageHeight, Image.SCALE_SMOOTH));
}

Use a appropriate layout manager to take care it for you automatically. See Laying Out Components Within a Container for more details.
If, for some bizarre reason, you can't use a layout manager, then you should probably be considering a solution based around custom painting, you could make use of the components preferred size to provide it better information when settings it's bounds
BufferedImage img = ImageIO.read(new File("<FilePath>"));
JLabel lblimg = new JLabel(new ImageIcon(img));
lblimg.setBounds(new Rectangle(new Point(300, 90), lblimg.getPreferredSize()));
Then, you don't need to make guesses

Related

How do I resize an image in a label that's selected in a classpath resource?

This is the code
btnVoltar.setIcon(new ImageIcon(AdicionarRefeiĆ§Ć£o1.class.getResource("/icons/Back Icon.png")));
I want to resize it so it fits on a label, but its way too big.
You can try this codes here, but then again this post is a duplicate from stackoverflow
private ImageIcon image; // where in your case it's your class resource
Image IMG = image.getImage(); // transform it
Image newimg = IMG.getScaledInstance(200, 200, java.awt.Image.SCALE_SMOOTH); // scale it the smooth way
//Change the 200,200 to the number you prefer to resize it to
image = new ImageIcon(newimg); // transform it back

Java - how to auto set width for img.getScaledInstance( );?

I wanted to resize a image if it is too large, but i want it to keep it's aspect ratio, how can i just define it's height and let it automatically get it's width?
ImageIcon image2 = new ImageIcon(image);
//Check if image size is more than 200
if(!checking){
Image img = image2.getImage() ;
Image newimg = img.getScaledInstance( "What to put here?", 200, java.awt.Image.SCALE_SMOOTH ) ;
image2 = new ImageIcon( newimg );
}
JButton newimage = new JButton(image2);
Copied from Javadocs,
If either width or height is a negative number then a value is substituted to maintain the aspect ratio of the original image dimensions. If both width and height are negative, then the original image dimensions are used.
So you could just do,
img.getScaledInstance(-1, 200, java.awt.Image.SCALE_SMOOTH ) ;
you should do some maths.find the ratio of height and old height then find new width
double Width=(200/(double)image2.getIconHeight())*image2.getIconWidth();
then you can set width
Image newimg = img.getScaledInstance( Width, 200, java.awt.Image.SCALE_SMOOTH ) ;

How resize javax.swing.Icon

I try to resize IUManager icon. But i cant do it correct. My code looks like:
// label
ErrorDetails = new javax.swing.JLabel();
// icon
Icon icon = UIManager.getIcon("OptionPane.informationIcon");
BufferedImage bi = new BufferedImage(
205,
250,
BufferedImage.SCALE_DEFAULT);
Graphics2D g = bi.createGraphics();
g.scale(205,205);
// paint new graphics
icon.paintIcon(null,g,250,250);
g.dispose();
// set resized UIManage icon
ErrorDetails.setIcon(icon);
but icon have still same size
You are attempting to paint the Icon onto the BufferedImage. Therefore you would need to create a new Icon using the BufferedImage>
ImageIcon scaled = new ImageIcon(bi);
ErrorDetails.setIcon(scaled);
Also follow Java naming conventions. Variable names should NOT start with an upper case character. "ErrorDetails" should be "errorDetails".

Adapting pixels of the image depending files and rows

I have this function where I receive String[][], f (rows), and c (columns). I've got to adapt my labels occupying all of my frame (1270,750).
For example, if I receive:
f=3 and c=3, the images will have: (423.333,250.) pixels
f=10 and c=10, the images will have (127,75) pixels.
I've proved with all the functions which are in the example, but it doesn't work. Any idea how to do it?
public void inicialitzamatriu(String[][] arraystrings,int f,int c) {
ff=f;
cc=c;
compsToExperiment=new JPanel();
GridLayout experimentLayout = new GridLayout(ff,cc);
compsToExperiment.setLayout(experimentLayout);
this.setContentPane(compsToExperiment);
for (int filas=0;filas<ff;filas++){
for (int columnas=0;columnas<cc;columnas++){
if (arraystrings[filas][columnas].equals("gat")){
JLabel cat2 = new JLabel();
cat2.resize(1270/cc, 750/cc);
cat2.setIcon(new ImageIcon("cat.png"));
compsToExperiment.add(cat2);
/*cat2.getSize();
//cat2.getWidth();
//cat2.getHeight();
cat2.resize(width, height);
cat2.setBounds(x, y, width, height)
cat2.setSize(width, height)
cat2.
*/
}
if (arraystrings[filas][columnas].equals("rat")){
JLabel rat2 = new JLabel();
rat2.setSize(1270/cc, 750/cc);
rat2.setIcon(new ImageIcon("raton.png"));
compsToExperiment.add(rat2);
}
if (arraystrings[filas][columnas].equals("menjar")){
JLabel comida2 = new JLabel();
comida2.setSize(1270/cc, 750/cc);
comida2.setIcon(new ImageIcon("comida.png"));
compsToExperiment.add(comida2);
}
if (arraystrings[filas][columnas].equals("res")){
JLabel nada2 = new JLabel();
nada2.setSize(1270/cc, 750/cc);
nada2.setIcon(new ImageIcon("nada.png"));
compsToExperiment.add(nada2);
}
}
}
this.repaint();
//compsToExperiment.repaint();
compsToExperiment.setVisible(true);
//this.setVisible(true);
//this.setVisible(false);
}
You need to do custom painting to scale the image to the space available in the grid.
You can try using Darryl's Stretch Icon (or maybe the Shrink Icon).

ImageIcon + ImageIcon = ImageIcon

I have two ImageIcons and I want to create a third ImageIcon which has nr 2 drawn upon nr 1.
How would I best do that?
The following code takes an Image from two ImageIcons and creates a new ImageIcon.
The image from the second ImageIcon is drawn on top of the image from the first, then the resulting image is used to make a new ImageIcon:
Image img1 = imageIcon1.getImage();
Image img2 = imageIcon2.getImage();
BufferedImage resultImage = new BufferedImage(
img1.getWidth(null), img1.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = resultImage.createGraphics();
g.drawImage(img1, 0, 0, null);
g.drawImage(img2, 0, 0, null);
g.dispose();
ImageIcon resultImageIcon = new ImageIcon(resultImage);
Edit
(Fixed some errors, added transparency support.)
For allowing transparency, the BufferedImage.TYPE_INT_ARGB can be used for the image type in the constructor, rather than BufferedImage.TYPE_INT_RGB which does not have an alpha channel.

Categories

Resources