1    
2    /*
3     * GUI.java
4     *
5     * Created on Apr 6, 2009, 7:57:13 PM
6     */
7    
8    package geneticalgorithm;
9    
10   import java.awt.Graphics2D;
11   
12   /**
13    *
14    * @author Nick Peters
15    */
16   public class GUI extends javax.swing.JFrame {
17   
18       /** Creates new form Client */
19       private GeneticAlgorithm ga;
20       public GUI() {
21           ga = new GeneticAlgorithm();
22           initComponents();
23       }
24       /** This method is called from within the constructor to
25        * initialize the form.
26        * WARNING: Do NOT modify this code. The content of this method is
27        * always regenerated by the Form Editor.
28        */
29       @SuppressWarnings("unchecked")
30       // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
31       private void initComponents() {
32   
33           method = new javax.swing.ButtonGroup();
34           jLabel1 = new javax.swing.JLabel();
35           jLabel2 = new javax.swing.JLabel();
36           methOrder = new javax.swing.JRadioButton();
37           methDistance = new javax.swing.JRadioButton();
38           rate = new javax.swing.JComboBox();
39           jButton1 = new javax.swing.JButton();
40           jLabel3 = new javax.swing.JLabel();
41           jLabel4 = new javax.swing.JLabel();
42           lblTour = new javax.swing.JLabel();
43           lblDistance = new javax.swing.JLabel();
44           jButton2 = new javax.swing.JButton();
45   
46           setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
47   
48           jLabel1.setText("Rate:");
49   
50           jLabel2.setText("Method:");
51   
52           method.add(methOrder);
53           methOrder.setSelected(true);
54           methOrder.setText("Order");
55           methOrder.addActionListener(new java.awt.event.ActionListener() {
56               public void actionPerformed(java.awt.event.ActionEvent evt) {
57                   methOrderActionPerformed(evt);
58               }
59           });
60   
61           method.add(methDistance);
62           methDistance.setText("Distance");
63           methDistance.addActionListener(new java.awt.event.ActionListener() {
64               public void actionPerformed(java.awt.event.ActionEvent evt) {
65                   methDistanceActionPerformed(evt);
66               }
67           });
68   
69           rate.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "0.00", "0.02", "0.04", "0.06" }));
70   
71           jButton1.setText("Run");
72           jButton1.addActionListener(new java.awt.event.ActionListener() {
73               public void actionPerformed(java.awt.event.ActionEvent evt) {
74                   jButton1ActionPerformed(evt);
75               }
76           });
77   
78           jLabel3.setText("Tour:");
79   
80           jLabel4.setText("Distance:");
81   
82           jButton2.setText("Reset");
83           jButton2.addActionListener(new java.awt.event.ActionListener() {
84               public void actionPerformed(java.awt.event.ActionEvent evt) {
85                   jButton2ActionPerformed(evt);
86               }
87           });
88   
89           org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
90           getContentPane().setLayout(layout);
91           layout.setHorizontalGroup(
92               layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
93               .add(layout.createSequentialGroup()
94                   .addContainerGap()
95                   .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
96                       .add(layout.createSequentialGroup()
97                           .add(jLabel4)
98                           .add(18, 18, 18)
99                           .add(lblDistance, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 199, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
100                      .add(layout.createSequentialGroup()
101                          .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
102                              .add(layout.createSequentialGroup()
103                                  .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
104                                      .add(jLabel2)
105                                      .add(jLabel1))
106                                  .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
107                                  .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
108                                      .add(rate, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
109                                      .add(layout.createSequentialGroup()
110                                          .add(methOrder)
111                                          .add(18, 18, 18)
112                                          .add(methDistance))))
113                              .add(layout.createSequentialGroup()
114                                  .add(jButton1)
115                                  .add(18, 18, 18)
116                                  .add(jButton2)))
117                          .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 199, Short.MAX_VALUE))
118                      .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
119                          .add(jLabel3)
120                          .add(6, 6, 6)
121                          .add(lblTour, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE)))
122                  .add(44, 44, 44))
123          );
124          layout.setVerticalGroup(
125              layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
126              .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
127                  .addContainerGap(263, Short.MAX_VALUE)
128                  .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
129                      .add(lblTour, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 19, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
130                      .add(jLabel3))
131                  .add(11, 11, 11)
132                  .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
133                      .add(jLabel4)
134                      .add(lblDistance, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 17, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
135                  .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
136                  .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
137                      .add(jLabel1)
138                      .add(rate, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
139                  .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
140                  .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
141                      .add(jLabel2)
142                      .add(methOrder)
143                      .add(methDistance))
144                  .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
145                  .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
146                      .add(jButton1)
147                      .add(jButton2))
148                  .add(17, 17, 17))
149          );
150  
151          pack();
152      }// </editor-fold>//GEN-END:initComponents
153  
154      private void methOrderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_methOrderActionPerformed
155          
156  }//GEN-LAST:event_methOrderActionPerformed
157  
158      private void methDistanceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_methDistanceActionPerformed
159  
160  }//GEN-LAST:event_methDistanceActionPerformed
161  
162      private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
163          double r = Double.parseDouble(rate.getSelectedItem().toString());
164          int m = (methOrder.isSelected()) ? GeneticAlgorithm.ORDER : GeneticAlgorithm.DISTANCE;
165          
166          Tour best;
167          ga.run(r, m);
168          best = ga.getBestTour();
169          String tour = "";
170          for(int j = 0; j < GeneticAlgorithm.N; j++)
171             tour = tour + best.getTour()[j] + " ";
172          lblDistance.setText(" " + best.getDistance());
173          lblTour.setText(tour);
174          TourDisplay td = ga.getPanel();
175          this.add(td); // add shapesJPanel to frame
176          td.setSize(TourDisplay.X_SIZE + 10, TourDisplay.Y_SIZE + 10);
177          td.setVisible(true); // display frame
178      }//GEN-LAST:event_jButton1ActionPerformed
179  
180      private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
181          ga = new GeneticAlgorithm();
182          lblDistance.setText("");
183          lblTour.setText("");
184      }//GEN-LAST:event_jButton2ActionPerformed
185  
186  
187      // Variables declaration - do not modify//GEN-BEGIN:variables
188      private javax.swing.JButton jButton1;
189      private javax.swing.JButton jButton2;
190      private javax.swing.JLabel jLabel1;
191      private javax.swing.JLabel jLabel2;
192      private javax.swing.JLabel jLabel3;
193      private javax.swing.JLabel jLabel4;
194      private javax.swing.JLabel lblDistance;
195      private javax.swing.JLabel lblTour;
196      private javax.swing.JRadioButton methDistance;
197      private javax.swing.JRadioButton methOrder;
198      private javax.swing.ButtonGroup method;
199      private javax.swing.JComboBox rate;
200      // End of variables declaration//GEN-END:variables
201  
202  }
203