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.
Components should be added to the frame BEFORE the frame is made visible.
Game
class, plus extra space for the title bar and borders of the frame.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: