Witam
Zrobiłem sobie programik na zaliczenie do szkoły, taka symulacja z przymrużeniem oka układu planetarnego. Każda planeta porusza się w osobnym wątku, który można zatrzymać i wznowić.
W sumie działa ale obsługa wątków działa na zakazanych metodach suspend() i resume(), przy użyciu innych metod wyskakiwał bład w stylu IllegalMonitorStateException, próbowałem coś się o tym dowiedzieć (synchronized itd) ale trudno mi to rozkminić, może mi ktoś wytłumaczyć jak przerobić ten programik aby działał z aktualnie dozwolonymi metodami. Wszelkie uwagi związane nie tylko z wątkami mile widziane.
Wyziew koda:
import se.datadosen.component.*;
public class UniverseFrame extends JFrame {
private PlanetComponent comp;
private Planet p1, p2, p3, p4, p5, p6, p7, p8, p9;
private PlanetRunnable r1, r2, r3, r4, r5, r6, r7, r8 ,r9;
private Thread t1, t2, t3, t4, t5, t6, t7, t8, t9;
private PlanetMenu merkury, wenus, ziemia, mars, jowisz, saturn, uran, neptun, pluton;
public UniverseFrame() {
setTitle("Wielowatkowe Planety");
setSize(800, 625);
setBackground(new Color(38, 18, 112));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new RiverLayout());
setResizable(false);
createAndShowGUI();
setPlanet();
addActions();
}
public void createAndShowGUI() {
comp = new PlanetComponent();
add("left", comp);
JPanel panelPrawy = new JPanel();
panelPrawy.setPreferredSize(new Dimension(200, 600));
//tworzenie paneli do obslugi planet (wnow, zatrzymaj)
merkury = new PlanetMenu("Merkury");
wenus = new PlanetMenu("Wenus");
//itd
add("center", panelPrawy);
panelPrawy.add(merkury);
panelPrawy.add("br", wenus);
//itd
}
public void setPlanet() {
p1 = new Planet(300, 300, 80, 5);
comp.add(p1/* 241, 229, 32*/);
r1 = new PlanetRunnable(comp, p1, 0.0057, 50.0);
t1 = new Thread(r1);
t1.start();
//itd
}
public void addActions() {
StartAction a1 = null, a2=null, a3=null, a4=null, a5=null, a6=null, a7=null, a8=null, a9=null ;
StopAction s1=null, s2=null, s3=null, s4=null, s5=null, s6=null, s7=null, s8=null, s9=null;
newActions(a1, s1, t1, merkury);
newActions(a2, s2, t2, wenus);
//itd
}
public void newActions(StartAction a1, StopAction a2, Thread t, PlanetMenu menu) {
a2 = new StopAction(t, menu);
a1 = new StartAction(t, menu);
menu.start.addActionListener(a1);
menu.stop.addActionListener(a2);
}
class StopAction implements ActionListener {
private Thread thr;
private PlanetMenu pm;
public StopAction(Thread t, PlanetMenu pm){
thr=t;
this.pm= pm;
}
public void actionPerformed(ActionEvent e) {
try {
pause();
} catch (InterruptedException ex) {
}
pm.start.setEnabled(true);
((JButton) e.getSource()).setEnabled(false);
}
public void pause() throws InterruptedException{
// thr.interrupt();
thr.suspend(); //użycie zakazanej metody
}
}
class StartAction implements ActionListener {
private Thread thr;
private PlanetMenu pm;
public StartAction(Thread t, PlanetMenu pm){
thr=t;
this.pm= pm;
}
public void actionPerformed(ActionEvent e) {
thr.resume(); //użycie zakazanej metody
pm.stop.setEnabled(true);
((JButton) e.getSource()).setEnabled(false);
}
}
}
public class PlanetRunnable implements Runnable {
private double freq;
private double sleep;
private Planet p;
private Component comp;
public PlanetRunnable(Component comp, Planet p, double freq, double sleep) {
this.comp = comp;
this.p = p;
this.freq = freq;
this.sleep = sleep;
}
public void run() {
try {
for (double i=0; i<1000; i+=freq) {
p.move(i);
comp.repaint();
Thread.sleep((long) sleep);
}
} catch (InterruptedException e) {}
}
}
Pozdrawiam