Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (541)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (604)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Problem with repainting of the screen: Please Help  (Read 1353 times)
0 Members and 1 Guest are viewing this topic.
Offline pars

Senior Newbie




Java games rock!


« Posted 2003-01-08 20:24:42 »

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);
          //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.
---------------------------------------------------
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);}
}
Offline Themroc

Junior Devvie





« Reply #1 - Posted 2003-01-09 16:26:52 »

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 ...
Offline pars

Senior Newbie




Java games rock!


« Reply #2 - Posted 2003-01-09 22:33:56 »

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

Thanx!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Mr.CodeIt (13 views)
2014-12-27 04:03:04

TheDudeFromCI (17 views)
2014-12-27 02:14:49

Mr.CodeIt (25 views)
2014-12-23 03:34:11

rwatson462 (56 views)
2014-12-15 09:26:44

Mr.CodeIt (46 views)
2014-12-14 19:50:38

BurntPizza (92 views)
2014-12-09 22:41:13

BurntPizza (113 views)
2014-12-08 04:46:31

JscottyBieshaar (86 views)
2014-12-05 12:39:02

SHC (97 views)
2014-12-03 16:27:13

CopyableCougar4 (102 views)
2014-11-29 21:32:03
How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!