0

The following in a Java SE6 program that is a tic tac toe game:

I have a class MiniPanel that extends JPanel that I have in a grid. If the user clicks one of these, a listener does some stuff then... eventually, it calls a method placeX() or placeO() on the MiniPanel that was clicked. That does some stuff then prints out a message saying that the method was called, and the very next line calls an animatePanel() method, which calls repaint() inside, printing out a message right before.

The class MiniPanel also overrides paintComponent(Graphics g) with a method that first prints out a message saying that paintComponent was called THEN does super.paintComponent(g) and then does other stuff.

The problem is that when I click a MiniPanel, the first 2 messages saying that placeX() or placeO() and animatePanel() was called appears, which means that repaint() must have been called. But paintComponent(Graphics g) is never called! Another weird thing is that it works if I call placeX() directly when the MiniPanel is initialized but not otherwise.

I only have paintComponent(Graphics g) overridden, nothing else.

Here is paintComponent:

public void paintComponent(Graphics g){
        if (Controller.DEBUG) System.out.println("MiniPanel at ("+row+", "+col+") received paintComponent (is repainting now).");
        super.paintComponent(g);
        ...
        ...
    }

placeX (placeO is pretty much the same thing):

    public void placeX(){
    ...
    if (Controller.DEBUG) System.out.println("MiniPanel at ("+row+", "+col+") received placeX.");
    ...
    animatePanel();
}

animatePanel:

        private void animatePanel() {
    if (Controller.DEBUG) System.out.println("MiniPanel at ("+row+", "+col+") doing animatePanel().");
    timer.start();
}

The timer calls this repeatedly for a little animation:

        private ActionListener taskPerformer = new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
        if (Controller.DEBUG) System.out.println("MiniPanel at ("+row+", "+col+") doing actionPerformed().");
        ...
        if (Controller.DEBUG) System.out.println("MiniPanel at ("+row+", "+col+") about to repaint... Should receive paintComponent command!");
        repaint();
    }
};
4

0 回答 0