On Jan 19, 3:52 am, "John B. Matthews"<nos...@nospam.invalid> wrote:
In article
<6b60997a-5381-4e15-a8a2-2523bfb48...@m20g2000prc.googlegroups.com>,
SamuelXiao<foolsmart2...@gmail.com> wrote:
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."
Also, consider the benefits of<http://sscce.org/>.
As it is incomplete, I am unable to compile your example.
--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
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
Practice/ by Grian Goetz, et al.
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.
(lock) held by the timer. Another way is a countdown latch. Another way, not
indicate if the timer task is finished. All of these techniques are in the
literature, which *you must study*. *First*.