11import javax .swing .*;
2- import java .awt .event .*;
32import java .awt .*;
3+ import java .awt .event .*;
44
55
66public class Algorithm_Visualizer {
@@ -10,54 +10,94 @@ public static void main(String[] args){
1010 }
1111}
1212
13- 1413class Window extends JFrame implements ActionListener {//inheriting JFrame
1514 JFrame frame ; // Window
1615 JMenuBar menuBar ; // Object of Menu bar
1716 JMenu mainMenu , helpDesk ; // Part of Menu bar
1817 JMenuItem menu_1 ,menu_2 ,menu_3 ,menu_4 ; // Items of Menu
19- JButton b ;
20- JTextArea txtArea ;
18+ JButton jbtRandomize , jbtReset , jbtBubble , jbtInsertion , jbtSelection , jbtStart ; // Sorting Buttons
19+ JPanel p1Sorting , p2Sorting ;
2120
2221 Window () {
2322 frame = new JFrame ("Algorithm_Visualizer" );
2423
24+ // menu items
2525 menuBar =new JMenuBar ();
2626 mainMenu = new JMenu ("Menu" );
2727 helpDesk = new JMenu ("Help" );
2828 menu_1 = new JMenuItem ("Path Finding" );
2929 menu_2 = new JMenuItem ("Sorting Techniques" );
3030 menu_3 = new JMenuItem ("Pic Puzzle Sorting" );
3131 menu_4 = new JMenuItem ("How it Works" );
32- menu_1 .addActionListener (this );
33- menu_2 .addActionListener (this );
34- menu_3 .addActionListener (this );
35- 36- b = new JButton ("click" );//create button
37- b .setBounds (130 , 100 , 100 , 40 );
3832
39- txtArea = new JTextArea ("Welcome to Algorithm_Visualizer!" );
40- txtArea .setBounds (5 ,5 ,360 ,320 );
33+ // Buttons for sorting
34+ jbtRandomize = new JButton ("Randomize" );//create button
35+ jbtReset = new JButton ("Reset" );//create button
36+ jbtBubble = new JButton ("Bubble sort" );//create button
37+ jbtInsertion = new JButton ("Insertion sort" );//create button
38+ jbtSelection = new JButton ("Selection sort" );//create button
39+ jbtStart = new JButton ("Start" );//create button
4140
41+ // Adding menus into Menu Bar
4242 mainMenu .add (menu_1 ); mainMenu .add (menu_2 ); mainMenu .add (menu_3 ); helpDesk .add (menu_4 );
4343 menuBar .add (mainMenu ); menuBar .add (helpDesk );
44- // f.add(b);//adding button on frame
45- frame .add (txtArea ); // adding text area on frame
44+ 45+ // Panel for buttons
46+ p1Sorting = new JPanel ();
47+ p1Sorting .setLayout (new GridLayout (6 ,1 ));
48+ // Adding Buttons to the panel
49+ p1Sorting .add (jbtRandomize ); p1Sorting .add (jbtReset ); p1Sorting .add (jbtSelection );
50+ p1Sorting .add (jbtBubble ); p1Sorting .add (jbtInsertion ); p1Sorting .add (jbtStart );
51+ 52+ // Actions
53+ menu_1 .addActionListener (this );
54+ menu_2 .addActionListener (this );
55+ menu_3 .addActionListener (this );
56+ menu_4 .addActionListener (this );
57+ jbtRandomize .addActionListener (this );
58+ jbtReset .addActionListener (this );
59+ jbtBubble .addActionListener (this );
60+ jbtInsertion .addActionListener (this );
61+ jbtSelection .addActionListener (this );
62+ jbtStart .addActionListener (this );
63+ 64+ // Adding frame
65+ frame .add (p1Sorting , BorderLayout .WEST );
4666 frame .setJMenuBar (menuBar );
4767 frame .setSize (400 ,400 );
48- frame .setLayout ( null );
68+ frame .setDefaultCloseOperation ( JFrame . EXIT_ON_CLOSE );
4969 frame .setVisible (true );
5070 }
5171
72+ // Action Listener method
5273 public void actionPerformed (ActionEvent entry ) {
74+ // Menu Items
5375 if (entry .getSource ()==menu_1 )
54- txtArea .cut ();
55- if (entry .getSource ()==menu_2 )
56- txtArea .paste ();
57- if (entry .getSource ()==menu_3 )
58- txtArea .selectAll ();
59- if (entry .getSource ()==menu_4 )
60- txtArea .setText ("Help Me" );
76+ System .out .println ("1" );
77+ else if (entry .getSource ()==menu_2 )
78+ System .out .println ("2" );
79+ else if (entry .getSource ()==menu_3 )
80+ System .out .println ("3" );
81+ else if (entry .getSource ()==menu_4 )
82+ System .out .println ("4" );
83+ 84+ // Buttons for Sorting techniques
85+ else if (entry .getSource ()== jbtRandomize )
86+ System .out .println ("Randomize" );
87+ else if (entry .getSource ()== jbtReset )
88+ System .out .println ("Reset" );
89+ else if (entry .getSource ()== jbtSelection )
90+ System .out .println ("Selection" );
91+ else if (entry .getSource ()== jbtBubble )
92+ System .out .println ("Bubble" );
93+ else if (entry .getSource ()== jbtInsertion )
94+ System .out .println ("Insertion" );
95+ else if (entry .getSource ()== jbtStart )
96+ System .out .println ("Start" );
97+ 6198 }
6299}
63100
101+ 102+ 103+
0 commit comments