/*
 * NewJApplet.java
 *
 * Created on January 23, 2008, 10:25 AM
 */

import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import java.text.*;
import java.awt.Graphics2D;
import java.awt.geom.*;



/**
 *
 * @author  root
 */
public class NewJApplet extends javax.swing.JApplet 
implements ActionListener{
    Timer timer;
    

    DecimalFormat df;
    
    double tstep,ex,ev,time;
    int STEPSIZE,outer_loop,step,zoom;
    
    double  [][] coo,coo1,c2,c3,c4,f1,f2,f3,f4;
    double []mass;
    
    /** Initializes the applet NewJApplet */
    public void init() {
        try {
            java.awt.EventQueue.invokeAndWait(new Runnable() {
                public void run() {
                    initComponents();
                }
            });
            
            


        } catch (Exception ex) {
            ex.printStackTrace();
        }
        timer = new Timer(1, this);
        timer.setInitialDelay(1);
        timer.start(); 
       
      
       
        
        timestepf.setText(""+tstep);
       
      
      
        df= new DecimalFormat("##00.000000");
        coo=new double[3][6]; 
        coo1=new double[3][6];
        c2=new double[3][6];        
        c3=new double[3][6];        
        c4=new double[3][6];
        
        f1=new double[3][6];
        f2=new double[3][6];
        f3=new double[3][6];
        f4=new double[3][6];
        
        mass = new double[3];



  initializeco();

   x2f.setText(""+coo[1][0]);  
   y2f.setText(""+coo[1][1]);
   z2f.setText(""+coo[1][2]);
   vx2f.setText(""+coo[1][3]);  
   vy2f.setText(""+coo[1][4]);
   vz2f.setText(""+coo[1][5]);
         
   x1f.setText(""+coo[0][0]);
   y1f.setText(""+coo[0][1]);
   z1f.setText(""+coo[0][2]);
   vx1f.setText(""+coo[0][3]);
   vy1f.setText(""+coo[0][4]);
   vz1f.setText(""+coo[0][5]);
   
   x3f.setText(""+coo[2][0]);
   y3f.setText(""+coo[2][1]);
   z3f.setText(""+coo[2][2]);
   vx3f.setText(""+coo[2][3]);
   vy3f.setText(""+coo[2][4]);
   vz3f.setText(""+coo[2][5]);
   
   m1f.setText(""+mass[0]);
   m2f.setText(""+mass[1]);
   m3f.setText(""+mass[2]);
   
   timestepf.setText(""+tstep);
   //stepf.setText(""+STEPSIZE);



    } 
    
    public void initializeco()
    {
    /* initial orientation */
    coo[0][0]=6.0; coo[0][1]=0.0; coo[0][2]=0.0;
    coo[0][3]=0.0; coo[0][4]=0.5; coo[0][5]=0.0;
        
    coo[1][0]=7.0; coo[1][1]=0.0; coo[1][2]=0.0;
    coo[1][3]=0.0; coo[1][4]=0.77; coo[1][5]=0.1;
    
    coo[2][0]=0.0; coo[2][1]=0.0; coo[2][2]=0.0;
    coo[2][3]=0.0; coo[2][4]=0.0; coo[2][5]=0.0;
    
    mass[0]=0.1; mass[1]=0.0; mass[2]=1.0;
    
    tstep=0.001; STEPSIZE=100;
    
    zoom=10;
    zoomf.setValue(zoom);
    }
    
    public void initialize()
    {outer_loop=0;
     time=0.0;}
       public void actionPerformed(ActionEvent e) {
           
          
          if(outer_loop>10000) return;
          jPanel1.repaint();
          timer.setInitialDelay(10);
          timer.restart();
          
      }
    
    
    /** This method is called from within the init() method 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">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new myPanel();
        jPanel2 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        timestepf = new javax.swing.JTextField();
        x2f = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        y3f = new javax.swing.JTextField();
        y2f = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        x1f = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        x3f = new javax.swing.JTextField();
        z2f = new javax.swing.JTextField();
        z3f = new javax.swing.JTextField();
        jLabel17 = new javax.swing.JLabel();
        y1f = new javax.swing.JTextField();
        z1f = new javax.swing.JTextField();
        jLabel19 = new javax.swing.JLabel();
        jLabel20 = new javax.swing.JLabel();
        vx1f = new javax.swing.JTextField();
        vx2f = new javax.swing.JTextField();
        vx3f = new javax.swing.JTextField();
        vy3f = new javax.swing.JTextField();
        vy2f = new javax.swing.JTextField();
        vy1f = new javax.swing.JTextField();
        vz1f = new javax.swing.JTextField();
        vz2f = new javax.swing.JTextField();
        vz3f = new javax.swing.JTextField();
        jLabel21 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel22 = new javax.swing.JLabel();
        m3f = new javax.swing.JTextField();
        m1f = new javax.swing.JTextField();
        m2f = new javax.swing.JTextField();
        jLabel23 = new javax.swing.JLabel();
        timel = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();
        jSlider1 = new javax.swing.JSlider();
        jLabel2 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        zoomf = new javax.swing.JSlider();
        jLabel4 = new javax.swing.JLabel();

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel1.setDoubleBuffered(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 417, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 456, Short.MAX_VALUE)
        );

        jButton1.setText("Start");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        timestepf.setText("jTextField1");

        x2f.setText("jTextField2");
        x2f.setMaximumSize(new java.awt.Dimension(30, 10));
        x2f.setMinimumSize(new java.awt.Dimension(30, 10));
        x2f.setPreferredSize(new java.awt.Dimension(30, 10));
        x2f.setVerifyInputWhenFocusTarget(false);

        jLabel1.setText("jLabel1");

        y3f.setText("jTextField3");
        y3f.setMaximumSize(new java.awt.Dimension(30, 10));
        y3f.setMinimumSize(new java.awt.Dimension(30, 10));
        y3f.setPreferredSize(new java.awt.Dimension(30, 10));
        y3f.setVerifyInputWhenFocusTarget(false);

        y2f.setText("jTextField4");
        y2f.setMaximumSize(new java.awt.Dimension(30, 10));
        y2f.setMinimumSize(new java.awt.Dimension(30, 10));
        y2f.setPreferredSize(new java.awt.Dimension(30, 10));
        y2f.setVerifyInputWhenFocusTarget(false);

        jLabel3.setText("jLabel3");

        x1f.setText("jTextField5");
        x1f.setMaximumSize(new java.awt.Dimension(30, 10));
        x1f.setMinimumSize(new java.awt.Dimension(30, 10));
        x1f.setPreferredSize(new java.awt.Dimension(30, 10));
        x1f.setVerifyInputWhenFocusTarget(false);

        jLabel7.setText("jLabel7");
        jLabel7.setMaximumSize(new java.awt.Dimension(100, 16));
        jLabel7.setMinimumSize(new java.awt.Dimension(100, 16));
        jLabel7.setPreferredSize(new java.awt.Dimension(100, 16));

        jLabel8.setText("timestep");

        jLabel9.setText("vx");

        x3f.setText("jTextField6");
        x3f.setMaximumSize(new java.awt.Dimension(30, 10));
        x3f.setMinimumSize(new java.awt.Dimension(30, 10));
        x3f.setPreferredSize(new java.awt.Dimension(30, 10));
        x3f.setVerifyInputWhenFocusTarget(false);

        z2f.setText("jTextField7");
        z2f.setMaximumSize(new java.awt.Dimension(30, 10));
        z2f.setMinimumSize(new java.awt.Dimension(30, 10));
        z2f.setPreferredSize(new java.awt.Dimension(30, 10));
        z2f.setVerifyInputWhenFocusTarget(false);

        z3f.setText("jTextField8");
        z3f.setMaximumSize(new java.awt.Dimension(30, 10));
        z3f.setMinimumSize(new java.awt.Dimension(30, 10));
        z3f.setPreferredSize(new java.awt.Dimension(30, 10));
        z3f.setVerifyInputWhenFocusTarget(false);

        jLabel17.setText("energy");

        y1f.setText("jTextField9");
        y1f.setMaximumSize(new java.awt.Dimension(30, 10));
        y1f.setMinimumSize(new java.awt.Dimension(30, 10));
        y1f.setPreferredSize(new java.awt.Dimension(30, 10));
        y1f.setVerifyInputWhenFocusTarget(false);

        z1f.setText("jTextField10");
        z1f.setMaximumSize(new java.awt.Dimension(30, 10));
        z1f.setMinimumSize(new java.awt.Dimension(30, 10));
        z1f.setPreferredSize(new java.awt.Dimension(30, 10));
        z1f.setVerifyInputWhenFocusTarget(false);

        jLabel19.setText("vy");

        jLabel20.setText("vz");

        vx1f.setText("jTextField5");
        vx1f.setMaximumSize(new java.awt.Dimension(30, 10));
        vx1f.setMinimumSize(new java.awt.Dimension(30, 10));
        vx1f.setPreferredSize(new java.awt.Dimension(30, 10));
        vx1f.setVerifyInputWhenFocusTarget(false);

        vx2f.setText("jTextField2");
        vx2f.setMaximumSize(new java.awt.Dimension(30, 10));
        vx2f.setMinimumSize(new java.awt.Dimension(30, 10));
        vx2f.setPreferredSize(new java.awt.Dimension(30, 10));
        vx2f.setVerifyInputWhenFocusTarget(false);

        vx3f.setText("jTextField6");
        vx3f.setMaximumSize(new java.awt.Dimension(30, 10));
        vx3f.setMinimumSize(new java.awt.Dimension(30, 10));
        vx3f.setPreferredSize(new java.awt.Dimension(30, 10));
        vx3f.setVerifyInputWhenFocusTarget(false);

        vy3f.setText("jTextField3");
        vy3f.setMaximumSize(new java.awt.Dimension(30, 10));
        vy3f.setMinimumSize(new java.awt.Dimension(30, 10));
        vy3f.setPreferredSize(new java.awt.Dimension(30, 10));
        vy3f.setVerifyInputWhenFocusTarget(false);

        vy2f.setText("jTextField4");
        vy2f.setMaximumSize(new java.awt.Dimension(30, 10));
        vy2f.setMinimumSize(new java.awt.Dimension(30, 10));
        vy2f.setPreferredSize(new java.awt.Dimension(30, 10));
        vy2f.setVerifyInputWhenFocusTarget(false);

        vy1f.setText("jTextField9");
        vy1f.setMaximumSize(new java.awt.Dimension(30, 10));
        vy1f.setMinimumSize(new java.awt.Dimension(30, 10));
        vy1f.setPreferredSize(new java.awt.Dimension(30, 10));
        vy1f.setVerifyInputWhenFocusTarget(false);

        vz1f.setText("jTextField10");
        vz1f.setMaximumSize(new java.awt.Dimension(30, 10));
        vz1f.setMinimumSize(new java.awt.Dimension(30, 10));
        vz1f.setPreferredSize(new java.awt.Dimension(30, 10));
        vz1f.setVerifyInputWhenFocusTarget(false);

        vz2f.setText("jTextField7");
        vz2f.setMaximumSize(new java.awt.Dimension(30, 10));
        vz2f.setMinimumSize(new java.awt.Dimension(30, 10));
        vz2f.setPreferredSize(new java.awt.Dimension(30, 10));
        vz2f.setVerifyInputWhenFocusTarget(false);

        vz3f.setText("jTextField8");
        vz3f.setMaximumSize(new java.awt.Dimension(30, 10));
        vz3f.setMinimumSize(new java.awt.Dimension(30, 10));
        vz3f.setPreferredSize(new java.awt.Dimension(30, 10));
        vz3f.setVerifyInputWhenFocusTarget(false);

        jLabel21.setText("y");

        jLabel10.setText("x");

        jLabel22.setText("z");

        m3f.setText("jTextField8");
        m3f.setMaximumSize(new java.awt.Dimension(30, 10));
        m3f.setMinimumSize(new java.awt.Dimension(30, 10));
        m3f.setPreferredSize(new java.awt.Dimension(30, 10));
        m3f.setVerifyInputWhenFocusTarget(false);

        m1f.setText("jTextField8");
        m1f.setMaximumSize(new java.awt.Dimension(30, 10));
        m1f.setMinimumSize(new java.awt.Dimension(30, 10));
        m1f.setPreferredSize(new java.awt.Dimension(30, 10));
        m1f.setVerifyInputWhenFocusTarget(false);

        m2f.setText("jTextField8");
        m2f.setMaximumSize(new java.awt.Dimension(30, 10));
        m2f.setMinimumSize(new java.awt.Dimension(30, 10));
        m2f.setPreferredSize(new java.awt.Dimension(30, 10));
        m2f.setVerifyInputWhenFocusTarget(false);

        jLabel23.setText("m");

        timel.setText("jLabel7");

        jLabel18.setText("time");

        jSlider1.setMaximum(1000);
        jSlider1.setMinimum(1);
        jSlider1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                jSlider1MouseDragged(evt);
            }
        });

        jLabel2.setText("animation speed");

        jLabel5.setFont(new java.awt.Font("Dialog", 1, 24)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(0, 0, 255));
        jLabel5.setText("*");

        jLabel6.setFont(new java.awt.Font("Dialog", 1, 24)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(255, 0, 51));
        jLabel6.setText("*");

        jLabel11.setFont(new java.awt.Font("Dialog", 1, 24)); // NOI18N
        jLabel11.setForeground(new java.awt.Color(0, 204, 0));
        jLabel11.setText("*");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addGap(103, 103, 103))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                                        .addComponent(x3f, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(y3f, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(z3f, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(vx3f, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(vy3f, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(vz3f, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(m3f, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(jPanel2Layout.createSequentialGroup()
                                                .addComponent(x1f, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(y1f, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                                .addComponent(x2f, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(y2f, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(jPanel2Layout.createSequentialGroup()
                                                .addComponent(z2f, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(vx2f, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(vy2f, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(vz2f, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(m2f, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                                            .addGroup(jPanel2Layout.createSequentialGroup()
                                                .addComponent(z1f, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(vx1f, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(vy1f, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(vz1f, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(m1f, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addGap(84, 84, 84)
                                        .addComponent(jLabel21)
                                        .addGap(45, 45, 45)
                                        .addComponent(jLabel22)
                                        .addGap(48, 48, 48)
                                        .addComponent(jLabel9)
                                        .addGap(40, 40, 40)
                                        .addComponent(jLabel19)
                                        .addGap(45, 45, 45)
                                        .addComponent(jLabel20)
                                        .addGap(39, 39, 39)
                                        .addComponent(jLabel23))))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(31, 31, 31)
                                .addComponent(jLabel10))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(20, 20, 20)
                                .addComponent(jLabel8)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(timestepf, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jLabel18)
                                .addGap(18, 18, 18)
                                .addComponent(timel, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(0, 18, Short.MAX_VALUE))))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel17)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 215, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(103, 103, 103))
                            .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(32, 32, 32))
        );

        jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {x2f, x3f, y1f, y2f, y3f, z1f, z2f, z3f});

        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(47, 47, 47)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(timestepf, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addGap(35, 35, 35)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(jLabel21)
                    .addComponent(jLabel20)
                    .addComponent(jLabel23)
                    .addComponent(jLabel19)
                    .addComponent(jLabel9)
                    .addComponent(jLabel22))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(y1f, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(x1f, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(z1f, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(vx1f, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(vy1f, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(vz1f, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(m1f, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(z2f, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(y2f, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(x2f, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(vx2f, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(vy2f, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(vz2f, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(m2f, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(x3f, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(y3f, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(z3f, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(vx3f, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(vy3f, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(vz3f, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(m3f, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(46, 46, 46)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel17)
                    .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel18)
                            .addComponent(timel)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
                        .addComponent(jButton1)))
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addComponent(jLabel2))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(32, 32, 32))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(135, 135, 135)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel11)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {x1f, x2f, x3f, y1f, y2f, y3f, z1f, z2f, z3f});

        jPanel2Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jLabel1, jLabel3});

        zoomf.setMaximum(200);
        zoomf.setMinimum(10);

        jLabel4.setText("zoom");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(7, 7, 7)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(51, 51, 51)
                        .addComponent(jLabel4)
                        .addGap(33, 33, 33)
                        .addComponent(zoomf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(266, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(zoomf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(614, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
       double d;
      tstep=Double.parseDouble(timestepf.getText());
      
      
        
        coo[0][0] = Double.parseDouble(x1f.getText());
        coo[0][1] = Double.parseDouble(y1f.getText());
        coo[0][2] = Double.parseDouble(z1f.getText());
        coo[0][3] = Double.parseDouble(vx1f.getText());
        coo[0][4] = Double.parseDouble(vy1f.getText());
        coo[0][5] = Double.parseDouble(vz1f.getText());
        
        coo[1][0] = Double.parseDouble(x2f.getText());
        coo[1][1] = Double.parseDouble(y2f.getText());
        coo[1][2] = Double.parseDouble(z2f.getText());
        coo[1][3] = Double.parseDouble(vx2f.getText());
        coo[1][4] = Double.parseDouble(vy2f.getText());
        coo[1][5] = Double.parseDouble(vz2f.getText());
        
        coo[2][0] = Double.parseDouble(x3f.getText());
        coo[2][1] = Double.parseDouble(y3f.getText());
        coo[2][2] = Double.parseDouble(z3f.getText());
        coo[2][3] = Double.parseDouble(vx3f.getText());
        coo[2][4] = Double.parseDouble(vy3f.getText());
        coo[2][5] = Double.parseDouble(vz3f.getText());
        mass[0] = Double.parseDouble(m1f.getText());
        mass[1] = Double.parseDouble(m2f.getText());
        mass[2] = Double.parseDouble(m3f.getText());
        
        //STEPSIZE=(int)Double.parseDouble(stepf.getText());
      initialize();
    }//GEN-LAST:event_jButton1MouseClicked

    private void jSlider1MouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jSlider1MouseDragged
        STEPSIZE=jSlider1.getValue();// TODO add your handling code here:
    }//GEN-LAST:event_jSlider1MouseDragged
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    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.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JSlider jSlider1;
    private javax.swing.JTextField m1f;
    private javax.swing.JTextField m2f;
    private javax.swing.JTextField m3f;
    private javax.swing.JLabel timel;
    private javax.swing.JTextField timestepf;
    private javax.swing.JTextField vx1f;
    private javax.swing.JTextField vx2f;
    private javax.swing.JTextField vx3f;
    private javax.swing.JTextField vy1f;
    private javax.swing.JTextField vy2f;
    private javax.swing.JTextField vy3f;
    private javax.swing.JTextField vz1f;
    private javax.swing.JTextField vz2f;
    private javax.swing.JTextField vz3f;
    private javax.swing.JTextField x1f;
    private javax.swing.JTextField x2f;
    private javax.swing.JTextField x3f;
    private javax.swing.JTextField y1f;
    private javax.swing.JTextField y2f;
    private javax.swing.JTextField y3f;
    private javax.swing.JTextField z1f;
    private javax.swing.JTextField z2f;
    private javax.swing.JTextField z3f;
    private javax.swing.JSlider zoomf;
    // End of variables declaration//GEN-END:variables
    
    public class myPanel extends javax.swing.JPanel{
     
        
        
    public void grad(double [][] c,double[][] f)
        { int i,j; double sx,sy,sz,dx,dy,dz,d2,d3,fac;
          
          for(i=0;i<3;i++)
          { sx=0.0; sy=0.0; sz=0.0;
            for(j=0;j<3;j++)
                if(i!=j) 
                {
                    dx=c[i][0]-c[j][0];
                    dy=c[i][1]-c[j][1];
                    dz=c[i][2]-c[j][2];
                    
                    d2=dx*dx+dy*dy+dz*dz;
                    d3=d2*Math.sqrt(d2);
                    fac=mass[j]/d3;
                    
                    sx=sx-dx*fac;
                    sy=sy-dy*fac;
                    sz=sz-dz*fac;
                }
            f[i][3]=sx;
            f[i][4]=sy;
            f[i][5]=sz;
            f[i][0]=c[i][3];
            f[i][1]=c[i][4];
            f[i][2]=c[i][5];
          }
            
    }
    public void advance(double [][] c,double[][] f,double [][]c1,double step)
    {int i,j;
    
     for(i=0;i<3;i++) for(j=0;j<6;j++) c1[i][j]=c[i][j]+step*f[i][j];        
    }
        
    
    public void rk4d(double [][] coo,double[][]coo1)
    {int i,j;
      
    grad(coo,f1);
    advance(coo,f1,c2,tstep);
    grad(c2,f2);
    advance(c2,f2,c3,tstep);
    grad(c3,f3);
    advance(c3,f3,c4,tstep*2.0);
    grad(c4,f4);
    
    for (i=0;i<3;i++)for(j=0;j<6;j++)
    coo1[i][j]=coo[i][j]+2.0*tstep*(f1[i][j]+2.0*f2[i][j]+2.0*f3[i][j]+f4[i][j])/6.0;    
    
    
    
    }
    
    
    
    public void rk4(double [][]coo)
    { int i,j;
      
    grad(coo,f1);
    advance(coo,f1,c2,tstep*0.5);
    grad(c2,f2);
    advance(c2,f2,c3,tstep*0.5);
    grad(c3,f3);
    advance(c3,f3,c4,tstep);
    grad(c4,f4);
    
    for (i=0;i<3;i++)for(j=0;j<6;j++)
    coo[i][j]=coo[i][j]+tstep*(f1[i][j]+2.0*f2[i][j]+2.0*f3[i][j]+f4[i][j])/6.0;    
    
          
    }
    
    
    
    public double epot()
    {int i,j; double e,es,dx,dy,dz;
     
     es=0.0;
     for (i=0;i<2;i++)for(j=i+1;j<3;j++)
     
     {
     dx=coo[i][0]-coo[j][0];
     dy=coo[i][1]-coo [j][1];
     dz=coo[i][2]-coo[j][2];
     
     es=es-mass[i]*mass[j]/Math.sqrt(dx*dx+dy*dy+dz*dz);
     
     }
    return es;
    
    }
    
    public double ekin()
    {int i,j; double e,es;
     es=0.0;
     for (i=0;i<3;i++)
     { e=0.0;
       for(j=3;j<6;j++) e=e+coo[i][j]*coo[i][j];
       e=e*mass[i];
       es=es+e;
     
    } return es*0.5;
    }
    
    public void iterate()
        {double ep,ek,del,delmax,delv,delmaxv,t; int i,j;
     
             outer_loop++;
   
    for (step=0;step<STEPSIZE-1;step++)
{ rk4(coo);rk4(coo);
    time=time+2*tstep; }
             
    
        
        


rk4d(coo,coo1);
rk4(coo);
rk4(coo);

 time=time+2*tstep;
 timel.setText(""+df.format(time));

//jTextArea1.append("  "+coo[1][0]+"  "+coo1[1][0]+"\n");
//jTextArea1.append("" + ekin()+"\n");
delmax=0.0; delmaxv=0.0;

for (i=0;i<3;i++)for(j=0;j<3;j++)
{del=Math.abs(coo[i][j]-coo1[i][j]);
 if(del>delmax)delmax=del;
}

for (i=0;i<3;i++)for(j=4;j<6;j++)
{delv=Math.abs(coo[i][j]-coo1[i][j]);
 if(delv>delmaxv)delmaxv=delv;
}

jLabel1.setText("ln error x  "+df.format(Math.log10(delmax/30.0)));
jLabel3.setText("ln error v  "+df.format(Math.log10(delmaxv/30.0)));

   
ep=epot();
ek=ekin();




jLabel7.setText(""+(ek+ep));
    
  
        }
   

    public void paintComponent(Graphics g)
{   int ic,ix0,iy0,ix1,iy1,ix2,iy2;
    double p,d,xc,yc,zc,m; Color col;
    
    Graphics2D g2=(Graphics2D) g;
    
     {super.paintComponent(g);
     g.clearRect(0,0,500,700);}
    
    iterate();
   
    
   // jProgressBar1.setValue((int)(ekin*10.0+50.0));
   // jProgressBar2.setValue((int)(epot*10.0+50.0));
     
    
    
            
            
    
   
    zoom=zoomf.getValue();
    
    m=mass[0]+mass[1]+mass[2];
    xc=coo[0][0]*mass[0]+coo[1][0]*mass[1]+coo[2][0]*mass[2];
    yc=coo[0][1]*mass[0]+coo[1][1]*mass[1]+coo[2][1]*mass[2];
    zc=coo[0][2]*mass[0]+coo[1][2]*mass[1]+coo[2][2]*mass[2];
  
    xc=xc/m; yc=yc/m; zc=zc/m;
   
    g.setColor(Color.blue);    
    g2.fill(new Ellipse2D.Double(200+zoom*(coo[0][0]-xc),
             200+zoom*(coo[0][1]-yc),10,10));

    g.setColor(Color.red);    
    g2.fill(new Ellipse2D.Double(200+zoom*(coo[1][0]-xc),
            200+zoom*(coo[1][1]-yc),10,10));
    
    g.setColor(Color.green);        
    g2.fill(new Ellipse2D.Double(200+zoom*(coo[2][0]-xc),
            200+zoom*(coo[2][1]-yc),10,10));
    
    
  }


      

    }
    
    }



