私はモールを管理することを目標とする小さなゲームをコーディングしています。私の問題はアニメーションです。
Animateur
実は、私の同僚が を拡張Thread
して実装するクラスを作成しましたRunnable
。
私のフレームでは、このクラスを宣言して初期化し、ボタンを押しrun()
て実行する必要があります。
アニメーションは、モールの片側から来て、何かを買って反対側から立ち去る人々で構成されています。
一日を始めるには、run()
メソッドを呼び出す必要があります。
初日、プッシュした後、すべてが完璧です。人々とすべてが見えます。しかし、同じボタンを 2 回押して、新しい日を始めると、何も機能せず、行き詰まります。クラスrun()
で別の時間を実行するために、新しい日を始めることができないため、プレイできません。Animateur
誰かが私を助けてくれたり、それを解決するためのアイデアを教えてくれませんか? :(
package myMall;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
public class Animateur extends Thread implements Runnable {
private int giorno = 1;
private int giornoAttuale=1;
private int delay = 30;
private int nbVisiteurs =0;
private int k=0;
private double gainmoyen;
public MonMall m;
public List<Integer> w = new ArrayList<Integer>();
public List<Integer> e = new ArrayList<Integer>();
public int [] r = new int[6];
public Animateur(MonMall k) {
this.m = k;
m.setGainmoyen((int)this.gainmoyen);
m.setVisitestructure(this.r);
}
public void setAvance(){
this.giornoAttuale++;
}
public void run() {
while (m.isSimulation()) {
if(giorno<=giornoAttuale){
k = k + 30;
if (k < 10000) {
Client c = new Client(1000, 0, 0, 0);
m.listeTemps.add(k);
if (m.GenereClient(0.05)) {
m.Clients.add(c);
m.ajouterVisiteur(c);
//System.out.println(entreesec(c));
m.destination.add(m.entreesec(c));
nbVisiteurs++;
m.listeVisiteursentres.add(nbVisiteurs);
}else{w.add(nbVisiteurs);}
}
for (int i = 0; i < m.Clients.size(); i++) { //generaliser a Visiteurs
Client a = m.Clients.get(i);
a.move();
//System.out.println(m.getBudget());
if ((a.getLigne() == 10) && (a.getColonne() == 18)) {
m.Clients.remove(i);
m.Visiteurs.remove(a);
}
if (!m.destination.isEmpty()) {
Element b = m.destination.get(i);
if (a.getLigne() == b.getLigne() && a.getColonne() == b.getColonne() - 1) {
b.entreereelle(a);
m.setBudget(m.getBudget() + b.getGain());
this.gainmoyen+=m.getBudget();
//b.sortie(a);//una estructura nunca se llena
//System.out.println(m.getBudget());
} else if (a.getLigne() == b.getLigne() && a.getColonne() == b.getColonne() + 1) {
b.entreereelle(a);
m.setBudget(m.getBudget() + b.getGain());
//b.sortie(a);
//System.out.println(m.getBudget());
}
}
}
if (m.Clients.isEmpty() && k > 12000) {// Pb con el numer o de clientes entrados pero solucionable
m.setSimulation(false);
//m.setListeTemps(e);
//m.setListeVisiteursentres(w);
this.e= m.listeTemps;
this.w= m.listeVisiteursentres;
this.gainmoyen=this.gainmoyen/this.nbVisiteurs;
for(Element e: m.destination){
if(e instanceof Clinique ){
r[0]++;
}else if (e instanceof CommerceGeneral){
r[1]++;
}else if (e instanceof CommerceSpecifique){
r[2]++;
}else if (e instanceof Fun){
r[3]++;
}else if (e instanceof Restauration){
r[4]++;
}else if (e instanceof Gym){
r[5]++;
}
}
System.out.println(m.isSimulation());
(new JOptionPane()).showMessageDialog(null, "Journee finie", "Fin!", JOptionPane.INFORMATION_MESSAGE);
try {
Thread.sleep(this.delay);
}catch (InterruptedException e) {
}
}
//m.notifyFin(); //NO FUNCIONA¿?
}
giorno++;
}
}
}
作成したスレッドを破棄し、クリックで新しいスレッドを初期化しようとしましたが、うまくいきませんでした
編集:
回答ありがとうございます。executor サービスを使用するには、このクラスをRunnable
?の代わりに実装するだけです。