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.
--------------------------------------------
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
| 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); xANSWER = xCount / theBoard.BLOCK_SIZE; yANSWER = yCount / theBoard.BLOCK_SIZE; if ( (xANSWER == 18) && (yANSWER == 9) && (currentDirection == RIGHT) ) { xCount = 0; yCount = 315;} if ( (xANSWER == 0) && (yANSWER == 9) && (xCount == 0) && (currentDirection == LEFT) ) { xCount = 630; yCount = 315;} 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; } 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; if ( theBoard.board[yANSWER][xANSWER] == theBoard.f ) {theBoard.board[yANSWER][xANSWER] = theBoard.n; FOOD_COUNTER--;} 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.
---------------------------------------------------
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| 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);} } |