1
\$\begingroup\$

I am creating a 2D Java game, which looks like this:

enter image description here

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:

desiredDisplay

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

}
\$\endgroup\$
2
  • \$\begingroup\$ is it supposed to have interactions between these two "displays" (maybe if you perform an actions where you want to drag an "item" from the top "display" down to the lower "display"?) \$\endgroup\$ Commented Jul 13, 2018 at 5:48
  • \$\begingroup\$ It does not need interactions between the "displays". The problem I run into if I create two different JFrames and Canvas' is the ability to focus on both "displays" at the same time, without having to click between them. \$\endgroup\$
    – Fusha
    Commented Jul 14, 2018 at 23:35

1 Answer 1

0
\$\begingroup\$

you can create a Component that does the layout for you.

private void createDisplay() {
    frame = new JFrame(title);

    canvas = new Canvas();
    Canvas bottomElement = ...;//code your bottem element here

    JPanel layoutComponent = new JPanel(); //your component that does layout
    layoutComponent.setLayout(new BorderLayout());
    layoutComponent.add(canvas, BorderLayout.CENTER);
    layoutComponent.add(bottomElement , BorderLayout.SOUTH);

    frame.add(layoutComponent);//add layout component now (instead of canvas)
    frame.pack();
}

maybe JPanel and Borderlayout are not the Components/Layouts of your choice... you can exchange them easily

for your listeners...
...you can let the listeners as they are but you have to handle the input, but you have to ask where the mouse event came from, see ActionEvent.getSource to determine the specific source...

\$\endgroup\$
2
  • \$\begingroup\$ Works now! For my code implementation of this to work (instead of a JFrame with no dimensions), I also needed to set preferred, maximum, and minimum sizes for the layoutComponent, canvas, and bottomCanvas. \$\endgroup\$
    – Fusha
    Commented Jul 16, 2018 at 20:32
  • \$\begingroup\$ @collin1158 of course you had to =) i'm glad you could do it without further help! keep up the good coding! \$\endgroup\$ Commented Jul 17, 2018 at 4:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .