0

When I have just one window open, everything works fine. When I have two windows open, I notice that the repaint method is not called when I want it to. I checked using breakpoints.

public static void main(String[] args) {

    //this creates a first window, allowing user to choose ai/human and colour
    //then GameInterface calls startGame with those parameters
    GameInterface gui = new GameInterface();
    gui.setVisible(true);

    //startGame("ai", "white");
}

When I try calling just startGame without making the GameInterface first, it works all fine! Here is reduced code for GameInterface:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class GameInterface extends JFrame implements ActionListener {

    private JButton startGameButton;

    private JPanel panel = new JPanel();


    public test() {
        initUI();
    }

    private void initUI() {

        getContentPane().add(panel);

        panel.setLayout(null);

        /*
         * Make a start of game button
         */
        startGameButton = new JButton("Start game!");
        startGameButton.setBounds(50, 160, 200, 30);
        startGameButton.setEnabled(false);
        startGameButton.setActionCommand("startGame");
        startGameButton.addActionListener(this);

        panel.add(startGameButton);

        setTitle("Game");
        setSize(300, 400);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    };

    @Override
    public void actionPerformed(ActionEvent e) {
        if ("startGame".equals(e.getActionCommand())) {

            dispose();

            StartGame.startGame("ai", "white");
        }
    }

}

And here's the full StartGame class:

import java.util.ArrayList;

public class StartGame {

    static ArrayList<Piece> blackPieces = new ArrayList<Piece>();
    static ArrayList<Piece> whitePieces = new ArrayList<Piece>();

    public static void main(String[] args) {

            GameInterface gui = new GameInterface();
            gui.setVisible(true);

    //  startGame("ai", "white");
}

public static void startGame(String opponent, String colour) {

    //not using method parameters in this example


    //create a game instance
    ChessGame game = ChessGame.getInstance();

    //create players
    Player player1 = new ChessGui("white");
    Player player2 = new AI ("black");


    player1.setGame(game);
    player2.setGame(game);

    //AND HERE IS THE PROBLEM
    whitePieces = player1.choosePieces(); 


    blackPieces = player2.choosePieces();

    // at this point, players should add their own pieces
    player1.setOwnPieces(whitePieces);
    player1.setOpponentPieces(blackPieces);
    player2.setOwnPieces(blackPieces);
    player2.setOpponentPieces(whitePieces);

    // add pieces to the game based on players' choices
    game.initialisePiecesFromPlayers();

    new Thread(game).start();

}

And the ChessGui's important methods are:

public class ChessGui extends JComponent implements Player {

    @Override
    public void paintComponent(Graphics g) {

        // draw background
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, WINDOW_SIZE_X, WINDOW_SIZE_Y);

        g.drawImage(imgBackground, 0, 0, null);
    }

    public ChessGui(String colour) {

        this.colour = colour;

        // create graphical board
        try {
            imgBackground = createTileMap(ROWS, COLUMNS);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // create application frame and set visible
        JFrame frame = new JFrame("Chess");
        frame.setSize(WINDOW_SIZE_X, WINDOW_SIZE_Y);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(this);
        frame.setResizable(false);
    }

    @Override
    public ArrayList<Piece> choosePieces() {

        //some code here

        //NOW THIS METHOD IS REACHED, BUT NEVER SEEMS TO CALL THE ABOVE PAINT COMPONENT METHOD
        this.repaint();

        //some more code here


    }

}

Now when I don't create the initial window for option choosing, all seems to work fine...So I'm guessing it's some kind of conflict between ChessGui and GameInterface. I've spend quite a bit of time now in debugging mode in Eclipse setting breakpoints and trying to track the issue down, but without success. Please help!

AstroCB
  • 12,337
  • 20
  • 57
  • 73
mmagician
  • 1,970
  • 2
  • 15
  • 26
  • 1) Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). 2) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). – Andrew Thompson May 26 '14 at 00:29

1 Answers1

0

you must call validate(); before repaint();:

 @Override
    public ArrayList<Piece> choosePieces() {

        //some code here

        this.validate();
        this.repaint();

        //some more code here


    }