Problem with repainting of the screen: Please Help

Ok I have three files, the first is my Pacman game (File is called Game.java), that deals with displaying the main interface of the game (pacman animation, etc). I am trying to keep track of the number of food bits remaining in the maze. So I have the second file responsible for receiving the variable FOOD_COUNTER from Game.java, and then paint the value on the screen ( This file is called Score.java). See the codes below.

Now the third file is a layout manager that uses GridBag layout, and is suppose to add the two above files to the main interface.

The problem I have with running the files, is that when the value of the FOOD_COUNTER is updated in Game.java (ie when pacman eats a bit food in the maze), the new value is not painted to the screen in Score.java. HOWEVER, if I minimize the window and maximize it again, I see the new value of the FOOD_COUNTER painted on the screen. I can’t figure out how to sort this problem. (if I try to add repaint() to the paintComponent() method in Score.java the program runs, but it slows down horribly).

Can you please help me?

Thanks

Here are the codes

This code is responsible for Pacman animation, adding the food to the maze etc.


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Game extends JPanel implements ActionListener{

     private int forwardBy = 5;
     private int upBy = 0;
     private int xANSWER = 0;
     private int yANSWER = 0;
     public int xCount = 315;
     private int yCount = 525;
     public static int FOOD_COUNTER = 140;
     private int CAPSULE_COUNTER = 4;
     private Timer animationTimer;
     static final int ANIM_DELAY=31;
     PacmanM pac = new PacmanM();
     Maze theBoard = new Maze();
     Capsule pill = new Capsule();
     
     public void paintComponent(Graphics g)
     {     
          super.paintComponent(g);
          startAnimation();          
               
          theBoard.xbound = 0;
          theBoard.ybound = 0;
          
          for(theBoard.count1=0; theBoard.count1 < theBoard.ARRAY_BOUND; theBoard.count1++)
          {
          for(theBoard.count2=0; theBoard.count2 < theBoard.ARRAY_BOUND; theBoard.count2++)
               {
               switch (theBoard.board[theBoard.count1][theBoard.count2])
               {
                    case 1:
                    {     
                         g.drawImage(theBoard.block.getImage(), theBoard.xbound, theBoard.ybound, theBoard.BLOCK_SIZE ,theBoard.BLOCK_SIZE ,this);
                    } break;
                         
                    case 2: 
                    {
                         g.setColor(theBoard.rgb);
                         g.fillRect(theBoard.xbound,theBoard.ybound,theBoard.BLOCK_SIZE,theBoard.BLOCK_SIZE);
                              
                         g.setColor(Color.white);
                         g.fillOval(theBoard.xbound+15,theBoard.ybound+15,5,5);
                    } break;
                         
                    case 3:
                    {
                         g.drawImage(pill.images[pill.currentImage].getImage(), theBoard.xbound , theBoard.ybound, theBoard.BLOCK_SIZE ,theBoard.BLOCK_SIZE ,this);
                         pill.currentImage = ( pill.currentImage +1 ) % pill.TOTAL_IMAGES;
     
                    } break;
                         
                    case 4:
                    {
                         g.setColor(theBoard.rgb);
                         g.fillRect(theBoard.xbound,theBoard.ybound,theBoard.BLOCK_SIZE,theBoard.BLOCK_SIZE);
                    } break;
               }     
          theBoard.xbound += theBoard.BLOCK_SIZE;
          }
     
     theBoard.ybound += theBoard.BLOCK_SIZE;
     theBoard.xbound = 0;
     }

     if (pac.images[pac.currentImage].getImageLoadStatus() == MediaTracker.COMPLETE)
     {   
          g.drawImage(pac.images[pac.currentImage].getImage(), xCount , yCount, theBoard.BLOCK_SIZE ,theBoard.BLOCK_SIZE ,pac);
          //calculating location of pacman in the array
          xANSWER = xCount  / theBoard.BLOCK_SIZE;
          yANSWER = yCount  / theBoard.BLOCK_SIZE;
               
          // using the left & right exits in the maze
          if ( (xANSWER == 18) && (yANSWER == 9) && (currentDirection == RIGHT) ) { xCount = 0; yCount = 315;}
          if ( (xANSWER == 0) && (yANSWER == 9) && (xCount == 0) && (currentDirection == LEFT) ) { xCount = 630; yCount = 315;}
               
          // Collision detection with the maze walls
          if ( ((forwardBy == 5) || (upBy == 5)))
          {
               if ( xANSWER != 18 ) if ((theBoard.board[yANSWER][xANSWER+1] == 1)) { forwardBy = 0; }

               if (theBoard.board[yANSWER+1][xANSWER] == 1) { upBy = 0; }
                    
               //preventing access to the ghost house
               if ( (xANSWER == 9) && (yANSWER == 7) ) upBy = 0;
          }

          if ( (forwardBy == -5) || (upBy == -5) )
          {
               if ( xANSWER != 0 ) if ((theBoard.board[yANSWER][xANSWER-1] == 1)) 
               { if ( (xCount) == ((xANSWER) * theBoard.BLOCK_SIZE) ) forwardBy = 0;}
               
               if (theBoard.board[yANSWER-1][xANSWER] == 1) 
               { if ( (yCount) == ((yANSWER) * theBoard.BLOCK_SIZE) ) upBy = 0;}     
          }
          }
        
          xCount += forwardBy;
          yCount += upBy;
          if (++pac.currentImage > pac.endImage) pac.currentImage = pac.startImage;     
               
          // checking whether the food has been eaten
          if ( theBoard.board[yANSWER][xANSWER] == theBoard.f ) {theBoard.board[yANSWER][xANSWER] = theBoard.n; FOOD_COUNTER--;}
          // checking whether the capsules have been eaten
          if ( theBoard.board[yANSWER][xANSWER] == theBoard.c ) {theBoard.board[yANSWER][xANSWER] = theBoard.n; CAPSULE_COUNTER--;}
               
          if ( (FOOD_COUNTER == 0) && (CAPSULE_COUNTER == 0) )
          {
               g.setColor (Color.red);
               g.setFont( new Font( "Serif", Font.BOLD, 20) );
               g.drawString(" LEVEL COMPLETED ", 227, 407);     
          }
     }
     
     public void startAnimation()
     {
          if (animationTimer == null)
          {
               pac.currentImage = 0;
               animationTimer = new Timer( ANIM_DELAY, this);
               animationTimer.start();
          } 
          else 
          {
               if (!animationTimer.isRunning())
               animationTimer.restart();
          }
     }
     
     public void stopAnimation (){ animationTimer.stop(); }     
     public Dimension getMinimumSize() { return getPreferredSize(); }
     public Dimension getPreferredSize (){return new Dimension(665,665);}
     public void actionPerformed(ActionEvent e){ repaint(); }
     
     public static void main(String[] args )
     {
          Game app = new Game();
          JFrame main = new JFrame("PACMAN");
          main.getContentPane().add(app, BorderLayout.CENTER);
          main.addWindowListener(new WindowAdapter()
          {
               public void windowClosing(WindowEvent e)
               {
                    System.exit(0);
               }
          });
               main.pack();
               main.show();
     }          
}     

This code is responsible for keeping track of the Food bits, and painting the results to the screen.


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Score extends JPanel
{
Game ga = new Game();

public void paintComponent (Graphics g)
{
g.setColor (Color.gray);
g.fillRect(0,0,150,70);
g.setColor (Color.white);
g.drawLine( 5,0,145,0 );
g.setColor (Color.white);
g.setFont( new Font( "Serif", Font.BOLD, 14) );
g.drawString("Score:",20,25);
g.setColor (Color.yellow);
g.setFont( new Font( "Serif", Font.BOLD, 20) );
g.drawString(ga.FOOD_COUNTER+"",55,50);
}

public Dimension getMinimumSize() { return getPreferredSize(); }
public Dimension getPreferredSize (){return new Dimension(150,70);}
}

How did you add the repaint() that slowed down everything? Just a “repaint()” or a “score.repaint()” (assuming the object is called score) after FOOD_COUNTER was decreased? The latter should work …

Yes before I just added repaint() on its own. Now I realised what the problem was and the program runs correctly.

Thanx!