I have a JPanel where my (game)program draws something that is determined by a piece of code. So far, so good. I wrote a very basic listener that listens for the change to the background model of the program and is then supposed to tell the GUI to update, i.e. move a gamepiece.
All of this happens in a mouseListener triggered method.
I'm guessing that this is the problem, as clicking the mouse again in the JPanel updates the JPanel correctly and I can see the piece moved by the model.
Code for the JPanel:
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
update(g);
}
Update calls a lot of AWT code that doesn't really matter for my issue, it just paints the game board.
(pseudo)Code from my controller, gets executed on mouse event by Mouse Listener:
public void human_and_cpu_move()
{
human_move(int x, int y, int x2, inty2)
cpu_make_move();
}
background model code
public void cpu_make_move()
{
do_something_cool;
tell_listener_something_cool_happened();
}
background Listener:
public class Background_listener()
{
Controller myController;
Background myBackground;
public Background_listener(Controller controller, Background background){
myController = controller;
myBackground = background;
}
public void tell_listener_something_cool_happened()
{
myController.get_JPanel.repaint();
}
}
What behavior do I see? the CPU moves are done only if I click the the JPanel again, they are not made on their own. What do I want? I want the CPU moves to be displayed as soon as they are done. Thanks in advance for any help, I couldn't find a similar question.
CodePudding user response:
I solved the problem: I forgot to tell the GUI to update the game board representation it held from the background model. Simple problem, simple solution.