0

I just finished making my first game in Java and want to put my game into fullscreen while keeping the game to scale. I'm using Swing and running the game through a JFrame.

How would I go about doing this?

package com.game.main;

import java.awt.Canvas;
import java.awt.Dimension;
import java.io.Serial;

import javax.swing.JFrame;

public class Window extends Canvas{

@Serial
private static final long serialVersionUID = -1478604005915452565L;

public Window(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.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    frame.add(game);
    frame.setVisible(true);
    game.start();
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    https://stackoverflow.com/questions/11570356/jframe-in-full-screen-java might give you a hint about the screen. However, I think tou are also asking about how to scale the actual content to fit the screen. Can you clarify? – Just another Java programmer Dec 20 '21 at 10:23
  • [This](https://docs.oracle.com/javase/tutorial/extra/fullscreen/index.html) may also help. – gthanop Dec 20 '21 at 10:24
  • 1
    What exactly is your question? You posted a bit of code, and told us what you want ... but what exactly isn't working for you? – GhostCat Dec 20 '21 at 10:31
  • This problem is solved with [Layout Managers](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html). I suggest you read about them and choose the one that will work for your case. – hfontanez Dec 25 '21 at 07:34

0 Answers0