I am creating a 2D Java game, which looks like this:
My question is what should I do to my display to create two 'separate' displays. I plan on doubling the height of the program, and have a second display which is able to render completely separately to the top display. I'd like it to look like this:
I would like to not have to click between the two displays to be able for them to recognize input. I think this could be done by just keeping one canvas and just commanding the canvas to draw in certain areas? I also am not planning on using any external libraries.
Game class: (Some irrelevant code removed)
public class Game implements Runnable {
private Display display;
private int width, height;
public String title;
private Thread thread;
private boolean running = false;
private BufferStrategy bs;
private Graphics g;
public State gameState;
public State menuState;
private Handler handler;
public Game(String title, int width, int height) {
this.width = width;
this.height = height;
this.title = title;
}
private void tick() {
// Ticks the current game state
if(StateManager.getState() != null) {
StateManager.getState().tick();
}
}
private void render() {
bs = display.getCanvas().getBufferStrategy();
if(bs == null) {
display.getCanvas().createBufferStrategy(3);
return;
}
g = bs.getDrawGraphics();
//Clear screen
g.clearRect(0, 0, width, height);
// Gets the current game state and runs its render method
if(StateManager.getState() != null) {
StateManager.getState().render(g);
}
// End drawing
bs.show();
g.dispose();
}
// Initialize the display, assets, and game states
public void init() {
display = new Display(title, width, height);
display.getFrame().addKeyListener(keyManager);
display.getFrame().addMouseListener(mouseManager);
display.getFrame().addMouseMotionListener(mouseManager);
display.getCanvas().addMouseListener(mouseManager);
display.getCanvas().addMouseMotionListener(mouseManager);
Assets.init();
handler = new Handler(this);
gameState = new GameState(handler);
menuState = new MenuState(handler);
StateManager.setState(gameState);
}
public void run() {
init();
// Some unimportant code is removed for sake of space
while (running) {
tick();
render();
}
stop();
}
}
Display class:
public class Display {
private JFrame frame;
private Canvas canvas;
private String title;
private int width, height;
public Display(String title, int width, int height) {
this.title = title;
this.width = width;
this.height = height;
createDisplay();
}
private void createDisplay() {
frame = new JFrame(title);
frame.setSize(width, height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
canvas = new Canvas();
canvas.setPreferredSize(new Dimension(width, height));
canvas.setMaximumSize(new Dimension(width, height));
canvas.setMinimumSize(new Dimension(width, height));
canvas.setFocusable(false);
frame.add(canvas);
frame.pack();
}
public Canvas getCanvas() {
return canvas;
}
public JFrame getFrame() {
return frame;
}
}
Launcher class:
public class Launcher {
private final static String TITLE = "Game";
private final static int WIDTH = 256;
private final static int HEIGHT = 192;
public static void main(String[] args) {
Game game = new Game(TITLE, WIDTH, HEIGHT);
game.start();
}
}

