Re: repaint not work as expected
On Jan 18, 8:29 pm, "John B. Matthews" <nos...@nospam.invalid> wrote:
In article
<e5b47e7f-05d2-45f1-9ffb-ea525bc82...@v17g2000prc.googlegroups.com>,
SamuelXiao <foolsmart2...@gmail.com> wrote:
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]
<http://groups.google.com/group/comp.lang.java.programmer/browse_frm/t...=
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/>.
--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
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.