Razvi
Razvi

Reputation: 398

How to fit IMG size into JPanel

I'm kinda doing a GUI where when you press the "NEXT" button, it shows one by one the images at some directory.

My question is: How can I fit the size of the IMGs with the JPanles dimensions, I'm working with 6 or more MP images and I need to see the entire image.

Here is the code that gives me the imageIcon and where I add it to the JPanel.

JButton btnNextImg = new JButton("Next IMG");
    btnNextImg.setBounds(96, 179, 110, 23);
    btnNextImg.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (i == nImg)
                i = 0;
            try {
                selectedImage = ImageIO.read(new File("C:\\IMAGES\\"+ String.valueOf(i+1) + ".jpg"));
                iSelect = new ImageIcon(selectedImage);
                originalImage.setIcon(iSelect);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            i++;
        }
    });
    contentPane.add(btnNextImg);

And where I add it.

JPanel panel = new JPanel();
    originalImage = new JLabel();
    panel.add(originalImage);
    panel.setBounds(5, 226, 309, 280);
    contentPane.add(panel);

Thank you so much.

Upvotes: 0

Views: 207

Answers (2)

camickr
camickr

Reputation: 324118

Here is the code that gives me the imageIcon and where I add it to the JPanel.

Check out Darryl's Stretch Icon. It will allow the Icon to fill the entire space available to the JLabel.

Upvotes: 0

Moh-Aw
Moh-Aw

Reputation: 3018

here's an image panel i found somewhere on the internet when i had the same problem:

public class ImagePanel extends JPanel {

private java.awt.Image image;
private boolean stretched = true;
private int xCoordinate;
private int yCoordinate;

public ImagePanel() {

}

public ImagePanel(Image image) {
    this.image = image;
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    if (image != null) {
        if (isStretched()) {
            g.drawImage(image, xCoordinate, yCoordinate, getWidth(), getHeight(), this);
        } else {
            g.drawImage(image, xCoordinate, yCoordinate, this);
        }
    }
}

public java.awt.Image getImage() {
    return image;
}

public void setImage(java.awt.Image image) {
    this.image = image;
    repaint();
}

public boolean isStretched() {
    return stretched;
}

public void setStretched(boolean stretched) {
    this.stretched = stretched;
    repaint();
}

public int getXCoordinate() {
    return xCoordinate;
}

public void setXCoodinate(int xCoordinate) {
    this.xCoordinate = xCoordinate;
}

public int getYCoordinate() {
    return xCoordinate;
}

public void setYCoordinate(int yCoordinate) {
    this.yCoordinate = yCoordinate;
    repaint();
}
}

to add an image to the panel use something like this:

ImagePanel imagePanel = new ImagePanel();
BufferedImage image = ImageIO.read(new File("C:\\IMAGES\\"+ String.valueOf(i+1) + ".jpg"));
imagePanel.setImage(image);

Upvotes: 1

Related Questions