Simulation läuft nicht!

Status
Nicht offen für weitere Antworten.

martin27

Mitglied
Hallo!

Wenn ich die Simulation mit dem Verfolgung-Knopf starte (unter NetBeans) werden zwar die Bälle gezeichnet. Leider läuft dann die run-Methode nicht ab.
Weiß da jemand vielleicht ne Lösung?

Code:
import javax.swing.JOptionPane;
import java.io.*;
import java.util.*;
import javax.swing.JFileChooser;

/*
 * Verfolgung.java
 *
 * Created on 8. Mai 2008, 14:17
 */



/**
 *
 * @author  Administrator
 */
public class Verfolgung extends javax.swing.JFrame{

    
    /** Creates new form Verfolgung */
    public Verfolgung() {
        initComponents();
        
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
  

    private void VerfolgungActionPerformed(java.awt.event.ActionEvent evt) {                                           
        if (Verfolgung.getText().equals("Verfolgung")) {
            background1.start();
            Verfolgung.setText("Stop");
        }
        else {
            
            background1.stop();
            Verfolgung.setText("Verfolgung");
            
        }
}                                          

    private void ResetActionPerformed(java.awt.event.ActionEvent evt) {                                      
       
}                                     

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
    }                                           

    private void jTextField8ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
    }                                           

    private void jTextField6ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
    }                                           

    private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
    }                                           

    private void jTextField4ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
    }                                           

    private void jTextField3ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
    }                                           

    private void jTextField10ActionPerformed(java.awt.event.ActionEvent evt) {                                             
        // TODO add your handling code here:
    }                                            

    private void jTextField5ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
    }                                           

    private void jTextField7ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
    }                                           

    private void jTextField9ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
    }                                           

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
         
    }                                          

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
    }                                          
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Verfolgung().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify                     
    private javax.swing.JButton Reset;
    private javax.swing.JButton Verfolgung;
    private Background background1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField10;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
    private javax.swing.JTextField jTextField7;
    private javax.swing.JTextField jTextField8;
    private javax.swing.JTextField jTextField9;
    // End of variables declaration                   
    
}



/*
 * Background.java
 *
 * Created on 8. Mai 2008, 14:38
 */

import java.awt.*;



/**
 *
 * @author  Administrator
 */
public class Background extends javax.swing.JPanel  implements Runnable {
    
    
    
    Thread jagd;
    
    
    double u;
    double v;
    double xQ;
    double yQ;
    double xP;
    double yP;
    double dPQ;
    double r;
   
    
    
    /** Creates new form Background */
    public Background() {
        initComponents();
        
     }    
    
    
    
    
    public void start() {
        
        xP = 100;
        yP = 50;
        xQ = 20;
        yQ = 220;
         u = 2;
         v = 2.5; 
         r=5;
        
         
      repaint();
    }
    
   public void stop() {
      if (jagd!=null)         // Falls unser Thread 'verfolgung'

         {                    // gestartet und noch nicht wieder

             jagd.stop();        // gestoppt wurde, wird er nun gestoppt

             jagd=null;        // (Einsparen von Rechnerzeit).

         }                            
            
        
    }
    
  
  /*  public boolean action(Event e, Object arg) {
        if (Verfolgung.equals(arg)) {
        if(jagd != null) {
            jagd.stop();
            jagd = null;
        }
        start();
        return true;    
        
            
    }
    */
    public void run() {
       while (true) {
           dPQ = Math.sqrt(Math.pow(xQ-xP,2) + Math.pow(yQ-yP,2));
           if (dPQ < r) {
               jagd.stop();
               jagd = null;
               
           }
           
           xQ += u;
           xP += v * (xQ-xP) / dPQ;
           yP += v * (yQ-yP) / dPQ;
           repaint();
           
           try {
               Thread.sleep(100);
           }
           catch (Exception e) {}
           
       } 
       
    }
    
    public void update (Graphics g) {
        paint(g);
    }

   // @Override
    public void paint(Graphics g) {
              super.paint(g);
        
      
       g.setColor(Color.blue);
       g.fillOval((int)Math.round(xQ-r), (int)Math.round(yQ-r), (int)(2*r), (int)(2*r));
       g.setColor(Color.red);
       g.fillOval((int)Math.round(xP-r), (int)Math.round(yP-r), (int)(2*r), (int)(2*r));
      
       
        
    }
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        setBackground(new java.awt.Color(255, 255, 255));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>                        
    
    
    // Variables declaration - do not modify                     
    // End of variables declaration                   
    
}
 
S

SlaterB

Gast
ich sehe nirgendwo Code, in dem der Thread jagd erzeugt und gestartet wird,
warum und wo sollte das deiner Meinung nach passieren?

Background selber ist kein Thread
 

martin27

Mitglied
Joup!

Hat sich schon erledigt . Nun läuft es! Jetzt habe ich ein anderes Problem. Ich möchte gern z.B. den Wert für für double xP in dem ersten Textfield eingeben lassen. Wie kann ich den eingebenen Wert an die andere Klasse übergeben, sodass die Simulation weiterhin läuft nur eben mit einer anderen Startposition?

Viel Dank schon mal im Voraus!
Gruß Martin
 
S

SlaterB

Gast
einen String kann man mit getText() auslesen, dann in double umwandeln und beliebig im Programm verwenden,
überall als Parameter übergeben oder ähnliches,

was soll man dazu sagen?
 
S

SlaterB

Gast
operation(parameter);

deine Aufgaben musst du schon alleine machen ;)
 

martin27

Mitglied
würd ich ja auch gern selber machen. nur kann ich das leider nicht , da ich die Formuleirung dafür noch nie gesehen hab. sonst würd ich ja hier nicht danach fragen.
Schade eigentlich :(
 
Status
Nicht offen für weitere Antworten.

Neue Themen


Oben