0

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
}

}

earandap
  • 1,446
  • 1
  • 13
  • 22
javaNoob
  • 23
  • 5
  • Would it be possible to draw both screens on a single window instead of having two windows? Could it be easier? Just suggesting – Barudar Oct 29 '19 at 07:00
  • 1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. [This answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). 3) See [Detection/fix for the hanging close bracket of a code block](http://meta.stackexchange.com/q/251795/155831) for a problem I could no longer be bothered fixing. .. – Andrew Thompson Oct 29 '19 at 13:46
  • 1
    .. Please [edit] the question to add 4 space chars ahead of the closing `}` at the end of the code. 4) `Window(int width, int height..` that is, at best, a guess. The `game` should return a sensible preferred size, and the window packed once it is added. 5) **But note that one component can appear exactly once in a Swing GUI. The approach I'd take here is to create two games and have them share a single model.** – Andrew Thompson Oct 29 '19 at 13:46
  • 1
    If you are in complete control of the drawing (for instance if you are overriding the paint or update methods of Canvas), you could draw everything on the same canvas. For instance by drawing the scene for player 1 on the left half and the scene for player 2 on the right half. Doing this could be easier if you don't need complex splitting. – Knokko Oct 29 '19 at 13:57
  • 1
    @AndrewThompson I appreciate your input and suggestions! I will look into your approach and see what I can come up with. – javaNoob Oct 29 '19 at 19:37

0 Answers0