@@ -10,10 +10,14 @@ public class Main extends JApplet {
1010 private SortPanel [] sortPanels = new SortPanel [9 ];
1111 private int size = 100 ;
1212 private int sleepTime = 2 ;
13-
13+ private String animationName = "" ;
1414
1515 public Main () {
16- setLayout (new GridLayout (0 , 3 , 0 , 0 ));
16+ setLayout (new GridLayout (1 , 1 , 0 , 0 ));
17+ SortPanelsHolder sortPanelHolder = new SortPanelsHolder ();
18+ sortPanelHolder .setLayout (new GridLayout (0 , 3 , 0 , 0 ));
19+ sortPanelHolder .setBackground (Color .BLACK );
20+ sortPanelHolder .setForeground (Color .BLACK );
1721 int [] list = new int [size ];
1822 for (int i = 0 ; i < list .length ; i ++) {
1923 list [i ] = i + 1 ;
@@ -25,32 +29,63 @@ public Main() {
2529 list [i ] = list [index ];
2630 list [index ] = temp ;
2731 }
28- sortPanels [0 ] = new SelectionSortPanel (" Selection Sort " , list , sleepTime );
29- sortPanels [1 ] = new InsertionSortPanel (" Insertion Sort " , list , sleepTime );
30- sortPanels [2 ] = new BubbleSortPanel (" Bubble Sort " , list , sleepTime );
31- sortPanels [3 ] = new QuickSortPanel (" Quick Sort " , list , sleepTime );
32- sortPanels [4 ] = new MergeSortPanel (" Merge Sort " , list , sleepTime );
33- sortPanels [5 ] = new HeapSortPanel (" Heap Sort " , list , sleepTime );
34- sortPanels [6 ] = new CocktailSortPanel (" Cocktail Sort " , list , sleepTime );
35- sortPanels [7 ] = new CombSortPanel (" Comb Sort " , list , sleepTime );
36- sortPanels [8 ] = new ShellSortPanel (" Shell Sort " , list , sleepTime );
3732
33+ Dimension screenSize = Toolkit .getDefaultToolkit ().getScreenSize ();
34+ int width = screenSize .width / 3 ;
35+ int height = screenSize .height / 3 ;
36+ sortPanels [0 ] = new SelectionSortPanel (" Selection Sort " , list , sleepTime , width , height );
37+ sortPanels [1 ] = new InsertionSortPanel (" Insertion Sort " , list , sleepTime , width , height );
38+ sortPanels [2 ] = new BubbleSortPanel (" Bubble Sort " , list , sleepTime , width , height );
39+ sortPanels [3 ] = new QuickSortPanel (" Quick Sort " , list , sleepTime , width , height );
40+ sortPanels [4 ] = new MergeSortPanel (" Merge Sort " , list , sleepTime , width , height );
41+ sortPanels [5 ] = new HeapSortPanel (" Heap Sort " , list , sleepTime , width , height );
42+ sortPanels [6 ] = new CocktailSortPanel (" Cocktail Sort " , list , sleepTime , width , height );
43+ sortPanels [7 ] = new CombSortPanel (" Comb Sort " , list , sleepTime , width , height );
44+ sortPanels [8 ] = new ShellSortPanel (" Shell Sort " , list , sleepTime , width , height );
3845
3946 for (int i = 0 ; i < sortPanels .length ; i ++) {
40- add (sortPanels [i ]);
47+ sortPanels [i ].setVisible (false );
48+ sortPanelHolder .add (sortPanels [i ]);
49+ }
50+ add (sortPanelHolder );
51+ }
52+
53+ class SortPanelsHolder extends JPanel {
54+ private static final long serialVersionUID = 1L ;
55+ @ Override
56+ protected void paintComponent (Graphics g ) {
57+ super .paintComponent (g );
58+ g .setColor (Color .WHITE );
59+ Font animationNameFont = new Font ("Monospaced" , Font .BOLD , 150 );
60+ FontMetrics animationNameFontFontMetrix = getFontMetrics (animationNameFont );
61+ g .setFont (animationNameFont );
62+ int x = (getWidth () - animationNameFontFontMetrix .stringWidth (animationName )) / 2 ;
63+ int y = (getHeight () - animationNameFontFontMetrix .getLeading ()) / 2 ;
64+ g .drawString (animationName , x , y );
4165 }
42-
4366 }
4467
45- public void beginAnimation () {
68+ public void beginAnimation (String animationName ) {
69+ this .animationName = animationName ;
70+ repaint ();
71+ try {
72+ Thread .sleep (2000 );
73+ } catch (InterruptedException e ) {
74+ e .printStackTrace ();
75+ }
76+ this .animationName = "" ;
77+ repaint ();
78+ for (int i = 0 ; i < sortPanels .length ; i ++) {
79+ sortPanels [i ].setVisible (true );
80+ }
4681 try {
47- Thread .sleep (3000 );
82+ Thread .sleep (1000 );
4883 } catch (InterruptedException e ) {
4984 e .printStackTrace ();
5085 }
5186 for (int i = 0 ; i < sortPanels .length ; i ++) {
5287 sortPanels [i ].beginAnimation ();
53- }
88+ }
5489 }
5590
5691 public static void main (String [] args ) {
@@ -62,6 +97,6 @@ public static void main(String[] args) {
6297 frame .pack ();
6398 frame .setLocationRelativeTo (null );
6499 frame .setVisible (true );
65- main .beginAnimation ();
100+ main .beginAnimation ("Random" );
66101 }
67102}
0 commit comments