0

I have made a Board class, where I import an image using ImageIcon and, instead of making a picture with the specified resolution, I want it to be full screen for any type of monitor.

public Board() {
        p = new Dude();
        addKeyListener(new AL());
        setFocusable(true);
        ImageIcon i = new ImageIcon("C:/test.jpg");

        img = i.getImage();
        time = new Timer(5, this);
        time.start();
    }

I've used this code before:

public class MainMenu2 {
    MainMenu2() throws IOException{
        JFrame Main_Menu = new JFrame("Main Menu");
        Main_Menu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        final int widthScreen = screenSize.width;
        final int heightScreen = screenSize.height;

        BufferedImage backgroundImage = ImageIO.read(new File("P://My Pictures//background1.jpg"));
        JLabel background = new JLabel(new ImageIcon(backgroundImage));
        Image scaleBackground = backgroundImage.getScaledInstance(widthScreen, heightScreen, Image.SCALE_SMOOTH);
        ImageIcon imageIcon = new ImageIcon(scaleBackground);
        Main_Menu.setContentPane(new JLabel(imageIcon));

which uses scale smooth, which sets the image to the screen width and height, but I don't know how to apply it to my current code. The difference between both is that, Board class extends JPanel, and MainMenu2 is just a normal class, where a JFrame is created, etc. Please help! Thank you.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
DennyS
  • 1
  • 1
  • You can use the [Stretch Icon](https://tips4java.wordpress.com/2012/03/31/stretch-icon/). It will dynamically adjust its size based on the space available. – camickr Feb 02 '20 at 03:34

1 Answers1

1

At the most basic level, it might look some like this...

ImageIcon i = ImageIO.read(new File("C:/test.jpg"));
Image scaleBackground = backgroundImage.getScaledInstance(widthScreen, heightScreen, Image.SCALE_SMOOTH);            
img = scaleBackground.getImage();

Now, problems - the size of the component isn't likely to match he size of the screen.

As a preference, I might be tempted to do something like Scale the ImageIcon automatically to label size

You should also read The Perils of Image.getScaledInstance()

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • in my code, I don't use scaleBackground so why would I apply it? scaleBackground is used in another code example which doesn't apply to my current one, how would i set an iage to full screen when class extends JPanel? – DennyS Feb 02 '20 at 13:45
  • @DennyS First - I took both examples and merged them - so you could, if you tried, apply it directly to you `JPanel` example. Second, you could also look at the [fully runnable example](https://stackoverflow.com/questions/14548808/scale-the-imageicon-automatically-to-label-size/14553003#14553003) which was also linked in my answer. At some point, you're going to take these examples and apply it to your own workflow – MadProgrammer Feb 02 '20 at 21:11