Skip to content

Commit de32214

Browse files
committed
Dijiktra's Algo added again
2 parents 0b1a016 + 6a5781b commit de32214

File tree

5 files changed

+81
-42
lines changed

5 files changed

+81
-42
lines changed

Diff for: src/sortvisualiser/MainApp.java

+19-21
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,35 @@
77
import javax.swing.SwingUtilities;
88

99
public class MainApp {
10-
private final JFrame window;
10+
private final JFrame window;/*JFrame is a class of the javax.swing package. It provides GUI interface
11+
Frame works like the main window where components like labels, buttons, textfields are added to create a GUI.*/
12+
public static final int WIN_WIDTH = 1280; // Assigning width of main frame window
13+
public static final int WIN_HEIGHT = 720; //Assigning height of main frame window
1114

12-
public static final int WIN_WIDTH = 1280;
13-
public static final int WIN_HEIGHT = 720;
14-
15-
private final ArrayList<Screen> screens;
15+
private final ArrayList<Screen> screens; // created a arraylist of type Screen
1616

1717
public MainApp() {
18-
screens = new ArrayList<>();
19-
window = new JFrame ("Sort visualiser");
20-
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
21-
window.setVisible(true);
18+
screens = new ArrayList<>(); // Initialising a new arraylist of screens
19+
window = new JFrame ("Sort visualiser"); // Heading of GUI window
20+
window.setVisible(true); // For making the GUI visible or not
2221
}
2322

24-
public Screen getCurrentScreen() {
23+
public Screen getCurrentScreen() { //returns current screen
2524
return screens.get(screens.size() - 1);
2625
}
2726

28-
public void pushScreen(Screen screen) {
29-
if (!screens.isEmpty()) {
27+
public void pushScreen(Screen screen) { //initialised a method to push screen that takes object of Screen class as parameter
28+
if (!screens.isEmpty()) { // if screen is not empty then remove the current screen from GUI
3029
window.remove(getCurrentScreen());
3130
}
32-
screens.add(screen);
33-
window.setContentPane(screen);
34-
window.validate();
31+
screens.add(screen); // If screen is empty, add a new screen to GUi
32+
window.setContentPane(screen); // Set the content pane through the screen object
33+
window.validate(); //Inbuilt function
3534
screen.onOpen();
3635
}
3736

38-
public void popScreen() {
39-
if (!screens.isEmpty()) {
37+
public void popScreen() { //initiated a method to pop the window and screen on GUI
38+
if (!screens.isEmpty()) { // if screen is not empty then remove the window and screen
4039
Screen prev = getCurrentScreen();
4140
screens.remove(prev);
4241
window.remove(prev);
@@ -52,15 +51,14 @@ public void popScreen() {
5251
}
5352
}
5453

55-
public void start() {
54+
public void start() { // starts and push the main menu screen onto the GUI
5655
pushScreen(new MainMenuScreen(this));
5756
window.pack();
5857
}
5958

60-
public static void main(String... args) {
61-
System.setProperty("sun.java2d.opengl", "true");
59+
public static void main(String[] args) {
6260
SwingUtilities.invokeLater(() -> {
6361
new MainApp().start();
6462
});
6563
}
66-
}
64+
}

Diff for: src/sortvisualiser/SortArray.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121

2222
/**
2323
* The array that can be sorted
24-
*
25-
* @author mhops
2624
*/
2725
public class SortArray extends JPanel {
2826
public static final int DEFAULT_WIN_WIDTH = 1280;
@@ -43,8 +41,14 @@ public class SortArray extends JPanel {
4341
private String tandComplexity = " ";
4442
private ISortAlgorithm algorithm;
4543
private long algorithmDelay = 0;
44+
<<<<<<< HEAD
4645
private final JSpinner spinner;
4746
//private int arrayChanges = 0; // Number of changes to the array the current algorithm has taken so far
47+
=======
48+
49+
private final JSpinner spinner;
50+
51+
>>>>>>> 6a5781b83e4af42e6dddad64f872c76d885b4356
4852

4953
public SortArray() {
5054
setBackground(Color.DARK_GRAY);
@@ -145,7 +149,6 @@ public void paintComponent(Graphics g) {
145149
panelGraphics.drawString(" Current algorithm: " + algorithmName, 10, 30);
146150
panelGraphics.drawString(" Time Complexity & Space Complexity: " + tandComplexity, 10, 55);
147151
panelGraphics.drawString("Current step delay: " + algorithmDelay + "ms", 10, 80);
148-
// panelGraphics.drawString(" Array Changes: " + arrayChanges, 10, 105);
149152

150153
drawBars(panelGraphics);
151154
} finally {
@@ -239,4 +242,4 @@ public void setAlgorithm(ISortAlgorithm algorithm) {
239242
algorithmDelay = algorithm.getDelay();
240243
spinner.setValue((int) algorithm.getDelay());
241244
}
242-
}
245+
}

Diff for: src/sortvisualiser/screens/MainMenuScreen.java

+30-5
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,27 @@
1515
import sortvisualiser.MainApp;
1616
import sortvisualiser.algorithms.*;
1717

18-
1918
public final class MainMenuScreen extends Screen {
19+
<<<<<<< HEAD
2020
private static final Color BACKGROUND_COLOUR = Color.BLUE; //defining color of background
2121
private final ArrayList<AlgorithmCheckBox> checkBoxes;
2222

2323
public MainMenuScreen(MainApp app) {
2424
super(app);
2525
checkBoxes = new ArrayList<>(); //array that contains the checkboxes
2626
setUpGUI();
27+
=======
28+
private static final Color BACKGROUND_COLOUR = Color.BLUE; // sets the background color
29+
private final ArrayList<AlgorithmCheckBox> checkBoxes; // creates an arraylist for the checkboxes in the GUI screen
30+
31+
public MainMenuScreen(MainApp app) {
32+
super(app);
33+
checkBoxes = new ArrayList<>(); // we initialised the checkbox arraylist
34+
setUpGUI(); //called the setUpGUI method defined below
35+
>>>>>>> 6a5781b83e4af42e6dddad64f872c76d885b4356
2736
}
2837

29-
private void addCheckBox(ISortAlgorithm algorithm, JPanel panel) {
38+
private void addCheckBox(ISortAlgorithm algorithm, JPanel panel) { //add the checkboxes for sorting algos in the mainMenuScreen of GUI window
3039
JRadioButton box = new JRadioButton("", true);
3140
box.setAlignmentX(Component.LEFT_ALIGNMENT);
3241
box.setBackground(BACKGROUND_COLOUR);
@@ -35,19 +44,26 @@ private void addCheckBox(ISortAlgorithm algorithm, JPanel panel) {
3544
panel.add(box);
3645
}
3746

38-
private void initContainer(JPanel p) {
47+
private void initContainer(JPanel p) { //Container class init() method
3948
p.setLayout(new BoxLayout(p, BoxLayout.PAGE_AXIS));
4049
p.setBackground(BACKGROUND_COLOUR);
4150
}
4251

52+
<<<<<<< HEAD
4353
public void setUpGUI() {
4454
JPanel sortAlgorithmContainer = new JPanel();
4555
JPanel optionsContainer = new JPanel();
56+
=======
57+
public void setUpGUI() { // method created to setup the GUI window, i.e setting background and alignment
58+
JPanel sortAlgorithmContainer = new JPanel(); // object for sorting array window
59+
JPanel optionsContainer = new JPanel(); // object for main menu class i.e, with the radio buttons for sort algos
60+
>>>>>>> 6a5781b83e4af42e6dddad64f872c76d885b4356
4661
JPanel outerContainer = new JPanel();
4762
initContainer(this);
4863
initContainer(optionsContainer);
4964
initContainer(sortAlgorithmContainer);
5065

66+
<<<<<<< HEAD
5167
outerContainer.setBackground(BACKGROUND_COLOUR);
5268
outerContainer.setLayout(new BoxLayout(outerContainer, BoxLayout.LINE_AXIS));
5369

@@ -60,6 +76,10 @@ public void setUpGUI() {
6076
} catch (IOException e) {
6177
System.out.println("Unable to load logo");
6278
}
79+
=======
80+
outerContainer.setBackground(BACKGROUND_COLOUR); // set the background color
81+
outerContainer.setLayout(new BoxLayout(outerContainer, BoxLayout.LINE_AXIS)); // set the layout
82+
>>>>>>> 6a5781b83e4af42e6dddad64f872c76d885b4356
6383

6484
sortAlgorithmContainer.setAlignmentX(Component.LEFT_ALIGNMENT);//Examples of components are the buttons, checkboxes, and scrollbars of a typical graphical user interface
6585
addCheckBox(new BubbleSort(), sortAlgorithmContainer);
@@ -69,7 +89,12 @@ public void setUpGUI() {
6989
addCheckBox(new InsertionSort(), sortAlgorithmContainer);
7090
addCheckBox(new RadixSort(), sortAlgorithmContainer);
7191

92+
<<<<<<< HEAD
7293
JButton startButton = new JButton("Start"); //button text
94+
=======
95+
96+
JButton startButton = new JButton("Start"); // start button for mainmenu in GUI
97+
>>>>>>> 6a5781b83e4af42e6dddad64f872c76d885b4356
7398
startButton.addActionListener((ActionEvent e) -> {
7499
ArrayList<ISortAlgorithm> algorithms = new ArrayList<>();
75100
for (AlgorithmCheckBox cb : checkBoxes) {
@@ -97,7 +122,7 @@ public void setUpGUI() {
97122
}
98123

99124
@Override
100-
public void onOpen() {
125+
public void onOpen() { // unchecks all the radiobutton
101126
checkBoxes.forEach((box) -> {
102127
box.unselect();
103128

@@ -129,4 +154,4 @@ public ISortAlgorithm getAlgorithm() {
129154
}
130155
}
131156

132-
}
157+
}

Diff for: src/sortvisualiser/screens/Screen.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,27 @@
55
import javax.swing.JPanel;
66
import sortvisualiser.MainApp;
77

8+
<<<<<<< HEAD
89
public abstract class Screen extends JPanel { //making a child class from parent class JPanel
910
protected MainApp app; //protected keyword is an access modifier used for attributes,
1011
//methods and constructors, making them accessible in the same package and subclasses
12+
=======
13+
public abstract class Screen extends JPanel {
14+
protected MainApp app; // object of MainApp class
15+
>>>>>>> 6a5781b83e4af42e6dddad64f872c76d885b4356
1116

1217
public Screen(MainApp app) {
13-
this.app = app;
18+
this.app = app; // setter function
1419
}
1520

1621
@Override
1722
public Dimension getPreferredSize() {
23+
<<<<<<< HEAD
1824
return new Dimension(WIN_WIDTH, WIN_HEIGHT); //se
25+
=======
26+
return new Dimension(WIN_WIDTH, WIN_HEIGHT); // return dimension of window
27+
>>>>>>> 6a5781b83e4af42e6dddad64f872c76d885b4356
1928
}
2029

21-
public abstract void onOpen();
22-
}
30+
public abstract void onOpen(); //abstract onOpen method used for starting a sort ting window from beginning
31+
}

Diff for: src/sortvisualiser/screens/SortingVisualiserScreen.java

+13-9
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,37 @@
77
import sortvisualiser.SortArray;
88
import sortvisualiser.algorithms.ISortAlgorithm;
99

10+
<<<<<<< HEAD
1011

1112
public final class SortingVisualiserScreen extends Screen { //The main class for the sort visualiser GUI
13+
=======
14+
public final class SortingVisualiserScreen extends Screen {
15+
>>>>>>> 6a5781b83e4af42e6dddad64f872c76d885b4356
1216
private final SortArray sortArray;
1317
private final ArrayList<ISortAlgorithm> sortQueue;
1418

1519
/**
1620
* Creates the GUI
17-
* @param algorithms List of algorithms to run for visualisation
18-
* @param app The main application
21+
* List of algorithms to run for visualisation
22+
* app The main application
1923
*/
2024
public SortingVisualiserScreen(ArrayList<ISortAlgorithm> algorithms, MainApp app) {
2125
super(app);
22-
setLayout(new BorderLayout());
23-
sortArray = new SortArray();
24-
add(sortArray, BorderLayout.CENTER);
25-
sortQueue = algorithms;
26+
setLayout(new BorderLayout()); // sets the border of GUI window
27+
sortArray = new SortArray(); // creates object of SortArray class
28+
add(sortArray, BorderLayout.CENTER); // adds the radiobutton for the selected sorting algorithm
29+
sortQueue = algorithms; // adds the algorithm to teh arraylist
2630
}
2731

28-
private void longSleep() {
32+
private void longSleep() { // used for delay
2933
try {
3034
Thread.sleep(1000);
3135
} catch (InterruptedException ex) {
3236
ex.printStackTrace();
3337
}
3438
}
3539

36-
private void shuffleAndWait() {
40+
private void shuffleAndWait() { // method for shuffling the rearranged arrays and changing their colors
3741
sortArray.shuffle();
3842
sortArray.resetColours();
3943
longSleep();
@@ -72,4 +76,4 @@ public void done() {
7276

7377
swingWorker.execute();
7478
}
75-
}
79+
}

0 commit comments

Comments
 (0)