473,503 Members | 12,383 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

JPanel - repaint problem

144 New Member
Hi,

i have problem with a JPanel in my JInternalFrame. it's like a maze game. after the game finished, i want to display game's statistic in the same panel i draw the tiles of the maze. but instead of displaying the statistic labels. it display nothing. I have to move the JInternalFrame so that the panel display the statistic.

how could that after the game finish (the player reach the key), the panel display the statistic immediately?

Here is the code determining that the game has ended
Expand|Select|Wrap|Line Numbers
  1.                     if(level==3)
  2. {
  3.     //the game is over if level is 3
  4.     JOptionPane.showMessageDialog(null, winnerName+" has completed this Level!");
  5.  
  6.     gameStart = false;
  7.     playArea.repaint();
  8. }
  9.  
and here is the paintComponent routine of the panel
Expand|Select|Wrap|Line Numbers
  1.                   public void paintComponent(Graphics g)
  2.         {
  3.             super.paintComponent(g);
  4.  
  5.             if(gameStart)
  6.                 gameMap.drawAll(this, g);
  7.             else
  8.             {
  9.                 //display game statistic
  10.                 if(addedStatistic==false)
  11.                 {
  12.                     setLayout(new FlowLayout(FlowLayout.CENTER,50, 8));
  13.  
  14.                     add(new JLabel("    MAZE GAME PROGRAMME    "));
  15.  
  16.                     //statistic for player1
  17.                     if(player1.isHigherScore())
  18.                     {
  19.                         add(new JLabel("Total score for Player 1 = "+player1.getScore()+" - NEW HIGH SCORE ^^"));
  20.  
  21.                         //update user file with new score
  22.                         player1.updateMyScore();
  23.                     }
  24.                     else
  25.                         add(new JLabel("Total score for Player 1 = "+player1.getScore()));
  26.                     //statistik for player2
  27.                     if(player2.isHigherScore())
  28.                     {
  29.                         add(new JLabel("Total score for Player 2 = "+player2.getScore()+" - NEW HIGH SCORE ^^"));
  30.  
  31.                         //update user file with new score
  32.                         player2.updateMyScore();
  33.                     }
  34.                     else
  35.                         add(new JLabel("Total score for Player 2 = "+player2.getScore()));
  36.  
  37.                     //display winner
  38.                     if(player1.getScore() > player2.getScore())
  39.                         add(new JLabel("Congratulations Player 1 : "+player1.getName()+" Win this Game."));
  40.                     else if(player2.getScore() > player1.getScore())
  41.                         add(new JLabel("Congratulations Player 2 : "+player2.getName()+" Win this Game."));
  42.                     else
  43.                         add(new JLabel("Congratulations You both have the same score"));
  44.  
  45.                     addedStatistic = true;
  46.                     repaint();
  47.                 }
  48.             }
  49.         }
  50.  
thx
Nov 21 '07 #1
4 6563
JosAH
11,448 Recognized Expert MVP
It's a threading problem: don't set up all those labels etc. in the paintComponent()
method because it runs in the AWT thread itself, i.e. the thread can't paint those
components (yet) because you're keeping it busy with all these labels.

Create a separate little method that does the setup, invoke it from another thread
and call repaint() at the end; AWT will schedule your paintComponent which sees
the components and delegates to their own paintComponent() methods etc.

kind regards,

Jos
Nov 21 '07 #2
thesti
144 New Member
thx Jos,

well, i'm new to Java. so i'm kinda confused with what you mean to invoke the addStatistic method from another thread. i've created another method to add the labels then call repaint. but still the same result.

could you explain me a little bit more?

thx
Nov 21 '07 #3
JosAH
11,448 Recognized Expert MVP
thx Jos,

well, i'm new to Java. so i'm kinda confused with what you mean to invoke the addStatistic method from another thread. i've created another method to add the labels then call repaint. but still the same result.

could you explain me a little bit more?

thx
Think of the AWT thread as the one and only thread that does the actual painting.
It calls your paintComponent() method when needed. Your paintComponent()
installs new labels and such so it does it in the AWT thread which is still busy
calling you paintComponent() method so it can't draw anything (yet).

You should install those labels etc. somewhere in another thread. The place
where you test if a player has won is a nice place. Install your labels and then
call repaint() which is a hint to the AWT thread that it should start repainting
everything in the near future.

Your paintComponent() method will reduce to almost nothing: either the game
is still not over so it should repaint those tiles; otherwise it doesn't need to do
anything: labels can repaint themselves and your method should just call:
super.paintComponent(g);

kind regards,

Jos
Nov 21 '07 #4
thesti
144 New Member
thx Jos,

i did what you told to create another method to setup the labels and call it from another thread. and i add a little workaround, i call the setSize() so that the labels is painted immediately

thx
Nov 22 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
12845
by: Hal Vaughan | last post by:
I have a JPanel in a window and I am putting different components in it as the user goes through a setup wizard. In theory, I have an array of components (other JPanels), and I want to step...
3
13867
by: Moth | last post by:
I have a PlotPanel class that extends JPanel which I have used previously to display charts in swing guis. I now want to display a graph in a jsp page so I was going to generate the graph and then...
11
5775
by: Ike | last post by:
I changing the size of frames dynamically in a page (I am restricted in that I cannot perform a reload of the content of any of the frames). In MSIE, when I resize the frames, thinks repaint...
7
4976
by: Paul | last post by:
I have a VB.NET form with a DataGrid. When I toggle to Excel (for example) and then back to my application the repaint of the DataGrid is really slow. You can see the repainting happening. When...
1
2132
by: dishal | last post by:
Hi, Im having a problem with typing on the JPanel. The thing is, that it works perfectly fine when this line " content.add(jp, BorderLayout.NORTH); " is taken out (its the buttons panel) but when its...
5
10091
by: James Barrett | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi all, I am experimenting with JApplet and JPanel. My JApplet contains a JPanel called jpanel1 size 210x210. I created a class myPanel which...
1
1993
by: tommyny04 | last post by:
I have a method addPlayer that's supposed to add a JLabel to a JPanel and update the JPanel. The JLabel is just a label with a player's name in it. The code I've wrote doesn't work and I'm not sure...
8
3107
by: eddiewould | last post by:
Hi, I want to write a custom widget which will act similarly to a JPanel (i.e it can contain other Components), but semantically it's not a kind of JPanel so it shouldn't extend from it. Here is...
1
2770
by: Akino877 | last post by:
Hello, I have a question regarding Java and Swing programming I wonder if I could ask the forum for some help. I have a JPanel that has a couple of radio buttons and an "OK/Next" button on it. ...
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7296
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7470
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5604
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4696
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3186
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3174
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
405
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.