3

I'm relatively new to Java, so I'm not even sure if this is possible, but I need to somehow get my JLabel to dynamically resize with my JFrame. It might be something as simple as a single line of code I'm missing, but frankly, I don't have the experience needed to solve this for myself.

I tried googling and browsing through other Stack posts, but they were either generally unhelpful or tried to get me to do something like

class ImagePanel extends JPanel 

or something along those lines --> which I can't do because I'm already extending my code with something else.

Hopefully I'm phrasing my question correctly as well...

Just in case, to clarify, I need my image to automatically resize itself as I resize the frame around it.

My code is below:

public static void ScalingImage() throws IOException
    {
        MyCollage GP = new MyCollage();  
        BufferedImage image = ImageIO.read(new File("myCollage_Final.jpg"));
        JLabel label = new JLabel(new ImageIcon(image));
        JFrame aFrame = new JFrame();
        int picwidth = (int)Math.round(aFrame.getWidth());
        int picheight = (int)Math.round(aFrame.getHeight());
        aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        aFrame.getContentPane().add(label);
        aFrame.pack();
        aFrame.setSize(2000, 2000);
        aFrame.add(GP);
        aFrame.setLocation(0, 0);
        aFrame.setVisible(true);
    }

So, if it's something obvious I'm missing, I'd love to know what it is. And if it's something not-so-obvious, then... I'd still love to know what it is haha.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
mitch.m
  • 31
  • 2
  • My first thought was [Using an appropriate layout manager](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html), but then I guessed you actually want to have the icon resized, which the `JLabel` doesn't do. You could have a look at this [example](http://stackoverflow.com/questions/25798156/resizing-icon-to-fit-on-jbutton-in-java/25798462#25798462) which does the same sort of thing for a `JButton` – MadProgrammer Dec 29 '15 at 08:20
  • Or you could look at [this example](http://stackoverflow.com/questions/14548808/scale-the-imageicon-automatically-to-label-size/14553003#14553003) which demonstrates a resizable component which scales it's associated image as the component is resized – MadProgrammer Dec 29 '15 at 08:22

2 Answers2

0

You can use the Stretch Icon.

The image will automatically be resized to fill the label. You can control whether the image is scaled proportionally or completely fills the area.

Using the StretchIcon is more flexible than doing custom painting as suggested in the other links as you can benefit from the automatic resizing on any component that supports an Icon.

camickr
  • 321,443
  • 19
  • 166
  • 288
0

"which I can't do because I'm already extending my code with something else."

You don't "extend code with"... you extend JPanel from a particular class. So it's perfectly possible to have one class which extends JPanel, and another class which continues to extend what you are already extending.

Alternatively, it may not actually be necessary to use inheritance to satisfy your other needs.

Also, the code you've posted isn't stylistically-correct Java, and doesn't exhibit any inheritance, which suggests to me that you've misunderstood something.

Robin Green
  • 32,079
  • 16
  • 104
  • 187