DoxygenExample.java

Go to the documentation of this file.
00001 
00024 import java.awt.*;
00025 import java.awt.event.*;
00026 import java.util.*;
00027 
00028 import sorting.*;
00029 
00030 
00046 public class DoxygenExample extends Frame
00047 {
00048 
00049   //three panels, used for the visualisation of the algorithm processes
00050   private Panel ssPanel = new Panel(null);
00051   private Panel isPanel = new Panel(null);
00052   private Panel bsPanel = new Panel(null);
00053   
00054   //three labels for the algorithm names
00055   private Label label1 = new Label();
00056   private Label label2 = new Label();
00057   private Label label3 = new Label();
00058   
00059   //a panel, containig the buttons
00060   private Panel buttonPanel = new Panel(null);
00061   
00062   private Button startButton = new Button();
00063   private Button randomizeButton = new Button();
00064   private Button randStartButton = new Button();
00065   private Button ssButton = new Button();
00066   private Button isButton = new Button();
00067   private Button bsButton = new Button();
00068   private Button exitButton = new Button();
00069 
00070   //the TimeLabel are used to show the runtime of the algorithms
00071   private Label ssTimeLabel = new Label();
00072   private Label isTimeLabel = new Label();
00073   private Label bsTimeLabel = new Label();
00074 
00075   private int[] ssArray = new int[210];    // the Array for the selectionsort algorithm
00076   private int[] isArray = new int[210];    //           for the insertionsort
00077   private int[] bsArray = new int[210];    //           for the bubblesort
00078 
00079 
00087   public DoxygenExample(String title) 
00088   {
00089     super(title);
00090 
00091     addWindowListener(new WindowAdapter() {
00092       public void windowClosing(WindowEvent evt) { System.exit(0); }
00093     });
00094     
00095     int frameWidth = 900;
00096     int frameHeight = 400;
00097     setSize(frameWidth, frameHeight);
00098     Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
00099     int x = (d.width - getSize().width) / 2;
00100     int y = (d.height - getSize().height) / 2 ;
00101     setLocation(x, y);
00102     Panel cp = new Panel(null);
00103     add(cp);
00104 
00105     this.setBackground(new Color(240, 240, 240));  
00106 
00107     ssPanel.setBounds(16, 40, 250, 200);
00108     cp.add(ssPanel);
00109     isPanel.setBounds(324, 40, 250, 200);
00110     cp.add(isPanel);
00111     bsPanel.setBounds(632, 40, 250, 200);
00112     cp.add(bsPanel);
00113     label1.setBounds(16, 16, 90, 16);
00114     label1.setText("Selectionsort");
00115     label1.setFont(new Font("MS Sans Serif", Font.PLAIN, 13));
00116     cp.add(label1);
00117     label2.setBounds(324, 16, 90, 16);
00118     label2.setText("Insertionsort");
00119     label2.setFont(new Font("MS Sans Serif", Font.PLAIN, 13));
00120     cp.add(label2);
00121     label3.setBounds(632, 16, 90, 16);
00122     label3.setText("Bubblesort");
00123     label3.setFont(new Font("MS Sans Serif", Font.PLAIN, 13));
00124     cp.add(label3);
00125     buttonPanel.setBounds(16, 280, 860, 81);
00126     cp.add(buttonPanel);
00127     startButton.setBounds(140, 48, 110, 25);
00128     randomizeButton.setBounds(16, 48, 110, 25);
00129     randStartButton.setBounds(380, 48, 110, 25);
00130     ssButton.setBounds(16,5,230,25);
00131     ssButton.setLabel("start");
00132     startButton.setLabel("start sorting");
00133     randomizeButton.setLabel("ramdomize");
00134     randStartButton.setLabel("randomized start");
00135     buttonPanel.add(randStartButton);
00136     buttonPanel.add(startButton);
00137     buttonPanel.add(randomizeButton);
00138     buttonPanel.add(ssButton);
00139     buttonPanel.add(isButton);
00140     buttonPanel.add(bsButton);
00141 
00142     ssButton.setBounds(16,10,220,25);
00143     ssButton.setLabel("start selectionsort");
00144     isButton.setBounds(324,10,220,25);
00145     isButton.setLabel("start insertionsort");
00146     bsButton.setBounds(632,10,220,25);
00147     bsButton.setLabel("start bubblesort");
00148 
00149     ssPanel.setBackground(new Color(128,128,128));
00150     isPanel.setBackground(new Color(128,128,128));
00151     bsPanel.setBackground(new Color(128,128,128));
00152     buttonPanel.setBackground(new Color(192,192,192));
00153 
00154     
00155     randomizeButton.addActionListener(new ActionListener() {
00156       public void actionPerformed(ActionEvent evt) {
00157         randomizeButtonActionPerformed(evt);
00158       }
00159     });
00160 
00161     randStartButton.addActionListener(new ActionListener() {
00162       public void actionPerformed(ActionEvent evt) {
00163         randStartButtonActionPerformed(evt);
00164       }
00165     });
00166 
00167     startButton.addActionListener(new ActionListener() {
00168       public void actionPerformed(ActionEvent evt) {
00169         startButtonActionPerformed(evt);
00170       }
00171     });
00172 
00173     ssButton.addActionListener(new ActionListener() {
00174       public void actionPerformed(ActionEvent evt) {
00175         ssButtonActionPerformed(evt);
00176       }
00177     });
00178 
00179     isButton.addActionListener(new ActionListener() {
00180       public void actionPerformed(ActionEvent evt) {
00181         isButtonActionPerformed(evt);
00182       }
00183     });
00184 
00185     bsButton.addActionListener(new ActionListener() {
00186       public void actionPerformed(ActionEvent evt) {
00187         bsButtonActionPerformed(evt);
00188       }
00189     });
00190 
00191 
00192     exitButton.setBounds(772, 48, 75, 25);
00193     exitButton.setLabel("exit");
00194     buttonPanel.add(exitButton);
00195 
00196     exitButton.addActionListener(new ActionListener() {
00197       public void actionPerformed(ActionEvent evt) {
00198         exitButtonActionPerformed(evt);
00199       }
00200     });
00201 
00202     ssTimeLabel.setBounds(16, 256, 120, 16);
00203     ssTimeLabel.setText("  ");
00204     ssTimeLabel.setFont(new Font("MS Sans Serif", Font.PLAIN, 13));
00205     cp.add(ssTimeLabel);
00206     isTimeLabel.setBounds(324, 256, 120, 16);
00207     isTimeLabel.setText("  ");
00208     isTimeLabel.setFont(new Font("MS Sans Serif", Font.PLAIN, 13));
00209     cp.add(isTimeLabel);
00210     bsTimeLabel.setBounds(632, 256, 120, 16);
00211     bsTimeLabel.setText("  ");
00212     bsTimeLabel.setFont(new Font("MS Sans Serif", Font.PLAIN, 13));
00213     cp.add(bsTimeLabel);
00214 
00215     setResizable(false);
00216     setVisible(true);
00217     
00218     this.randomizeArrays();  //ramdomize the arrays
00219   }
00220 
00221 
00225   public void randomizeArrays()
00226   {
00227     
00228     //generate new random numbers
00229     Random r = new Random(System.currentTimeMillis()); 
00230    
00231     for (int i=0; i<210; i++)
00232     {
00233       ssArray[i] = 1 + (Math.abs(r.nextInt()) % 180);
00234       //same numbers in all arrays
00235       isArray[i] = ssArray[i];
00236       bsArray[i] = ssArray[i];
00237     }
00238 
00239     //visualize the generated numbers
00240     Paint.showArray(ssArray, ssPanel);
00241     Paint.showArray(isArray, isPanel);
00242     Paint.showArray(bsArray, bsPanel);
00243   }
00244 
00248   public void startSelectionSort()
00249   {
00250     SelectionSort sSort = new SelectionSort();
00251     sSort.setSettings(this.ssArray, this.ssPanel, this.ssTimeLabel);
00252     Thread ssThread = new Thread(sSort);  //generate new thread
00253 
00254     ssThread.start();
00255     
00256     //wait until thread has finished
00257     try
00258     {
00259       ssThread.join();
00260     }
00261     catch (InterruptedException ie)
00262     {
00263 
00264     }
00265   }
00266 
00270   public void startInsertionSort()
00271   {
00272     InsertionSort iSort = new InsertionSort();
00273     iSort.setSettings(this.isArray, this.isPanel, this.isTimeLabel);
00274     Thread isThread = new Thread(iSort);   //generate new thread
00275 
00276 
00277     isThread.start();
00278 
00279     //wait until thread has finished
00280     try
00281     {
00282       isThread.join();
00283     }
00284     catch (InterruptedException ie)
00285     {
00286 
00287     }
00288   }
00289 
00293   public void startBubbleSort()
00294   {
00295     BubbleSort bSort = new BubbleSort();
00296     bSort.setSettings(this.bsArray, this.bsPanel, this.bsTimeLabel);
00297     Thread bsThread = new Thread(bSort);  //generate new thread
00298 
00299 
00300     bsThread.start();
00301 
00302     //wait until thread has finished
00303     try
00304     {
00305       bsThread.join();
00306     }
00307     catch (InterruptedException ie)
00308     {
00309 
00310     }
00311   }
00312 
00316   public void sortArrays()
00317   {
00318     SelectionSort sSort = new SelectionSort();
00319     sSort.setSettings(this.ssArray, this.ssPanel, this.ssTimeLabel);
00320     Thread ssThread = new Thread(sSort);  //generate new thread
00321 
00322     InsertionSort iSort = new InsertionSort();
00323     iSort.setSettings(this.isArray, this.isPanel, this.isTimeLabel);
00324     Thread isThread = new Thread(iSort);  //generate new thread
00325 
00326     BubbleSort bSort = new BubbleSort();
00327     bSort.setSettings(this.bsArray, this.bsPanel, this.bsTimeLabel);
00328     Thread bsThread = new Thread(bSort);  //generate new thread
00329 
00330 
00331     /* Ok, this is not realy a simultaneously start - as you can see below, the starting order is: selectionsort, insertionsort and then bubblesort.
00332        But as soon as one thread has been started via start(),
00333        the next thread will be started and doesn't need to wait until the previous thread has finished. */
00334     ssThread.start();
00335     isThread.start();
00336     bsThread.start();
00337 
00338 
00339     //wait until threads are finished
00340     try
00341     {
00342       ssThread.join();
00343     }
00344     catch (InterruptedException ie)
00345     {
00346 
00347     }
00348 
00349     try
00350     {
00351       isThread.join();
00352     }
00353     catch (InterruptedException ie)
00354     {
00355 
00356     }
00357 
00358     try
00359     {
00360       bsThread.join();
00361     }
00362     catch (InterruptedException ie)
00363     {
00364 
00365     }
00366   }
00367 
00372   public void ssButtonActionPerformed(ActionEvent evt)
00373   {
00374     this.startSelectionSort();
00375   }
00376 
00381   public void isButtonActionPerformed(ActionEvent evt)
00382   {
00383     this.startInsertionSort();
00384   }
00385 
00390   public void bsButtonActionPerformed(ActionEvent evt)
00391   {
00392     this.startBubbleSort();
00393   }
00394 
00399   public void randomizeButtonActionPerformed(ActionEvent evt)
00400   {
00401     this.randomizeArrays();
00402   }
00403 
00409   public void randStartButtonActionPerformed(ActionEvent evt)
00410   {
00411     this.randomizeArrays();
00412     this.sortArrays();
00413   }
00414 
00420   public void startButtonActionPerformed(ActionEvent evt)
00421   {
00422     this.sortArrays();
00423   }
00424 
00429   public void exitButtonActionPerformed(ActionEvent evt)
00430   {
00431     System.exit(0);
00432   }
00433 
00438   public static void main(String[] args)
00439   {
00440     new DoxygenExample("Selectionsort, Insertionsort and Bubblesort");
00441   }
00442 }
00443 

Generated on Thu Jun 22 19:07:31 2006 for Doxygen Example (Java) by  doxygen 1.4.5