
import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import java.awt.Color;

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



/*
 * NewJApplet.java
 *
 * Created on January 14, 2008, 4:02 PM
 */



/**
 *
 * @author  root
 */
public class NewJApplet extends javax.swing.JApplet 
implements ActionListener {
    
   Timer timer;
   
   public int number_cities;
   public int window_w =300;
   public double[] xcoor,ycoor;
   public int j;
   public int[] weg,weg_neu,weg_min;
   public int i_next,i_city,p,n;

   public int i ,done=0;
   public double dx,dy,ds,x,y,r,z,L,L_neu,L_min,kT,L_aver;
   public int accepted,nsamples;
   DecimalFormat df;
   boolean showmin=false;
  
   
          
    /** Initializes the applet NewJApplet */
   
      public void actionPerformed(ActionEvent e) {
          jPanel1.repaint();
          timer.restart();
          
      }
     
    public void init() {
        try {
            java.awt.EventQueue.invokeAndWait(new Runnable() {
                public void run() {
                    initComponents();
                    number_cities=30;
                    
                    setupcities();
                    df = new DecimalFormat("##0.000");
                   
                    
                    
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        timer = new Timer(10, this);
        timer.setInitialDelay(1);
        timer.start(); 
    }
    
    /** 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();
        jProgressBar1 = new javax.swing.JProgressBar();
        jProgressBar2 = new javax.swing.JProgressBar();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jSlider1 = new javax.swing.JSlider();
        jLabel4 = new javax.swing.JLabel();
        jToggleButton1 = new javax.swing.JToggleButton();

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

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

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

        jButton2.setText("T-");
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
        });

        jLabel1.setText("T=10.0");

        jLabel2.setText("L=");

        jLabel3.setText("min=");

        jSlider1.setMaximum(50);
        jSlider1.setMinimum(5);
        jSlider1.setOrientation(javax.swing.JSlider.VERTICAL);
        jSlider1.setValue(25);
        jSlider1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                jSlider1MouseDragged(evt);
            }
        });

        jLabel4.setText("N=25");

        jToggleButton1.setText("show min");
        jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(40, 40, 40)
                                .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jToggleButton1))))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(jProgressBar2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addGap(0, 112, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(17, 17, 17)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jToggleButton1)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jProgressBar2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addContainerGap(82, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
        // TODO add your handling code here:
        kT=kT*2.0;
        jLabel1.setText("T="+df.format(kT));
        nsamples=0;
        L_aver=0;
    }//GEN-LAST:event_jButton1MouseClicked

    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseClicked
       
        kT=kT*0.5;        
        jLabel1.setText("T="+df.format(kT));
        nsamples=0;
        L_aver=0;
    }//GEN-LAST:event_jButton2MouseClicked

    private void jSlider1MouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jSlider1MouseDragged
       number_cities=jSlider1.getValue();
       jLabel4.setText("N="+number_cities);
       setupcities();
       
    }//GEN-LAST:event_jSlider1MouseDragged

    private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton1ActionPerformed
        showmin=jToggleButton1.isSelected();
    }//GEN-LAST:event_jToggleButton1ActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JProgressBar jProgressBar1;
    private javax.swing.JProgressBar jProgressBar2;
    private javax.swing.JSlider jSlider1;
    private javax.swing.JToggleButton jToggleButton1;
    // End of variables declaration//GEN-END:variables
 
   



   
       

 
 
public void setupcities() {
    
                    xcoor =new double[number_cities];
                    ycoor =new double[number_cities];
                    weg = new int[number_cities];
                    weg_neu = new int[number_cities];
                    
                    weg_min=new int[number_cities];
                    
                    
                    kT=10.0;
                   
     window_w=300;
     
     jLabel1.setText(""+kT);
     
     for (i=0;i<number_cities;i++)
    {      x=Math.random();
           xcoor[i]=x;
           x=Math.random();
           ycoor[i]=x;
    }

     for(i=0;i<number_cities;i++)
 {weg[i]=i;weg_min[i]=i;}


  L=0.0;
  for (i=0;i<number_cities-1;i++)
    { dx=xcoor[i+1]-xcoor[i];
      dy=ycoor[i+1]-ycoor[i];
      ds=Math.sqrt(dx*dx+dy*dy);
      L=L+ds;
    }
    L_min=L; L_aver=0; nsamples=0; 
    
    
}
class myPanel extends javax.swing.JPanel{
    
public void paintComponent(Graphics g)
{        int ix,iy,ix1,iy1,i2;
    
super.paintComponent(g);  
     
 {
     
   
//for (i=0;i<number_cities;i++)
//{
// ix=(int)(xcoor[i]*window_w); iy=(int)(ycoor[i]*window_w);
//g.drawOval(ix,iy,10,10);
 //}

 

          /* generate new path */
      x=Math.random();
      p=(int)(x*5);

      x=Math.random();
      n=1+(int)(x*(number_cities-5));

      if( (p>=0) && (p<number_cities) && (n>0) &&(p+n<number_cities))
	{
	  for(i=0;i<number_cities;i++)weg_neu[i]=weg[i];
          
          for(i=p;i<=p+n ;i++) {weg_neu[i]=weg[2*p+n-i];}

	  /* calculate length */


          L_neu=0.0;
          for (i=0;i<number_cities-1;i++)
          { i_city=weg_neu[i];
            i_next=weg_neu[i+1];
            dx=xcoor[i_next]-xcoor[i_city];
            dy=ycoor[i_next]-ycoor[i_city];
            ds=Math.sqrt(dx*dx+dy*dy);
            L_neu=L_neu+ds;
           }



          accepted=0;
       /* if new path is shorter then accept */
         
          if(L_neu<L) {

          for(i=0;i<number_cities;i++){weg[i]=weg_neu[i];}
          L=L_neu;
          if(L<L_min) 
              
              
          {
              L_min=L; 
              for(i=0;i<number_cities;i++)weg_min[i]=weg[i];}
          accepted=1;
          }

          else
	  /* generate random number and compare with Boltzmann-Factor */

	  { x=Math.random();

            r = Math.exp(-(L_neu-L)/kT);

            if(x<=r) {          for(i=0;i<number_cities;i++){weg[i]=weg_neu[i];}
	                         L=L_neu;
                                 accepted=1; }
          }

         
          if(accepted==1)
	    { 
          
            jProgressBar1.setValue((int)(L*150/number_cities));
            jProgressBar2.setValue((int)(L_min*150/number_cities));

	    }  /* for i */

	    } /* if accepted */


   }


L_aver=L_aver+L;
nsamples++;


jLabel2.setText("L="+df.format(L));
jLabel3.setText("min="+df.format(L_min));

 for(i=0;i<number_cities;i++)
     
 { ix=(int)(xcoor[weg[i]]*window_w);
   iy=(int)(ycoor[weg[i]]*window_w);
   
g.drawOval(ix,iy,10,10);
 }
 
 for(i=0;i<number_cities-1;i++)
      { ix=(int)(xcoor[weg[i]]*window_w)+5;
        iy=(int)(ycoor[weg[i]]*window_w)+5;
        ix1=(int)(xcoor[weg[i+1]]*window_w)+5;
        iy1=(int)(ycoor[weg[i+1]]*window_w)+5;
   
   
g.drawLine(ix,iy,ix1,iy1);}
 
 
 if(showmin)
 {
     
 g.setColor(Color.RED);    
     
 for(i=0;i<number_cities-1;i++)
      { ix=(int)(xcoor[weg_min[i]]*window_w)+5;
        iy=(int)(ycoor[weg_min[i]]*window_w)+5;
        ix1=(int)(xcoor[weg_min[i+1]]*window_w)+5;
        iy1=(int)(ycoor[weg_min[i+1]]*window_w)+5;
   
   
g.drawLine(ix,iy,ix1,iy1);}
 
 
 
 }
 
    g.setColor(Color.BLACK);     
};
}}



