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();