-1

I have a got a JLabel.

JLabel background=new JLabel(new ImageIcon("C:/CraftBukkit/background.png"));

        add(background);

        background.setLayout(new FlowLayout());

The question is, how can I stretch/contract this image so that it is always the same size as the screen? Currently, it is either too big, too small, or not the right dimensions comparing to the screen size.

I have been unable to find any solutions on the internet.

Thanks!

mKorbel
  • 109,525
  • 20
  • 134
  • 319
user3731979
  • 73
  • 3
  • 11
  • Have a look at [this example](http://stackoverflow.com/questions/11959758/java-maintaining-aspect-ratio-of-jpanel-background-image/11959928#11959928) and [this example](http://stackoverflow.com/questions/14548808/scale-the-imageicon-automatically-to-label-size/14553003#14553003) – MadProgrammer Jun 13 '14 at 09:47
  • You want a to know how to resize the icon of a label so it meets the requirements of its container? First, JLabel doesn't resize the icon when it's size changes, second, to resize the image, you have a number of possible choices as listed on the two linked examples (and other examples I linked in the comments) – MadProgrammer Jun 13 '14 at 10:00

1 Answers1

1

First you need an undecorated JFrame; that is a JFrame without title bar, and make that maximized.

For the screen size without toolbar maybe you want to use the size of the first display attached.

In the JFrame constructor

setUndecorated(true);
setExtendedState(JFrame.MAXIMIZED_BOTH);
JPanel background = new JPanel();
JLabel label = new JLabel(...);
// maybe setHGap(0); gap between icon and text
panel.add(label);
add(panel);
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • 1
    JLabel doesn't resize the label to meet the requirements of the containe – MadProgrammer Jun 13 '14 at 09:45
  • While this is not exactly what I wanted, one of your lines of code indirectly solves the problem by eliminating the issue. Probably still looking for a more efficient solution, but this will do perfectly for now. Thanks! :) – user3731979 Jun 13 '14 at 10:00