简体   繁体   中英

Repainting in Java Swing with containers

I have a simple grid that places a square wherever the user clicks. The grid and access to the panes are held in a "Game" object.

This works:

private void buildClicked(int x, int y) {
    panel.repaint();
    game.buy(x, y);
}

This does not trigger a repaint:

private void buildClicked(int x, int y) {
    game.getPanel().repaint();
    game.buy(x, y);
}

If I make the panel a public variable of Game, this doesn't work either:

private void buildClicked(int x, int y) {
    game.panel.repaint();
    game.buy(x, y);
}

"getPanel" simply returns the same custom panel object that the top's "panel" object is referring to.

I would like to contain the panel in the Game object wrapper. Similarly, calling the repaint function inside the "buy" function doesn't work.

Why does the "repaint" function behave differently in the above examples?

The comments under my question were exactly right. I had been declaring the panel separately in the Game object and the window itself. Another case of being caught up in new material and missing something basic. Thank you!

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