i try to move a label in a jframe from place to place by using animation of timer in swing and i always get the last animation that the timer sends for example:
a cetrian label has a variable "place" that holds the label's place every moment place is a variable that 0 < place < 101 and its first value is 1 i also have an enum list that shows all that 100 points x and y so if for example that label place is 52 so:
label.setposition(pointslist.place.getx(),pointslist.place.gety());
in this way it sets the position of the label on place 52
public void move_player1{
timer = new Timer(20, new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
//Move 1 px everytime
if(player1.getLocation().x<pointslist.values()[place].getx()&&player1.getLocation().y==pointslist.values()[place].gety())
player1.setLocation(player1.getLocation().x+1, player1.getLocation().y);
if(player1.getLocation().x==pointslist.values()[place].getx()&&player1.getLocation().y>pointslist.values()[place].gety())
player1.setLocation(player1.getLocation().x, player1.getLocation().y-1);
if(player1.getLocation().x>pointslist.values()[place].getx()&&player1.getLocation().y==pointslist.values()[place].gety())
player1.setLocation(player1.getLocation().x-1, player1.getLocation().y);
if(player1.getLocation().x>pointslist.values()[place].getx()&&player1.getLocation().y>pointslist.values()[place].gety())
player1.setLocation(player1.getLocation().x-1, player1.getLocation().y-1);
if(player1.getLocation().x<pointslist.values()[place].getx()&&player1.getLocation().y<pointslist.values()[place].gety())
player1.setLocation(player1.getLocation().x+1, player1.getLocation().y+1);
if(player1.getLocation().x<pointslist.values()[place].getx()&&player1.getLocation().y>pointslist.values()[place].gety())
player1.setLocation(player1.getLocation().x-1, player1.getLocation().y-1);
if(player1.getLocation().x>pointslist.values()[place].getx()&&player1.getLocation().y<pointslist.values()[place].gety())
player1.setLocation(player1.getLocation().x-1, player1.getLocation().y+1);
if(player1.getLocation().x<pointslist.values()[place].getx()&&player1.getLocation().y>pointslist.values()[place].gety())
player1.setLocation(player1.getLocation().x+1, player1.getLocation().y-1);
if(player1.getLocation().x==pointslist.values()[place].getx()&&player1.getLocation().y==pointslist.values()[place].gety()){
timer.stop();
}
}
});
timer.start();
}
now lets say that in other function i get a random number x and makes the label go through all the points one by one x times like that:
int x=*random* (for example 6);
for(int i=0;i<x;i++)
{
place++;
move_player1();
}
if the player stands on point 52 i want to see in the gui the player moving point by point until it arrives 58 but it doesnt work and i see in the gui the player moving directly from point 52 to 58 when i want it to go only 1 by 1 how can i solve it? how can i stop the 2nd timer from running before the first finish ?
--------edit: here is a runnable example
mainframe:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
public class mainframe extends JFrame{
private JPanel gamepanel;
private JPanel DicePanel;
private Die Dice;
private final int WIDTH=850;
private final int HEIGHT=610;
private final String BACKGROUNDPATH=".\\images\\lns.png";
public JButton button;
public int place;
JLabel player1;
Timer timer;
public mainframe(){
this.setSize(WIDTH,HEIGHT);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
gamepanel=new JPanel();
gamepanel.setBounds(new Rectangle(570,HEIGHT));
gamepanel.setLayout(new BorderLayout());
JLabel backgroundimg=new JLabel(new ImageIcon(BACKGROUNDPATH));
gamepanel.add(backgroundimg);
player1=new JLabel(new ImageIcon(".\\images\\player1.png"));
player1.setBounds(pointslist.POINT1.getx(),pointslist.POINT1.gety(),59,29);
backgroundimg.add(player1);
place=1;
DicePanel=new JPanel();
DicePanel.setLayout(new BorderLayout());
DicePanel.setPreferredSize(new Dimension(WIDTH-gamepanel.getWidth()-15,HEIGHT));
Dice=new Die();
//Button
button = new JButton("ROLL THE DICE !");
button.setFont(new Font("Arial",Font.BOLD,20));
button.setPreferredSize(new Dimension(200,100));
DicePanel.add(button,BorderLayout.SOUTH);
this.setLayout(new BorderLayout());
this.add(gamepanel,BorderLayout.CENTER);
this.add(DicePanel,BorderLayout.LINE_END);
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Dice.roll();
int dienum=Dice.getFaceValue();
System.out.println(dienum);
for(int i=0;i<dienum;i++)
{
place++;
move_player1();
}
}
public void move_player1()
{
timer = new Timer(50, new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
//Move 1 px everytime
if(player1.getLocation().x<pointslist.values()[place].getx()&&player1.getLocation().y==pointslist.values()[place].gety())
player1.setLocation(player1.getLocation().x+1, player1.getLocation().y);
if(player1.getLocation().x>pointslist.values()[place].getx()&&player1.getLocation().y==pointslist.values()[place].gety())
player1.setLocation(player1.getLocation().x-1, player1.getLocation().y);
if(player1.getLocation().x==pointslist.values()[place].getx()&&player1.getLocation().y>pointslist.values()[place].gety())
player1.setLocation(player1.getLocation().x, player1.getLocation().y-1);
if(player1.getLocation().x>pointslist.values()[place].getx()&&player1.getLocation().y>pointslist.values()[place].gety())
player1.setLocation(player1.getLocation().x-1, player1.getLocation().y-1);
if(player1.getLocation().x<pointslist.values()[place].getx()&&player1.getLocation().y<pointslist.values()[place].gety())
player1.setLocation(player1.getLocation().x+1, player1.getLocation().y+1);
if(player1.getLocation().x<pointslist.values()[place].getx()&&player1.getLocation().y>pointslist.values()[place].gety())
player1.setLocation(player1.getLocation().x-1, player1.getLocation().y-1);
if(player1.getLocation().x>pointslist.values()[place].getx()&&player1.getLocation().y<pointslist.values()[place].gety())
player1.setLocation(player1.getLocation().x-1, player1.getLocation().y+1);
if(player1.getLocation().x<pointslist.values()[place].getx()&&player1.getLocation().y>pointslist.values()[place].gety())
player1.setLocation(player1.getLocation().x+1, player1.getLocation().y-1);
if(player1.getLocation().x==pointslist.values()[place].getx()&&player1.getLocation().y==pointslist.values()[place].gety())
{
timer.stop();
}
}
});
timer.start();
}
});
}
public static void main(String[] args) {
mainframe frame = new mainframe();
frame.setVisible(true);
}
}
The Dice class:
class Die{
// Note: If we changed the class definition to "public class Die"
// then we would put this class definition in a separate file Die.java
// Represents one die (singular of dice) with faces showing values
// between 1 and 6.
private final int MAX = 6; // maximum face value
private int faceValue; // current value showing on the die
//-----------------------------------------------------------------
// Constructor: Sets the initial face value.
//-----------------------------------------------------------------
public Die()
{
faceValue = 1;
}
// Alternate Constructor
public Die(int value)
{
faceValue = value;
}
//-----------------------------------------------------------------
// Rolls the die and returns the result.
//-----------------------------------------------------------------
public int roll()
{
faceValue = (int)(Math.random() * MAX) + 1;
return faceValue;
}
//-----------------------------------------------------------------
// Face value mutator.
//-----------------------------------------------------------------
public void setFaceValue (int value)
{
faceValue = value;
}
//-----------------------------------------------------------------
// Face value accessor.
//-----------------------------------------------------------------
public int getFaceValue()
{
return faceValue;
}
// Returns a string representation of this die.
public String toString()
{
String result = Integer.toString(faceValue);
return result;
}
}
and the enum of all the positions:
public enum pointslist {
NOPOINT(-10,-10),
POINT1(15,500),
POINT2(70,500),
POINT3(125,500),
POINT4(180,500),
POINT5(230,500),
POINT6(285,500),
POINT7(338,500),
POINT8(390,500),
POINT9(445,500),
POINT10(500,500),
POINT11(500,450),
POINT12(445,450),
POINT13(390,450),
POINT14(338,450),
POINT15(285,450),
POINT16(230,450),
POINT17(180,450),
POINT18(125,450),
POINT19(70,450),
POINT20(15,450),
POINT21(15,395),
POINT22(70,395),
POINT23(125,395),
POINT24(180,395),
POINT25(230,395),
POINT26(285,395),
POINT27(338,395),
POINT28(390,395),
POINT29(445,395),
POINT30(500,395),
POINT31(500,342),
POINT32(445,342),
POINT33(390,342),
POINT34(338,342),
POINT35(285,342),
POINT36(230,342),
POINT37(180,342),
POINT38(125,342),
POINT39(70,342),
POINT40(15,342),
POINT41(15,290),
POINT42(70,290),
POINT43(125,290),
POINT44(180,290),
POINT45(230,290),
POINT46(285,290),
POINT47(338,290),
POINT48(390,290),
POINT49(445,290),
POINT50(500,290),
POINT51(500,235),
POINT52(445,235),
POINT53(390,235),
POINT54(338,235),
POINT55(285,235),
POINT56(230,235),
POINT57(180,235),
POINT58(125,235),
POINT59(70,235),
POINT60(15,235),
POINT61(15,180),
POINT62(70,180),
POINT63(125,180),
POINT64(180,180),
POINT65(230,180),
POINT66(285,180),
POINT67(338,180),
POINT68(390,180),
POINT69(445,180),
POINT70(500,180),
POINT71(500,130),
POINT72(445,130),
POINT73(390,130),
POINT74(338,130),
POINT75(285,130),
POINT76(230,130),
POINT77(180,130),
POINT78(125,130),
POINT79(70,130),
POINT80(15,130),
POINT81(15,75),
POINT82(70,75),
POINT83(125,75),
POINT84(180,75),
POINT85(230,75),
POINT86(285,75),
POINT87(338,75),
POINT88(390,75),
POINT89(445,75),
POINT90(500,75),
POINT91(500,20),
POINT92(445,20),
POINT93(390,20),
POINT94(338,20),
POINT95(285,20),
POINT96(230,20),
POINT97(180,20),
POINT98(125,20),
POINT99(70,20),
POINT100(15,20);
private final int x;
private final int y;
pointslist(int x,int y)
{
this.x=x;
this.y=y;
}
public int getx(){return x;};
public int gety(){return y;};
}
please pay attention that if for example the player needs to move from square 8 to square 14 it takes the short way instead of going through all the 9 10 11 12 13 i dont want it i want it to make all the way to a certian square