2

So I was following a tutorial on Java on Youtube. However, the "teacher" was only dimensioning his JFrame to around 400x400px. I would like mine to be fullscreen. So I tried fooling around with my code (did not just wanna do 1920x1280 in dimensions, since I would like it to be viable on most resolutions).

However, as I tried changing numbers I kind of just went maniac with "JFrame.MAXIMIZED_BOTH", since I was convinced that would solve all my trouble. Guess what, it didn't. Since I changed a lot and I am not entirely sure how to get back to the point where things work, I am showing you my full main class code. Mainly contains drawing, loop and buffering:

import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;


public class Game extends Canvas implements Runnable{
    private static final long serialVersionUID = 1L;
    public static final int WIDTH = JFrame.MAXIMIZED_BOTH, HEIGHT = JFrame.MAXIMIZED_BOTH, SCALE = 3;
    public static boolean running = false;
    public Thread gameThread;

    private BufferedImage spriteSheet;

    private Player player;

    public void init(){
        ImageLoader loader = new ImageLoader();
        spriteSheet = loader.load("/Grass_road.png");

        SpriteSheet ss = new SpriteSheet(spriteSheet);

        player = new Player(0, 0, ss);
    }

    public synchronized void start(){
        if(running)return;
        running = true;
        gameThread = new Thread(this);
        gameThread.start();
    }
    public synchronized void stop(){
        if(!running)return;
        running = false;
        try {
            gameThread.join();
        } catch (InterruptedException e) {e.printStackTrace();}
    }

    public void run(){
        init();
        long lastTime = System.nanoTime();
        final double amountOfTicks = 60D;
        double ns = 1000000000 / amountOfTicks;
        double delta = 0;

        while(running){
            long now = System.nanoTime();
            delta += (now - lastTime) / ns;
            lastTime = now;
            if(delta >= 1){
                tick();
                delta--;
            }
            render();
        }
        stop();
    }
    public void tick(){
        player.tick();
    }
    public void render(){
        BufferStrategy bs = this.getBufferStrategy();
        if(bs == null){
            createBufferStrategy(3);
            return;
        }
        Graphics g = bs.getDrawGraphics();
        //RENDER HERE
        g.fillRect(0, 0, JFrame.MAXIMIZED_BOTH, JFrame.MAXIMIZED_BOTH);

        player.render(g);

        //END RENDER
        g.dispose();
        bs.show();
    }

    public static void main(String[] args){
        Game game = new Game();
        game.setPreferredSize(new Dimension(WIDTH, HEIGHT));
        game.setMaximumSize(new Dimension(WIDTH, HEIGHT));
        game.setMinimumSize(new Dimension(WIDTH, HEIGHT));

        JFrame frame = new JFrame("Battle Beasts");
        frame.setSize(WIDTH, HEIGHT);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(true);
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.setUndecorated(true);
        frame.add(game);
        frame.setVisible(true);

        game.start();
    }

I did succeed in getting fullscreen and removing border aswell. As it is now I have an extremely small box in the corner, just a few pixels. Rest is flickering as never before.

Bottom line, my screen is flickering, since what I've drawn from the line "g.fillRect(0, 0, JFrame.MAXIMIZED_BOTH, JFrame.MAXIMIZED_BOTH);", now only fills a very small amount of the screen. How do I make it fill a fullscreen on all resolutions?

Edit: Steps for Andrew;

JFrame frame = new JFrame("Battle Beasts");
        frame.add(game);
        frame.pack();
        frame.setSize(WIDTH, HEIGHT);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(true);
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.setUndecorated(true);
        frame.setVisible(true);

        game.start();
Jon Dier
  • 157
  • 1
  • 3
  • 17
  • @AndrewThompson what an usefull comment. I am transfering from C# to Java(I am a newbie at both). And I follow a tutorial which have fine parallels with a project of mine and the guy helps a lot telling the differences. :) – Jon Dier Dec 01 '14 at 00:41
  • You could actually read an appropropriate tutorial, like [Full-Screen Exclusive Mode API](https://docs.oracle.com/javase/tutorial/extra/fullscreen/) – MadProgrammer Dec 01 '14 at 00:45
  • 1
    *"the guy helps a lot telling the differences."* I wouldn't 1) Use a `Canvas` as a rendering surface. (Instead use `JPanel` and override `paintComponent()`.) 2) Use a `Thread` to animate it. (Instead use the Swing `Timer`) 3) Use a `BufferStrategy` (unnecessary with Swing panels, which are double buffered). – Andrew Thompson Dec 01 '14 at 00:46

1 Answers1

2

How do I make it fill a fullscreen on all resolutions?

First, get rid of these lines:

    game.setPreferredSize(new Dimension(WIDTH, HEIGHT));
    game.setMaximumSize(new Dimension(WIDTH, HEIGHT));
    game.setMinimumSize(new Dimension(WIDTH, HEIGHT));

See Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? (Yes.) The game should override getPreferredSize() to return an ideal value, then deal with whatever it is given.

Then change:

    JFrame frame = new JFrame("Battle Beasts");
    // ..
    frame.add(game);

To:

    JFrame frame = new JFrame("Battle Beasts");
    frame.add(game); // add this early on, then..
    frame.pack(); // immediately call pack()
    // ..
Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Tried changing what you posted, getting following error when trying to debug: Exception in thread "main" java.awt.IllegalComponentStateException: The frame is displayable. at java.awt.Frame.setUndecorated(Unknown Source) at Game.main(Game.java:91) ** Not error but console says, and game doesn't run – Jon Dier Dec 01 '14 at 00:53
  • Show the steps between `JFrame frame = new JFrame("Battle Beasts");` & `game.start();` (as an edit to the question). Don't put code or exception output in comments where it is unreadable. – Andrew Thompson Dec 01 '14 at 00:55
  • @AndrewThomson side comment, do you recommend Java books? – Jon Dier Dec 01 '14 at 01:19