-1

I have followed a handful of tutorials on how to fix this and none of them helped

What I am attempting to do is size the frame to 1280 by 720. But when I export my game to a jar the frame size is smaller then it should be, which leads to a variety of problems. Another confusing thing is that when I give the jar to others it works fine, and there are no problems for them. This is very confusing and super annoying.

My code:

package com.teto.main;
import java.awt.Canvas;
import java.awt.Cursor;
import java.awt.Dimension;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class FullFrame extends Canvas {
    private static final long serialVersionUID = 5486926782194361510L;
    Cursor csr = new Cursor(Cursor.CROSSHAIR_CURSOR);
    public FullFrame(int width, int height, String title, Game game) {
        JFrame frame = new JFrame(title);
        frame.setPreferredSize(new Dimension(width, height));
        frame.setMaximumSize(new Dimension(width, height));
        frame.setMinimumSize(new Dimension(width, height));
        frame.setSize(new Dimension(width, height));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        String imageName = "lgico.png";
        URL imageUrl = frame.getClass().getResource(imageName);

        if (imageUrl == null)
        {
            System.out.println("bruh");
            imageUrl = Thread.currentThread().getContextClassLoader().getResource(imageName);
        }

        ImageIcon icon = new ImageIcon( imageUrl );

        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setIconImage(icon.getImage());
        frame.setCursor(csr);
        frame.add(game);
        game.start();
        frame.pack();
    }
}

People have said that you should override getPreferredSize() but I don't know how I would do that and even if I try it doesn't do anything.

Ps. Yeah, I know my code is messy.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Zeeen
  • 312
  • 1
  • 2
  • 13

1 Answers1

0
frame.add(game);

Components should be added to the frame BEFORE the frame is made visible.

    frame.setPreferredSize(new Dimension(width, height));
    frame.setMaximumSize(new Dimension(width, height));
    frame.setMinimumSize(new Dimension(width, height));
    frame.setSize(new Dimension(width, height));
  1. The above code does nothing. The reason it does nothing is because you use frame.pack() later on in the code which will size the frame based on the preferred size of the components added to the frame. The frame size will therefore be the preferred size of your Game class, plus extra space for the title bar and borders of the frame.
  2. You should not attempt to be controlling the frame size by using those methods. You should NOT specify the size because you have no idea what the resolution of each monitor that plays your game will be.

A better way to set the size is to tell the frame to fill the screen.

So your code should be restructured to look something like:

// load the image
ImageIcon icon = new ImageIcon( imageUrl );

JFrame frame = new JFrame(title);
frame.add(game);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setCursor(csr);
frame.setIconImage(icon.getImage());
frame.setResizable(false);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH); // this maximizes the frame
frame.setVisible(true);
game.start();
camickr
  • 321,443
  • 19
  • 166
  • 288
  • didn't work but thankyou anyways. i think its just generally something wrong with my pc in general, because it resizes just not correctly, like if i put it too 500, 500 it will be close to that but slightly off. idk whats wrong but im sure I'll figure it out sometime soon – Zeeen Apr 22 '20 at 22:01
  • *ike if i put it too 500, 500* - no idea what you are talking about. You should NOT specify a size anywhere. Post your updated code demonstrating the problem. And your code doesn't even need the Game class. The first step is learning how to display the frame at its maximized state. Then you worry about adding components to the frame. – camickr Apr 22 '20 at 22:22