repaint not work as expected


S

SamuelXiao

Hi all, I am writing a simple monopoly board game, it has 2 player, 1
is controlled by human while another by PC. Human will trigger roll
dice/buy/so on by pressing buttons. While the PC is doing these
actions automatically by calling functions in sequence. Below is part
of the code.

public void btnRoll(){
final Timer timer = new Timer();
final int index = turn - 1;
boolean snakeEyes = false;
dice1 = (int)(Math.random() * 6 + 1);
dice2 = (int)(Math.random() * 6 + 1);

if(dice1 == dice2) {
snakeEyes = true;
rolled = false;
}

if(snakeEyes == true){
tempFlagPlayer = true;
}else{
tempFlagPlayer = false;
}

timer.schedule(new TimerTask(){
private int diceSum = dice1 + dice2;

public void run(){
synchronized(lock) {
if (diceSum > 0){

movePlayer(players.get(index), tempFlagPlayer); // move player
one space each time
diceSum --;
repaint();
}else{

checkPlayerMovedStatus(players,tempFlagPlayer);

propertymanager.CheckProperty(turn,
players.get(index).getPosition());
lock.notify();
rolled = true;
timer.cancel();
}
repaint();
}
}
}, 100L,100L);
repaint();
}

and in AIturn(int tempNumOfPlayers){

public void AIturn(int tempNumOfPlayers){
btnRoll();
synchronized(lock) {
if(!rolled){
try{
lock.wait();
}
catch(InterruptedException e){}
}

if(propertymanager.Properties[players.get(turn-1).getPosition()]
[0] == 0){
SystemLogHelper.info("enter btnBuy()");
btnBuy();
}
if(rolled) {
btnDone(tempNumOfPlayers);
}
}
}

but I found there is another problem for AIturn(int tempNumOfPlayers),
in the btnRoll() in AIturn(),

if (diceSum > 0){
movePlayer(players.get(index), tempFlagPlayer); //
move player one
space each time
diceSum --;
repaint();
}

I found the repaint() doesn't work when it comes to AIturn() call, it
directly go to the cell instead step by step. The repaint() seems not
update each step. If there any way to force repaint()? Thanks.
 
Ad

Advertisements

J

John B. Matthews

SamuelXiao said:
Hi all, I am writing a simple monopoly board game, it has 2 player, 1
is controlled by human while another by PC. Human will trigger roll
dice/buy/so on by pressing buttons. While the PC is doing these
actions automatically by calling functions in sequence. Below is
part of the code.

[code elided]
I found the repaint() doesn't work when it comes to AIturn() call, it
directly go to the cell instead step by step. The repaint() seems
not update each step. If there any way to force repaint()? Thanks.

You might be able to use paintImmediately(), as discussed here:

<http://java.sun.com/products/jfc/tsc/articles/painting/index.html>

But beware the caveats mentioned under "Synchronous Painting."

In general, I prefer javax.swing.Timer for animation. Some advantages
are mentioned here:

<http://download.oracle.com/javase/6/docs/api/javax/swing/Timer.html>

Here's a simple example:

<http://sites.google.com/site/drjohnbmatthews/subway>

Also, consider the benefits of <http://sscce.org/>.
 
S

SamuelXiao

 SamuelXiao said:
Hi all, I am writing a simple monopoly board game, it has 2 player, 1
is controlled by human while another by PC. Human will trigger roll
dice/buy/so on by pressing buttons. While the PC is doing these
actions automatically by calling functions in sequence.  Below is
part of the code.

[code elided]
I found the repaint() doesn't work when it comes to AIturn() call, it
directly go to the cell instead step by step.  The repaint() seems
not update each step.  If there any way to force repaint()? Thanks.

You might be able to use paintImmediately(), as discussed here:

<http://java.sun.com/products/jfc/tsc/articles/painting/index.html>

But beware the caveats mentioned under "Synchronous Painting."

In general, I prefer javax.swing.Timer for animation. Some advantages
are mentioned here:

<http://download.oracle.com/javase/6/docs/api/javax/swing/Timer.html>

Here's a simple example:

<http://sites.google.com/site/drjohnbmatthews/subway>

Also, consider the benefits of <http://sscce.org/>.

Actually, btnRoll() is a function within antionPerformed(), but it is
not in the same class....

btnRoll.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(!monopolyBoard.rolled) {
monopolyBoard.btnRoll();
}
}
});

As above, btnRoll() in fact at another class monopolyBoard...when I
use the swing timer, I found it cannot to do the same effect... For
the repaint in loop..do you know if there any way force to repaint? I
tried use thread.sleep but it is not work as well.

public void btnRoll(){
final Timer timer = new Timer();
final int index = turn - 1;
boolean snakeEyes = false;
dice1 = (int)(Math.random() * 6 + 1);
dice2 = (int)(Math.random() * 6 + 1);

if(dice1 == dice2) {
snakeEyes = true;
rolled = false;
}
if(snakeEyes == true){
tempFlagPlayer = true;
}else{
tempFlagPlayer = false;
}

timer.schedule(new TimerTask(){
private int diceSum = dice1 + dice2;

public void run(){
synchronized(lock){
if (diceSum > 0){
movePlayer(players.get(index), tempFlagPlayer); // move player
one space each time
diceSum --;
validate();
repaint(); // not repaint() here
}else{
checkPlayerMovedStatus(players,tempFlagPlayer);
propertymanager.CheckProperty(turn,
players.get(index).getPosition());
// rolled = true;
timer.cancel();

if(!tempFlagPlayer){
rolled = true;
}
lock.notify();
}
MonopolyBoard.this.repaint();
}
}
}, 100L,100L);
}

Pls forgive my English. Any help would be highly appreciated.
 
J

John B. Matthews

SamuelXiao said:
In general, I prefer javax.swing.Timer for animation. Some advantages
are mentioned here:

<http://download.oracle.com/javase/6/docs/api/javax/swing/Timer.html>

Here's a simple example:

<http://sites.google.com/site/drjohnbmatthews/subway>

[...]

timer.schedule(new TimerTask(){

This code uses java.util.Timer, rather than javax.swing.Timer, as
suggested above. The later "can make dealing with the event-dispatching
thread a bit simpler."

As it is incomplete, I am unable to compile your example.
 
S

SamuelXiao

 SamuelXiao said:
In general, I prefer javax.swing.Timer for animation. Some advantages
are mentioned here:
<http://download.oracle.com/javase/6/docs/api/javax/swing/Timer.html>
Here's a simple example:
<http://sites.google.com/site/drjohnbmatthews/subway>

   timer.schedule(new TimerTask(){

This code uses java.util.Timer, rather than javax.swing.Timer, as
suggested above. The later "can make dealing with the event-dispatching
thread a bit simpler."

As it is incomplete, I am unable to compile your example.

Hi, I changed the code to use java swing timer, it can now repaint the
token smoothly, but only for the human-controlled token, but for the
PC token, it still has the problem that timer has not yet finished but
the functions after timer.start() has already been done. Below is
some code.

Under class monpolyBoard.

// PC turn will trigger this method
public void AIturn(int tempNumOfPlayers){
btnRoll(); // timer is in btnRoll()
// and below few lines have been done before the timer
finish.
if(propertymanager.Properties[players.get(turn-1).getPosition()][0]
== 0){
SystemLogHelper.info("enter btnBuy()");
btnBuy();
}
if(rolled) btnDone(tempNumOfPlayers);
}

public void btnDone(int tempNumOfPlayers){
rolled = false;
turn = (turn % tempNumOfPlayers) + 1;
// turn start 1
if(players.get(turn - 1) instanceof AutoPlayer){
AIturn(tempNumOfPlayers);
}
}

public void btnRoll() {
boolean snakeEyes = false;
dice1 = rand.nextInt(6) + 1;
dice2 = rand.nextInt(6) + 1;
minusDice = dice1 + dice2;
if(dice1 == dice2) {
snakeEyes = true;
rolled = false;
}
else {
rolled = true;
}
if(snakeEyes == true){
tempFlagPlayer = true;
}else{
tempFlagPlayer = false;
}
timer = new Timer(100, new RollActionListener());
timer.start();
repaint();
}


private class RollActionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
if(minusDice-- <= 0){
checkPlayerMovedStatus(players,tempFlagPlayer);
propertymanager.CheckProperty(turn,
players.get(turn-1).getPosition());
timer.stop();
}else{
movePlayer(players.get(turn-1),tempFlagPlayer);
}
SystemLogHelper.info("minusDice is: " + minusDice);
repaint();
}
}

Under MonopolyEntry, it register the eventListener.

public void actionPerformed(ActionEvent e) {
if(e.getSource() == btnRoll){
if(!monopolyBoard.rolled) {
monopolyBoard.btnRoll();
}
}
if(e.getSource() == btnDone){
if(monopolyBoard.rolled) {
monopolyBoard.btnDone(tempNumOfPlayers);
}
}

// and so on
}

If I use the swing timer, the AIturn()'s btnRoll()'s timer will not
finish before the next few line:

if(propertymanager.Properties[players.get(turn-1).getPosition()][0]
== 0){
SystemLogHelper.info("enter btnBuy()");
btnBuy();
}
if(rolled) btnDone(tempNumOfPlayers);

That's why i have added a lock object before...that one works but it
doesn't repaint every time. Do you know how I can wait for the timer
finished it work first then go to the next few lines? Thanks.
 
L

Lew

SamuelXiao said:
In general, I prefer javax.swing.Timer for animation. Some advantages
are mentioned here:

Here's a simple example:

timer.schedule(new TimerTask(){

This code uses java.util.Timer, rather than javax.swing.Timer, as
suggested above. The later "can make dealing with the event-dispatching
thread a bit simpler."
Also, consider the benefits of<http://sscce.org/>.

As it is incomplete, I am unable to compile your example.

Hi, I changed the code to use java swing timer, it can now repaint the
token smoothly, but only for the human-controlled token, but for the
PC token, it still has the problem that timer has not yet finished but
the functions after timer.start() has already been done. Below is
some code.

Under class monpolyBoard.

// PC turn will trigger this method
public void AIturn(int tempNumOfPlayers){
btnRoll(); // timer is in btnRoll()
// and below few lines have been done before the timer
finish.
if(propertymanager.Properties[players.get(turn-1).getPosition()][0]
== 0){
SystemLogHelper.info("enter btnBuy()");
btnBuy();
}
if(rolled) btnDone(tempNumOfPlayers);
}

public void btnDone(int tempNumOfPlayers){
rolled = false;
turn = (turn % tempNumOfPlayers) + 1;
// turn start 1
if(players.get(turn - 1) instanceof AutoPlayer){
AIturn(tempNumOfPlayers);
}
}

public void btnRoll() {
boolean snakeEyes = false;
dice1 = rand.nextInt(6) + 1;
dice2 = rand.nextInt(6) + 1;
minusDice = dice1 + dice2;
if(dice1 == dice2) {
snakeEyes = true;
rolled = false;
}
else {
rolled = true;
}
if(snakeEyes == true){
tempFlagPlayer = true;
}else{
tempFlagPlayer = false;
}
timer = new Timer(100, new RollActionListener());
timer.start();
repaint();
}


private class RollActionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
if(minusDice--<= 0){
checkPlayerMovedStatus(players,tempFlagPlayer);
propertymanager.CheckProperty(turn,
players.get(turn-1).getPosition());
timer.stop();
}else{
movePlayer(players.get(turn-1),tempFlagPlayer);
}
SystemLogHelper.info("minusDice is: " + minusDice);
repaint();
}
}

Under MonopolyEntry, it register the eventListener.

public void actionPerformed(ActionEvent e) {
if(e.getSource() == btnRoll){
if(!monopolyBoard.rolled) {
monopolyBoard.btnRoll();
}
}
if(e.getSource() == btnDone){
if(monopolyBoard.rolled) {
monopolyBoard.btnDone(tempNumOfPlayers);
}
}

// and so on
}

If I use the swing timer, the AIturn()'s btnRoll()'s timer will not
finish before the next few line:

if(propertymanager.Properties[players.get(turn-1).getPosition()][0]
== 0){
SystemLogHelper.info("enter btnBuy()");
btnBuy();
}
if(rolled) btnDone(tempNumOfPlayers);

That's why i [sic] have added a lock object before...that one works but it
doesn't repaint every time. Do you know how I can wait for the timer
finished it work first then go to the next few lines? Thanks.

You have completely failed to protect shared data via critical sections. You
really need to read up on Java concurrency, say the book /Java Concurrency in
Practice/ by Grian Goetz, et al.

You have to synchronize (somehow) access to *every* datum shared between
threads. Your code makes no attempt to protect, for example, 'rolled',
'turn', 'players', 'dice1' or 'dice2'.

You also call "snakeEyes" something which is not snake-eyes. The term is not
synonymous with "double"; it means specifically double ones. It is a very
peculiar name for a variable that tracks a condition that is not double ones.

To answer your question, one way to force a wait is to block on a monitor
(lock) held by the timer. Another way is a countdown latch. Another way, not
recommended, is *correct* use of wait/notify, usually with a shared flag to
indicate if the timer task is finished. All of these techniques are in the
literature, which *you must study*. *First*.
 
Ad

Advertisements

J

John B. Matthews

SamuelXiao said:
Do you know how I can wait for the timer
finished it work first then go to the next few lines?

One simple expedient would be to have the Timer's handler send a
message to any interested listener(s).

Also, you might like to examine this simple, turn-based game [1] that
uses the AWT EventQueue [2] to sequence events. In particular, see
org.gcs.robot.RCView [3].

[1]<http://robotchase.sourceforge.net/>
[2]<http://download.oracle.com/javase/6/docs/api/java/awt/EventQueue.html>
[3]<http://robotchase.svn.sourceforge.net/viewvc/robotchase/trunk/src/org/gcs/robot/RCView.java>
 
Ad

Advertisements

S

SamuelXiao

 SamuelXiao said:
Do you know how I can wait for the timer
finished it work first then go to the next few lines?

One simple expedient would be to have the Timer's handler send a
message to any interested listener(s).

Also, you might like to examine this simple, turn-based game [1] that
uses the AWT EventQueue [2] to sequence events. In particular, see
org.gcs.robot.RCView [3].

[1]<http://robotchase.sourceforge.net/>
[2]<http://download.oracle.com/javase/6/docs/api/java/awt/EventQueue.html>
[3]<http://robotchase.svn.sourceforge.net/viewvc/robotchase/trunk/src/org....>

Thanks all, I have changed to use the swing timer and put the
if(propertymanager.Properties[players.get(turn-1).getPosition()][0] ==
0){
SystemLogHelper.info("enter btnBuy()");
btnBuy();
}
if(rolled) btnDone(tempNumOfPlayers);

}
inside the actionPerformed() as well, and now it works, thanks very
much.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top