Moving an object using a thread problem
Hi,
i am having a problem with threads.
In the following code a blue JLabel is moved from the left to the right side of a JPanel and back again.Then the movement starts again.
This seems easy to realize. But it only works if i set a "System.out.println(...)"
inside the run() method of the Thread. Else, the label moves from left to right and back again, but then there is no more continously movement. The label gets a little bit to the right but comes back again quickly and tries again - altough it should go to the right in a slow motion.
Here is the code(can be compiled in one File, named: Test.java):
The most important is MoveThread run() method. If the outputs "forward" and "back" are commented out, the described problem occurs.
Thanks for any explanation in advance, why this happens.
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
/** Moves the label over the screen (important) */
class MoveThreadextends Thread{
MyLabel myLabel;
public MoveThread(MyLabel myLabel){
this.myLabel = myLabel;
}
publicvoid run(){
while(isInterrupted() ==false){
try{Thread.sleep(100);}catch(InterruptedException ie){}
while(myLabel.getX() < 400){
myLabel.move(0);//forward
System.out.println("forward");
}
while(myLabel.getX() > 0){
myLabel.move(1);//back
System.out.println("back");
}
}
}
}
/** This is the label to move (not important) */
class MyLabelextends JLabel{
JPanel panel;
int i = 0;
int operationPlus = 1;
int operationMinus = -1;
public MyLabel(JPanel panel){
super("Label");
this.panel = panel;
setBackground(java.awt.Color.blue);
setBorder(new LineBorder(java.awt.Color.RED));
setOpaque(true);
}
publicvoid move(int method){
setLocation(getX()+((method == 0)?operationPlus:operationMinus), getY());
panel.repaint();
}
}
/** The label is placed on this panel (not important) */
class MyPanelextends JPanel{
MyLabel myLabel;
MoveThread moveThread;
public MyPanel(){
setLayout(null);
myLabel =new MyLabel(this);
myLabel.setBounds(0,20,50,50);
add(myLabel);
moveThread =new MoveThread(myLabel);
moveThread.start();
}
publicvoid stopThread(){
moveThread.interrupt();
}
}
/** Holds the Panel (not important) */
publicclass Testextends JFrame{
MyPanel myPanel =new MyPanel();
public Test(){
getContentPane().add(myPanel);
addWindowListener(new WindowAdapter(){
publicvoid windowClosing(WindowEvent e){
myPanel.stopThread();
System.exit(0);
}
});
setSize(600,600);
setVisible(true);
}
publicstaticvoid main(String args[]){
Test t =new Test();
}
}

