/*
 * 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.*;


/**
 *
 * @author  root
 */
public class NewJApplet extends javax.swing.JApplet 
implements ActionListener{
    Timer timer;
    
double tstep =1.0e-3;
int STEPSIZE =10;
double TOTAL_TIME =500.0;
double maxdev= 100.0;
double [][] a_mat,i_tens,w_mat,a_mat_der,w_mat_b,a_inv_der,
            wat_der_b,a_mat_der2,t_mat;
double[] r_vec,l_vec,w_vec,l_vec_b,w_vec_b,
         w_der_b,p_vec,dL_b,p_vec_b;


double sum,ekin,deta,dett,w_b_sq,time_now,iterations,tst2,
        epot,denom,Fx,Fy,Fz;
double dLx,dLy,dLz,dLx1,dLy1,dLz1;

int outer_loop,i,j,k,step;
DecimalFormat df;

    
    /** 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(); 
       
      
       
        
        jTextField1.setText(""+tstep);
       
       a_mat = new double[3][3];
       i_tens= new double[3][3];
       w_mat= new double[3][3];
       a_mat_der= new double[3][3];
       w_mat_b= new double[3][3];
       a_inv_der= new double[3][3];
       wat_der_b= new double[3][3];
       a_mat_der2= new double[3][3];
       t_mat= new double[3][3];
       r_vec= new double[3];
       l_vec= new double[3];
       w_vec= new double[3];
       l_vec_b= new double[3];
       w_vec_b= new double[3];
       w_der_b= new double[3]; 
       p_vec=new double[3];
       dL_b=new double[3];
       p_vec_b=new double[3];
      
       df= new DecimalFormat("0.000000");
  
 // moments of inertia               
 // none of these must be zero !!!   

  for(i=0;i<3;i++)for(j=0;j<3;j++) i_tens[i][j]=0.0;
  i_tens[0][0]=1.0; i_tens[1][1]=2.0; i_tens[2][2]=3.0;
        l_vec[0] = 1.0;
        l_vec[1] = 1.0;
        l_vec[2] = 1.0;
        
        
        Fx=0.0; Fy=0.0; Fz=1.0;



  initialize();

   jTextField2.setText(""+l_vec[0]);
   jTextField3.setText(""+l_vec[1]);
   jTextField4.setText(""+l_vec[2]);
   
   jTextField5.setText(""+Fx);
   jTextField9.setText(""+Fy);
   jTextField10.setText(""+Fz);
   
   jTextField6.setText(""+i_tens[0][0]);
   jTextField7.setText(""+i_tens[1][1]);
   jTextField8.setText(""+i_tens[2][2]);
   
  /* calculate potential energy p F */

  
  //epot=-p_vec[2]*ELSTAT;

  



    } 
    
    public void initialize()
    {
    /* initial orientation */
    for(i=0;i<3;i++) for(j=0;j<3;j++) a_mat[i][j]=(i==j ? 1.0 : 0.0);
        /* initial angular momentum */
    p_vec_b[0]=1.0; p_vec_b[1]=0.0; p_vec_b[2]=0.0;


     
        w_mat[0][0] = 0.0;
        w_mat[1][1] = 0.0;
        w_mat[2][2] = 0.0;

        iterations = 1 + (TOTAL_TIME / (tstep * STEPSIZE));
        outer_loop = 0;
        tst2 = tstep * tstep * 0.5;
        
        dLx=0; dLy=0;
  
    }
    
       public void actionPerformed(ActionEvent e) {
           
          if(outer_loop>=iterations) {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() {

        jPanel2 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextField5 = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jTextField6 = new javax.swing.JTextField();
        jTextField7 = new javax.swing.JTextField();
        jTextField8 = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jProgressBar1 = new javax.swing.JProgressBar();
        jProgressBar2 = new javax.swing.JProgressBar();
        jLabel16 = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();
        jTextField9 = new javax.swing.JTextField();
        jTextField10 = new javax.swing.JTextField();
        jLabel19 = new javax.swing.JLabel();
        jLabel20 = new javax.swing.JLabel();
        jPanel1 = new myPanel();
        jLabel21 = new javax.swing.JLabel();

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

        jTextField1.setText("jTextField1");

        jTextField2.setText("jTextField2");
        jTextField2.setMinimumSize(new java.awt.Dimension(60, 18));
        jTextField2.setPreferredSize(new java.awt.Dimension(60, 18));

        jLabel1.setText("jLabel1");

        jLabel2.setText("jLabel2");

        jTextField3.setText("jTextField3");
        jTextField3.setMinimumSize(new java.awt.Dimension(60, 18));
        jTextField3.setPreferredSize(new java.awt.Dimension(60, 18));

        jTextField4.setText("jTextField4");
        jTextField4.setMinimumSize(new java.awt.Dimension(60, 18));
        jTextField4.setPreferredSize(new java.awt.Dimension(60, 18));

        jLabel3.setText("jLabel3");

        jLabel4.setText("jLabel4");

        jTextField5.setText("jTextField5");

        jLabel5.setText("jLabel5");

        jLabel6.setText("jLabel6");

        jLabel7.setText("jLabel7");

        jLabel8.setText("timestep");

        jLabel9.setText("Fx");

        jLabel10.setText("Lx");

        jLabel11.setText("Ly");

        jLabel12.setText("Lz");

        jTextField6.setText("jTextField6");

        jTextField7.setText("jTextField7");

        jTextField8.setText("jTextField8");

        jLabel13.setText("Ix");

        jLabel14.setText("Iy");

        jLabel15.setText("Iz");

        jLabel16.setText("kinetic");

        jLabel17.setText("potential");

        jLabel18.setText("jLabel18");

        jTextField9.setText("jTextField9");

        jTextField10.setText("jTextField10");

        jLabel19.setText("Fy");

        jLabel20.setText("Fz");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 396, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel13)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jProgressBar2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(4, 4, 4)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel17)
                                    .addComponent(jLabel16)))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jTextField6, 0, 1, Short.MAX_VALUE)
                                    .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jLabel10)
                                    .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel9))
                                .addGap(45, 45, 45)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel11)
                                    .addComponent(jLabel14)
                                    .addComponent(jTextField9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel19))
                                .addGap(42, 42, 42)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel20)
                                    .addComponent(jLabel15)
                                    .addComponent(jTextField8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(jTextField10, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jLabel12)))
                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(80, 80, 80)
                                .addComponent(jLabel8)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE)
                                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                            .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE)
                                            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel18, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );

        jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jTextField10, jTextField2, jTextField3, jTextField4, jTextField5, jTextField6, jTextField7, jTextField8, jTextField9});

        jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel1, jLabel18, jLabel3, jLabel5, jLabel6, jLabel7});

        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel20)
                            .addComponent(jLabel19))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jTextField9, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextField10, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel10)
                    .addComponent(jLabel11)
                    .addComponent(jLabel12))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel13)
                    .addComponent(jLabel14)
                    .addComponent(jLabel15))
                .addGap(3, 3, 3)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel16))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jProgressBar2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel17))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel4)
                .addGap(35, 35, 35)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel18)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(23, 23, 23)
                .addComponent(jButton1)
                .addContainerGap())
        );

        jPanel2Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jTextField10, jTextField2, jTextField3, jTextField4, jTextField5, jTextField6, jTextField7, jTextField8, jTextField9});

        jPanel2Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jLabel5, jLabel6, jLabel7});

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

        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, 429, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 363, Short.MAX_VALUE)
        );

        jLabel21.setText("dipole moment along x-axis (blue)");

        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(36, 36, 36)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(59, 59, 59)
                        .addComponent(jLabel21, javax.swing.GroupLayout.PREFERRED_SIZE, 365, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(40, 40, 40)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel21))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(43, 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(jTextField1.getText());
        l_vec[0] = Double.parseDouble(jTextField2.getText());
        l_vec[1] = Double.parseDouble(jTextField3.getText());
        l_vec [2] = Double.parseDouble(jTextField4.getText());
        Fx = Double.parseDouble(jTextField5.getText());
        Fy = Double.parseDouble(jTextField9.getText());
        Fz = Double.parseDouble(jTextField10.getText());
        
        d= Double.parseDouble(jTextField6.getText());
        if(Math.abs(d)>0.0) i_tens[0][0]=d;
         
        d= Double.parseDouble(jTextField7.getText());
        if(Math.abs(d)>0.0) i_tens[1][1]=d; 
        
        d= Double.parseDouble(jTextField8.getText());
        if(Math.abs(d)>0.0) i_tens[2][2]=d;
        
      initialize();
    }//GEN-LAST:event_jButton1MouseClicked
    
    
    // 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 jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    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 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.JProgressBar jProgressBar1;
    private javax.swing.JProgressBar jProgressBar2;
    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//GEN-END:variables
    
    public class myPanel extends javax.swing.JPanel{
     
        
    public void iterate()
        {
     
             outer_loop++;
   
    for (step=0;step<STEPSIZE;step++)
{ 


  /* calculate angular momentum in body fixed cs */

  for(i=0;i<3;i++) 
  l_vec_b[i]=a_mat[0][i]*l_vec[0]
            +a_mat[1][i]*l_vec[1]
            +a_mat[2][i]*l_vec[2];


  /* calculate angular velocities in body fixed cs  */
  
  for (i=0;i<3;i++) w_vec_b[i]=l_vec_b[i]/i_tens[i][i];


  /* make W-matrix */

  w_mat_b[0][1]=-w_vec_b[2]; w_mat_b[0][2]= w_vec_b[1];
  w_mat_b[1][0]= w_vec_b[2]; w_mat_b[1][2]=-w_vec_b[0];
  w_mat_b[2][0]=-w_vec_b[1]; w_mat_b[2][1]= w_vec_b[0];

  /* calculate time derivative of angular velocity w_b */

  
  
  // transform dipole to lab system 
  for(i=0;i<3;i++) 
  p_vec[i]  =a_mat[i][0]*p_vec_b[0]
            +a_mat[i][1]*p_vec_b[1]
            +a_mat[i][2]*p_vec_b[2];
  
  
  
  dLx=p_vec[1]*Fz-p_vec[2]*Fy;
  dLy=p_vec[2]*Fx-p_vec[0]*Fz;
  dLz=p_vec[0]*Fy-p_vec[1]*Fx;
  
  for(i=0;i<3;i++)
    { sum=0.0;
      for(k=0;k<3;k++) sum=sum+w_mat_b[i][k]*l_vec_b[k];
      
      
        // include time derivative of ang. momentum 
      
   

      
         dL_b[0]=a_mat[0][0]*dLx+a_mat[1][0]*dLy+a_mat[2][0]*dLz;
         dL_b[1]=a_mat[0][1]*dLx+a_mat[1][1]*dLy+a_mat[2][1]*dLz;
         dL_b[2]=a_mat[0][2]*dLx+a_mat[1][2]*dLy+a_mat[2][2]*dLz;


      
      
      
       w_der_b[i]=(sum+dL_b[i])/i_tens[i][i];
    }
 /* calculate angular velocities at midtime */

  for (i=0;i<3;i++) w_vec_b[i]=w_vec_b[i]+w_der_b[i]*tstep*0.5;


  /* make t-matrix minus unit matrix */

  

  t_mat[0][0] = (w_vec_b[0]*w_vec_b[0]-w_vec_b[1]*w_vec_b[1]-w_vec_b[2]*w_vec_b[2])*tst2*0.5;
  t_mat[1][1] = (-w_vec_b[0]*w_vec_b[0]+w_vec_b[1]*w_vec_b[1]-w_vec_b[2]*w_vec_b[2])*tst2*0.5;
  t_mat[2][2] = (-w_vec_b[0]*w_vec_b[0]-w_vec_b[1]*w_vec_b[1]+w_vec_b[2]*w_vec_b[2])*tst2*0.5;

  t_mat[0][1] = -w_vec_b[2]*tstep+w_vec_b[0]*w_vec_b[1]*tst2;
  t_mat[1][0] =  w_vec_b[2]*tstep+w_vec_b[0]*w_vec_b[1]*tst2;
  
  t_mat[0][2] =  w_vec_b[1]*tstep+w_vec_b[0]*w_vec_b[2]*tst2;
  t_mat[2][0] = -w_vec_b[1]*tstep+w_vec_b[0]*w_vec_b[2]*tst2;

  t_mat[1][2] = -w_vec_b[0]*tstep+w_vec_b[1]*w_vec_b[2]*tst2;
  t_mat[2][1] =  w_vec_b[0]*tstep+w_vec_b[1]*w_vec_b[2]*tst2;


  denom = 1.0 +(w_vec_b[0]*w_vec_b[0]+w_vec_b[1]*w_vec_b[1]+w_vec_b[2]*w_vec_b[2])*tst2*0.5;

  /* calculate change of A-matrix  */
  
     for(i=0;i<3;i++) for(j=0;j<3;j++)
    { sum =0;
    for(k=0;k<3;k++) sum=sum+a_mat[i][k]*t_mat[k][j];
    a_mat_der[i][j] = sum;
    }

  /* calculate new A-matrix */


  for(i=0;i<3;i++) for(j=0;j<3;j++)
    { 
      a_mat[i][j]=(a_mat[i][j]+a_mat_der[i][j])/denom;
	
  
    }



  /* calculate determinant */

  for(i=0;i<3;i++) t_mat[i][i]=t_mat[i][i]+1.0;

  //dett=deter3(t_mat);

  deta=deter3(a_mat);

 // transform dipole to lab system 
  for(i=0;i<3;i++) 
  p_vec[i]  =a_mat[i][0]*p_vec_b[0]
            +a_mat[i][1]*p_vec_b[1]
            +a_mat[i][2]*p_vec_b[2];


  // calculate change of angular momentum   
  // dL/dt = pxF     with F in z-direction  
  // dLx/dt=py F; dLy/dt=-px F ; dLz/dt=0   

  
  dLx1=p_vec[1]*Fz-p_vec[2]*Fy;
  dLy1=p_vec[2]*Fx-p_vec[0]*Fz;
  dLz1=p_vec[0]*Fy-p_vec[1]*Fx;



  l_vec[0]=l_vec[0]+(dLx1+dLx)*0.5*tstep;  
  l_vec[1]=l_vec[1]+(dLy1+dLy)*0.5*tstep;
  l_vec[2]=l_vec[2]+(dLz1+dLz)*0.5*tstep;
  
  
  /* calculate kinetic energy */

  ekin=0; for(i=0;i<3;i++) ekin=ekin+0.5*w_vec_b[i]*w_vec_b[i]*i_tens[i][i];



  /* calculate potential energy p F */

  
  epot=-p_vec[0]*Fx-p_vec[1]*Fy-p_vec[2]*Fz;

  

  time_now=((double)outer_loop)*((double)STEPSIZE)*((double)tstep);

 // transform angular velocity to lab system 
  for(i=0;i<3;i++) 
  w_vec[i]  =a_mat[i][0]*w_vec_b[0]
            +a_mat[i][1]*w_vec_b[1]
            +a_mat[i][2]*w_vec_b[2];
 
}
  
        }
    public double deter3(double[][]a)
    {


    double d;

    d=a[0][0]*a[1][1]*a[2][2]+a[0][1]*a[1][2]*a[2][0] 
   +a[0][2]*a[1][0]*a[2][1]-a[0][2]*a[1][1]*a[2][0]
   -a[0][1]*a[1][0]*a[2][2]-a[0][0]*a[1][2]*a[2][1];

    return d;
}

    public void paintComponent(Graphics g)
{   int ic,ix0,iy0,ix1,iy1,ix2,iy2;
    double p,d; Color col;
    
     {super.paintComponent(g);
     g.clearRect(0,0,500,700);}
    
    iterate();
    jLabel2.setText("Det= "+deta);
    //jLabel1.setText("Ekin= "+ekin );
    //jLabel3.setText("Epot="+epot);
    
    jProgressBar1.setValue((int)(ekin*10.0+50.0));
    jProgressBar2.setValue((int)(epot*10.0+50.0));
     
    
    
            
            
    
    jLabel4.setText("Etot="+df.format(epot+ekin));
    jLabel5.setText("Lx="+df.format(l_vec[0]));
    jLabel6.setText("Ly="+df.format(l_vec[1]));
    jLabel7.setText("Lz="+df.format(l_vec[2]));
    jLabel1.setText("Ox="+df.format(w_vec[0]));
    jLabel3.setText("Oy="+df.format(w_vec[1]));
    jLabel18.setText("Oz="+df.format(w_vec[2]));
    
  
    ix0=(int) (a_mat[0][0]*100.0); 
    iy0=(int) (a_mat[1][0]*100.0); 
    
    ix1=(int) (a_mat[0][1]*100.0); 
    iy1=(int) (a_mat[1][1]*100.0);
    
    ix2=(int) (a_mat[0][2]*100.0); 
    iy2=(int) (a_mat[1][2]*100.0); 
    
    g.setColor(Color.blue);
    g.drawLine(200-ix0, 200-iy0, 200+ix0, 200+iy0);
    g.drawOval(196+ix0,196+iy0,8,8);g.fillOval(196-ix0,196-iy0,8,8);
    g.setColor(Color.red);
    g.drawLine(200-ix1, 200-iy1, 200+ix1, 200+iy1);
    g.setColor(Color.green);
    g.drawLine(200-ix2, 200-iy2, 200+ix2, 200+iy2);
    
    
  }


      

    }
    
    }



