Hello all. I’ve started working on my game but have not had the foresight to consider the game to be played at full screen. I’m using Canvas and JFrame.
Basically I would like to make the game true full screen mode and have my graphics inside the frame stretch with it. Is it doable with my current setup of using Canvas and JFrame? Or must I overhaul it.
these are my Main class and Window Class.
package com.hakired.onepiecegame.window;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import com.hakired.onepiecegame.framework.KeyInput;
public class Game extends Canvas implements Runnable {
private boolean running = false;
private Thread thread;
public static int WIDTH, HEIGHT;
// Object;
Handler handler;
Camera cam;
public synchronized void start() {
if (running)
return;
running = true;
thread = new Thread(this);
thread.start();
}
public void init() {
WIDTH = getWidth();
HEIGHT = getHeight();
handler = new Handler();
cam = new Camera(0, 0); // Camera
this.addKeyListener(new KeyInput(handler));
handler.init();
}
public void run() {
this.requestFocus();
init();
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int updates = 0;
int frames = 0;
while (running) {
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while (delta >= 1) {
tick();
updates++;
delta--;
}
render();
frames++;
if (System.currentTimeMillis() - timer > 1000) {
timer += 1000;
System.out.println("FPS: " + frames + " TICKS: " + updates);
frames = 0;
updates = 0;
}
}
}
private void tick() {
handler.tick();
}
private void render() {
BufferStrategy bs = this.getBufferStrategy();
if (bs == null) {
this.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
// ////////////////////////////////
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
handler.render(g);
// //////////////////////////////
g.dispose();
bs.show();
}
public static void main(String args[]) {
new Window(800, 600, "One Piece Game", new Game());
}
}
Window Class
package com.hakired.onepiecegame.window;
import java.awt.Dimension;
import java.awt.GraphicsDevice;
import javax.swing.JFrame;
public class Window {
public int w;
public int h;
public Window(int width, int height, String title, Game game){
w = width;
h = height;
game.setPreferredSize(new Dimension(width, height));
game.setMaximumSize(new Dimension(width, height));
game.setMinimumSize(new Dimension(width, height));
JFrame frame = new JFrame("title");
frame.add(game);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
//frame.setBounds(0, 0, width, height);
game.start();
}
}