0
threadの使い方について
絵画した黒丸を上から下に流すというプログラムを、指定した時間差(例えば5秒後に2つ、7秒後に1つ、10秒後に2つといった感じ)で動かしたいのですが、Threadがうまく使えず、表示もされなくなってしまいました。
どうすればいいのかわからないので、教えていただけますでしょうか。
//game.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.concurrent.*;
class MovingFrame extends JFrame{
public MovingFrame(){
this.setTitle("Random Frame");
this.setSize(900,600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String argv[]) {
Thmove mv1 = new Thmove();
Thmove mv2 = new Thmove();
mv1.start();
mv2.start();
}
}
//Thmove.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Thmove extends Thread{
public void run(){
try{
sleep(5000);
}catch(InterruptedException e){
}
MovingPanel amv = new MovingPanel();
}
}
class MovingPanel extends JPanel implements ActionListener{
private Timer timer;
private int x,y;
MovingPanel(){
x=30; y=0;
timer = new Timer(1, this);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillOval(x,y,30,30);
}
public void actionPerformed(ActionEvent e){
y=y+5;
if (y>600) timer.stop();
this.repaint();
}
}