Java多线程应用程序游戏永远不会停止/结束

时间:2016-05-29 20:33:28

标签: java multithreading

我还在学习java,我试图用NetBeans编写我的第一个多线程java应用程序(赛马游戏)。这一刻有点令人沮丧,因为游戏开始了,但从未结束。我希望有人可以帮助我。提前致谢。这是我的代码:

package wettrennen;
import java.util.Random;
import javax.swing.Icon;
import javax.swing.ImageIcon;

public class Pferd extends Thread{

public int entfernung;
public int nr;
public int y;
public int zug = 0;
public boolean aktiv;

//Constructor
public Pferd (int zug, int entfernung, boolean aktiv){
    this.zug = zug;
    this.entfernung = entfernung;
    this.aktiv = true;
}

/**
 * method, which makes each horse run
 */
public void rennen(){
    int zufallszahl = (int)(Math.random()*4);
    this.zug = +(zufallszahl*10);
    this.entfernung+ = zug;

}

/**
 * main method, which is automatically called for starting the threads
 */
public void run(){
    while (aktiv = true && entfernung <= 270){
        try {
            rennen();

            try {
                sleep(1000);
            } catch (Exception e) {
                System.err.println("Sleeping is not working.");
            }
        } catch (Exception e) {
            System.err.println("Racing is not working");
        }
    }
}

public int getZug(){
    return zug;
}

public int getEntfernung(){
    return entfernung;
}

public boolean getStatus(){
    return aktiv;
}

public boolean setStatus(){
    this.aktiv = false;
    return aktiv;
}
}

包括GUI和Main方法的第二类

package wettrennen;
import java.awt.*;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.JFrame;

public class Wettrennen extends JFrame {

Thread thread;
Vector<Pferd> pferde;
Vector<Thread> threads;
public int numPlayer;
public int entfernung;
public int laenge;
public boolean aktiv;
public final int y = 50;
public Graphics g;




/**
 * Creates new form Game
 */
public Wettrennen() {
    initComponents();
}

/**
 * GUI regenerated by the Form Editor.
 */

...                      


/**
 * action event method which is called after pressing the start button
 * @param evt 
 */
private void StartActionPerformed(java.awt.event.ActionEvent evt) {                                        

numPlayer = getNumPlayer();
initializeHorses();
initializeThreads();
startThreads();

    while (aktiv = true) {
        zeichnen();
    }
}

/**
 * action event method, which ist called after pressing the clear button
 * @param evt 
 */
private void ClearActionPerformed(java.awt.event.ActionEvent evt) {                                      
    destroy();
    stop();
}                                     

/**
 * the (one and only) main method
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Wettrennen().setVisible(true);
        }
    });
}

/**
 * method that gets the Number of Players from the input field
 * @return 
 */
public int getNumPlayer() {
    String iputNumPlayer = InputNumPlayer.getText();
    int numPlayer = Integer.parseInt(iputNumPlayer);
    return numPlayer;
}

/**
 * method that initializes the number of horses referring to the number players
 */
public void initializeHorses(){
    pferde = new Vector<Pferd>();
    for (int i = 0; i < numPlayer; i++) {
        pferde.add(new Pferd(0, 0,true));
    }
}

/**
 * method which sorts every horse to a thread
 */
public void initializeThreads(){
    threads = new Vector<Thread>();
    for (int i = 0; i < pferde.size(); i++) {
        threads.add(new Thread(pferde.get(i)));
    }
}

/**
 * method starting all initialized threads
 */
public void startThreads(){
    for (int k = 0; k < threads.size(); k++) {
        System.out.println("Methode startThreads()");
        threads.get(k).start();
    }
}

/**
* the paint method which organises that the course of each horse is displayed on canvas
* after the parameter entfernung of one horse reaches 270 the game ist over,
* the aktiv parameter of each horse is set false and
* the main thread (game) is set null 
*/
public void zeichnen (){
    Graphics g = canvas.getGraphics();
    thread = new Thread();

    if (threads != null) {
        for (int j = 0; j < threads.size(); j++) {
            int zug = pferde.get(j).getZug();
            int entfernung = pferde.get(j).getEntfernung();
            g.setColor(Color.red);
            g.drawLine(50, y + j*50, 50 + zug, y + j*50);

            if (entfernung >= 270) {
                g.drawString("Horse" + threads.get(j) + "won.", 10, 10);
            System.out.println("Horse" + threads.get(j) + "won.");
            pferde.get(j).setStatus();
            thread = null;
            stop();

        } else {
            repaint();
        }
    }

    } else {
        g.drawString("Thread is null", 10, 10);
    }

}
/**
 * method quitting the game
 */
public void destroy(){
    thread = null;
}

/**
 * method for stopping and quitting the game
 */
public void stop(){
    thread = null;
    threads = null;
}

}

0 个答案:

没有答案