Search code examples
javaimagenetbeansgraphicsderby

Stretching images in Java


I read some of the questions on SO, but I'm struggling to stretch images to fit a JLabel. I have a Derby DB which determines which image is used. The coding below is the catch code, but the others work on the same method.

I have the following coding: (experimentation)

    import java.awt.Image;
    import javax.swing.ImageIcon;
    import java.awt.Graphics;
    import java.awt.Toolkit;
    ...
//On Window Load, this happens
    Toolkit toolkit1 = Toolkit.getDefaultToolkit();
    Image image1 = toolkit1.getImage("unknown.png");
    Image scaledImage1 = image1.getScaledInstance(232, 177, Image.SCALE_DEFAULT);
    ImageIcon profilepic1 = new ImageIcon(scaledImage1);
    lblImg.setIcon(profilepic1);

The JFrame: Game Time! Prediction App

The JLabel is the one with the default text "Feature coming soon!" It has the dimensions of 232 x 177, and I want the images to stretch (smaller) to fit in to that JLabel.

Can anyone help me and explain what I'm doing wrong? Thanks!


Solution

  • You should be able to use Darryl's Stretch Icon.