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 -
if(level==3)
-
{
-
//the game is over if level is 3
-
JOptionPane.showMessageDialog(null, winnerName+" has completed this Level!");
-
-
gameStart = false;
-
playArea.repaint();
-
}
-
and here is the paintComponent routine of the panel -
public void paintComponent(Graphics g)
-
{
-
super.paintComponent(g);
-
-
if(gameStart)
-
gameMap.drawAll(this, g);
-
else
-
{
-
//display game statistic
-
if(addedStatistic==false)
-
{
-
setLayout(new FlowLayout(FlowLayout.CENTER,50, 8));
-
-
add(new JLabel(" MAZE GAME PROGRAMME "));
-
-
//statistic for player1
-
if(player1.isHigherScore())
-
{
-
add(new JLabel("Total score for Player 1 = "+player1.getScore()+" - NEW HIGH SCORE ^^"));
-
-
//update user file with new score
-
player1.updateMyScore();
-
}
-
else
-
add(new JLabel("Total score for Player 1 = "+player1.getScore()));
-
//statistik for player2
-
if(player2.isHigherScore())
-
{
-
add(new JLabel("Total score for Player 2 = "+player2.getScore()+" - NEW HIGH SCORE ^^"));
-
-
//update user file with new score
-
player2.updateMyScore();
-
}
-
else
-
add(new JLabel("Total score for Player 2 = "+player2.getScore()));
-
-
//display winner
-
if(player1.getScore() > player2.getScore())
-
add(new JLabel("Congratulations Player 1 : "+player1.getName()+" Win this Game."));
-
else if(player2.getScore() > player1.getScore())
-
add(new JLabel("Congratulations Player 2 : "+player2.getName()+" Win this Game."));
-
else
-
add(new JLabel("Congratulations You both have the same score"));
-
-
addedStatistic = true;
-
repaint();
-
}
-
}
-
}
-
thx
4 6569 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
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
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.paintComp onent(g);
kind regards,
Jos
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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 through them, displaying
panel_0, then panel_1....
I have no problem removing panel_0 from the JPanel parent (I've even done a
repaint() right after removing to be sure of this), but no matter what I
do, I can't seem to do an "add(panel_1)" to the...
|
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 save it to a jpeg and then
display it in the page.
Is there anyway to create an Image from a JPanel without actually displaying
it in a JFrame first? Can anyone suggest a better way to create a chart
image for servlet/jsp?
|
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 perfectly. In NS 7 there are
artifacts left over from what previously occupied a given frame.
If I minimize the NS browser, then mazimize it again, the NS browser
repaints the contents of the frames, and everything looks correct.
Is there a way -...
|
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 I toggle back
to Excel, it does not do that. The repaint is quick. I'm running this
within a Citrix environment. The scrolling of the DataGrid is slow
also.
Would anyone know how I can fix or even determine what is going on?
Thank you!
|
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 added back in, i simply cannot type! HELP!!!!
Codes are as follow:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
| |
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 extends JPanel and I set its size to
210x210.
|
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 why. Any help would be greatly appreciated
int playerCount = 0;
private void addPlayerButtonActionPerformed(java.awt.event.ActionEvent evt) {
String name = nameTextField.getText();...
|
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 some sample code showing what I'm trying to achieve:
Effectively I expect to see a JLabel with the message "This is a test" as well as a JButton "foo".
What am I doing wrong? Is this possible? Also, I am aware it would be easier to just extend...
|
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. When a radio button wass selected and "OK/Next" was clicked on, I was able to write some code inside the "ActionPerformed" method to verify that the radio button was selected as intended. But that was only a quick test to see that things worked. ...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |