Jump to content
tcms

Thread.Sleep

Recommended Posts

tcms

Boas pessoal,

Fiz um pequeno exercicio em que deveria dar uso às Threads, e nomeadamente usar o Thread.sleep para as atrasar. Cada objecto (carro) está associado a uma thread, mas como não estou a conseguir implementar as threads tive de fazer usando um Timer e resultou. Mas sinceramente gostava de saber como podia fazer o mesmo usando o Thread.Sleep.

Aqui vai o código para se perceber melhor:

Código do Carro (objecto)

@SuppressWarnings("serial")
public class Car extends JComponent implements Runnable, ActionListener{
private ImageIcon body;
private int cord_x, cord_y, random_timer;
private Timer timer;
public Car(ImageIcon body, int x, int y){
 this.body = body;
 cord_x = x;
 cord_y = y;
 random_timer = (int) (Math.random() * 50 + 10);
}
@Override
public void paintComponent(Graphics x) {
 super.paintComponent(x);
 body.paintIcon(this, x, cord_x, cord_y);
 repaint();
}
@Override
public void run(){
 timer = new Timer(random_timer,  this);
 timer.start();
}
@Override
public void actionPerformed(ActionEvent e) {
 if(cord_x >=0 && cord_x < 800){
  cord_x+=1;
 }
}
}

Pista (O código aqui está um pouco martelado, mas esta classe não era o principal objectivo do exercicio)

public class Track {
private static  Car one, two;
private JFrame track;
private JPanel sub_track1 = new JPanel();
private JPanel sub_track2 = new JPanel();
// private ArrayList<Car> cars;

public Track(){
 track = new JFrame();
 //  this.cars = cars;
}
public void Start_Up(){
 Track_config();
 track.setVisible(true);
}
private void Track_config(){
 track.setSize(800,300);
 track.setLayout(new GridLayout(2,0));
 track.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 Config();
 addCars();
}
public void Config(){
 track.add(sub_track1);
 track.add(sub_track2);
 sub_track1.setPreferredSize(new Dimension(800, 150));
 sub_track2.setPreferredSize(new Dimension(800, 150));
 sub_track1.setLayout(new BorderLayout());
 sub_track2.setLayout(new BorderLayout());
 sub_track1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
 sub_track2.setBorder(BorderFactory.createLineBorder(Color.BLACK));
}
public void addCars(){
 one = new Car(new ImageIcon("carro_amarelo.gif"),0,(sub_track1.getPreferredSize().height/2));
 two = new Car(new ImageIcon("carro_amarelo.gif"),0,(sub_track2.getPreferredSize().height/2));
 sub_track1.add(one);
 sub_track2.add(two);
}
public static void main(String[] args) {
 Track testing = new Track();
 testing.Start_Up();
 Thread thread1 = new Thread(one);
 Thread thread2 = new Thread(two);
 thread1.start();
 thread2.start();
}

Edited by brunoais
geshi

Share this post


Link to post
Share on other sites
Rui Carlos

Onde é que queres chamar o sleep?

Qual foi o problema que obtivestes ao usar o sleep?

Tens que ter o cuidado de chamar o sleep na thread que queres interromper.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.