Making my first attempt at a 2D two-player game in Java. The issue I am running into is I am trying to split the window in half so two people can play the game against each other. I have successfully been able to have the screen split using GridLayout, but the second half is all white. Am I not able to use add.frame(game);
twice to display the game twice? Otherwise, the window displays fine when it is not split and I was able to implement a camera object to track player 1. I have provided both Game() and Window() for reference.
public class Game extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;
private boolean isRunning = false;
private Thread thread;
private Handler handler;
private Camera camera;
private SpriteSheet ss;
private BufferedImage level = null;
private BufferedImage sprite_sheet = null;
private BufferedImage floor = null;
public Game() {
handler = new Handler();
new Window(1000,563, "GameObjects.Tank Wars", this);
start();
camera = new Camera(0,0);
this.addKeyListener(new KeyInput(handler));
BufferedImageLoader loader = new BufferedImageLoader();
level = loader.loadImage("/Tank_Level.png");
sprite_sheet = loader.loadImage("/sprite_sheet.png");
ss = new SpriteSheet(sprite_sheet);
floor = ss.grabImage(4,2,32,32);
loadLevel(level);
}
}
public class Window {
public Window(int width, int height, String title, Game game) {
JFrame frame = new JFrame(title);
// size of frame
frame.setSize(new Dimension(width, height));
GridLayout layout = new GridLayout(2,1);
frame.setLayout(layout);
frame.add(game); //add in game class to frame
frame.add(game);
frame.setResizable(false); //cannot resize window
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null); //when game started box will start center of window
frame.setVisible(true); //lets us see window
}
}