Skip to content

Commit df8054a

Browse files
committed
add sort name, improve UI
1 parent 1d97457 commit df8054a

File tree

11 files changed

+74
-38
lines changed

11 files changed

+74
-38
lines changed

src/net/bohush/sorting/BubbleSortPanel.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ public class BubbleSortPanel extends SortPanel {
88
private int redColumn = -1;
99
private int greenColumn = -1;
1010

11-
public BubbleSortPanel(String name, int[] list, int sleepTime) {
12-
super(name, list, sleepTime);
11+
public BubbleSortPanel(String name, int[] list, int sleepTime, int width, int height) {
12+
super(name, list, sleepTime, width, height);
1313
}
1414

1515
@Override

src/net/bohush/sorting/CocktailSortPanel.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ public class CocktailSortPanel extends SortPanel {
99
private int greenColumn1 = -1;
1010
private int greenColumn2 = -1;
1111

12-
public CocktailSortPanel(String name, int[] list, int sleepTime) {
13-
super(name, list, sleepTime);
12+
public CocktailSortPanel(String name, int[] list, int sleepTime, int width, int height) {
13+
super(name, list, sleepTime, width, height);
1414
}
1515

1616
@Override

src/net/bohush/sorting/CombSortPanel.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ public class CombSortPanel extends SortPanel {
99
private int blueColumn = -1;
1010
private int greenColumn = -1;
1111

12-
public CombSortPanel(String name, int[] list, int sleepTime) {
13-
super(name, list, sleepTime);
12+
public CombSortPanel(String name, int[] list, int sleepTime, int width, int height) {
13+
super(name, list, sleepTime, width, height);
1414
}
1515

1616
@Override

src/net/bohush/sorting/HeapSortPanel.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ public class HeapSortPanel extends SortPanel {
99
private int greenColumn = -1;
1010
private java.util.ArrayList<Integer> heapList = new java.util.ArrayList<Integer>();
1111

12-
public HeapSortPanel(String name, int[] list, int sleepTime) {
13-
super(name, list, sleepTime);
12+
public HeapSortPanel(String name, int[] list, int sleepTime, int width, int height) {
13+
super(name, list, sleepTime, width, height);
1414
}
1515

1616
@Override

src/net/bohush/sorting/InsertionSortPanel.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ public class InsertionSortPanel extends SortPanel {
88
private int redColumn = -1;
99
private int greenColumn = -1;
1010

11-
public InsertionSortPanel(String name, int[] list, int sleepTime) {
12-
super(name, list, sleepTime);
11+
public InsertionSortPanel(String name, int[] list, int sleepTime, int width, int height) {
12+
super(name, list, sleepTime, width, height);
1313
}
1414

1515
@Override

src/net/bohush/sorting/Main.java

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/net/bohush/sorting/MergeSortPanel.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ public class MergeSortPanel extends SortPanel {
1010
private int greenColumnStart = -1;
1111
private int greenColumnFinish = -1;
1212

13-
public MergeSortPanel(String name, int[] list, int sleepTime) {
14-
super(name, list, sleepTime);
13+
public MergeSortPanel(String name, int[] list, int sleepTime, int width, int height) {
14+
super(name, list, sleepTime, width, height);
1515
}
1616

1717
@Override

src/net/bohush/sorting/QuickSortPanel.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ public class QuickSortPanel extends SortPanel {
1010
private int cyanColumn = -1;
1111
private int greenColumn = -1;
1212

13-
public QuickSortPanel(String name, int[] list, int sleepTime) {
14-
super(name, list, sleepTime);
13+
public QuickSortPanel(String name, int[] list, int sleepTime, int width, int height) {
14+
super(name, list, sleepTime, width, height);
1515
}
1616

1717
@Override

src/net/bohush/sorting/SelectionSortPanel.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ public class SelectionSortPanel extends SortPanel {
99
private int blueColumn = -1;
1010
private int greenColumn = -1;
1111

12-
public SelectionSortPanel(String name, int[] list, int sleepTime) {
13-
super(name, list, sleepTime);
12+
public SelectionSortPanel(String name, int[] list, int sleepTime, int width, int height) {
13+
super(name, list, sleepTime, width, height);
1414
}
1515

1616
@Override

src/net/bohush/sorting/ShellSortPanel.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ public class ShellSortPanel extends SortPanel {
99
private int blueColumn = -1;
1010
private int greenColumn = -1;
1111

12-
public ShellSortPanel(String name, int[] list, int sleepTime) {
13-
super(name, list, sleepTime);
12+
public ShellSortPanel(String name, int[] list, int sleepTime, int width, int height) {
13+
super(name, list, sleepTime, width, height);
1414
}
1515

1616
@Override

0 commit comments

Comments
 (0)