简体   繁体   中英

JFrame not refreshing with repaint

I have a visual class Gameboard that consists of a frame, representing a 'board'. The idea is that when a button is clicked the players on the board will move, and so when the button is clicked the board must refresh. This is the frame:

public Gameboard(Game game, int size, ArrayList<Enemy> e, Ogre o){

    this.ogre = o.getPosition();
    if(this.enemies.isEmpty() == false){
        this.enemies.clear();
    }
    if(e.isEmpty() == false){
        for(Enemy en : e){
            this.enemies.add(en.getPosition());
        }
    }
    this.game = game;

    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            }

            JFrame frame = new JFrame("Swamp Escape!");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new BorderLayout());
            frame.add(new GridPane(size));
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
            frame.setResizable(false);

        }
    });
}

public class GridPane extends JPanel {

    public GridPane(int size) {

        setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        for (int row = 0; row < size; row++) {
            for (int col = 0; col < size; col++) {
                gbc.gridx = col;
                gbc.gridy = row;

                if(col == ogre.getX() && row == ogre.getY()) {
                    OgrePane ogre = new OgrePane();
                    Border border = null;
                    if (row < size - 1) {
                        if (col < size - 1) {
                            border = new MatteBorder(1, 1, 0, 0, Color.GRAY);
                        } else {
                            border = new MatteBorder(1, 1, 0, 1, Color.GRAY);
                        }
                    } else {
                        if (col < size - 1) {
                            border = new MatteBorder(1, 1, 1, 0, Color.GRAY);
                        } else {
                            border = new MatteBorder(1, 1, 1, 1, Color.GRAY);
                        }
                    }
                    ogre.setBorder(border);
                    add(ogre, gbc);
                } else {
                    CellPane cellPane = new CellPane();
                    Border border = null;
                    if (row < size - 1) {
                        if (col < size - 1) {
                            border = new MatteBorder(1, 1, 0, 0, Color.GRAY);
                        } else {
                            border = new MatteBorder(1, 1, 0, 1, Color.GRAY);
                        }
                    } else {
                        if (col < size - 1) {
                           border = new MatteBorder(1, 1, 1, 0, Color.GRAY);
                        } else {
                            border = new MatteBorder(1, 1, 1, 1, Color.GRAY);
                        }
                    }
                    cellPane.setBorder(border);
                    add(cellPane, gbc);
                }

            }
        }

        JButton newGame = new JButton("New Game");
        newGame.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                game.newGame();
                ogre = game.getHek().getPosition();
                if(enemies != null) enemies.clear();
                for(Enemy en : game.enemies){
                    enemies.add(en.getPosition());
                }
                revalidate();
                repaint();
            }
        });
        gbc.gridx = 0;
        gbc.gridy = size + 1;
        add(newGame, gbc);

        JButton move = new JButton("Next Move");
        move.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                game.nextMove();
            }
        });
        gbc.gridx = 1;
        gbc.gridy = size + 1;
        add(move, gbc);

        JButton undo = new JButton("Undo");
        gbc.gridx = 2;
        gbc.gridy = size + 1;
        add(undo, gbc);

        JButton diet = new JButton("Change Diet");
        gbc.gridx = 3;
        gbc.gridy = size + 1;
        add(diet, gbc);
    }
}

The newGame method is working correctly but when the button tries to repaint the frame nothing happens. Specifically the new Game button.

The frame consists of a grid, each of which are populated with one a three types of cells, each extending the jpanel class.

The repaint is being called in the JButton "new game". The Cells are added in a loop fashion.

Any help is welcome!

Instead of simply calling invalidate() and repaint() from the JPanel level, call them from the JFrame level. Like so:

JButton newGame = new JButton("New Game");
    newGame.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            game.newGame();
            ogre = game.getHek().getPosition();
            if(enemies != null) enemies.clear();

            for(Enemy en : game.enemies){
                enemies.add(en.getPosition());
            }

            JFrame frame = (JFrame) getRootPane().getParent();
            frame.pack();
        }
    });

If anyone has a similar issue this is how I fixed it:

Rather than repainting the JFrame, I simply disposed of the the current frame and created a new one with the updated positions. Its a bit of a 'Gaffer-tape' fix but it work how I want it to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM