Skip to content

Commit

Permalink
added user input for array
Browse files Browse the repository at this point in the history
  • Loading branch information
hugh5 committed Nov 1, 2022
1 parent 77b0f01 commit 714e404
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 21 deletions.
17 changes: 10 additions & 7 deletions src/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;

public class MainWindow implements ActionListener, ChangeListener {
private final JFrame window;
private MenuPanel menuPanel;
private SortingPanel sortingPanel;
public static final int MAX = 64;
public static final int MIN = 2;
public static final int MIN = 1;


public MainWindow() {
Expand Down Expand Up @@ -49,12 +50,14 @@ public void show() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(e);
if (e.getSource() instanceof JButton) {
if (e.getActionCommand().equals("generate")) {
sortingPanel.generateArray();
} else if (e.getActionCommand().equals("start")) {
sortingPanel.start(menuPanel.getSortingMethod());
}
if (e.getActionCommand().equals("generate")) {
sortingPanel.generateArray();
} else if (e.getActionCommand().equals("upload")) {
int[] array = menuPanel.getArray();
menuPanel.getLength().setValue(array.length);
sortingPanel.setArray(array);
} else if (e.getActionCommand().equals("start")) {
sortingPanel.start(menuPanel.getSortingMethod());
}
}

Expand Down
53 changes: 53 additions & 0 deletions src/MenuPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ public class MenuPanel {
private JSlider length;
private JLabel lengthLabel;
private JComboBox<SortingMethod> sortingMethod;
private JButton upload;
private JTextField setArray;

private JButton start;
private JButton forward;
private JButton backward;
Expand Down Expand Up @@ -44,6 +47,35 @@ private void createNorthPanel() {
northPanel.add(lengthLabel);
lengthLabel.setText("Length:" + length.getValue());

URL dividerURL = getClass().getResource("assets/divider.png");
ImageIcon dividerIcon = null;
if (dividerURL != null) {
dividerIcon = new ImageIcon(dividerURL);
dividerIcon = new ImageIcon(dividerIcon.getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT));
northPanel.add(new JLabel(dividerIcon));
} else {
northPanel.add(new JLabel("|"));
}

URL uploadURL = getClass().getResource("assets/upload.png");
if (uploadURL != null) {
ImageIcon uploadIcon = new ImageIcon(uploadURL);
uploadIcon = new ImageIcon(uploadIcon.getImage().getScaledInstance(15, 15, Image.SCALE_DEFAULT));
upload = new JButton("Set Array", uploadIcon);
} else {
upload = new JButton("Set Array");
}
northPanel.add(upload);

setArray = new JTextField(String.format("[%d, %d, %d]", MainWindow.MAX, MainWindow.MAX / 4, MainWindow.MAX / 2), 30);
northPanel.add(setArray);

if (dividerIcon != null) {
northPanel.add(new JLabel(dividerIcon));
} else {
northPanel.add(new JLabel("|"));
}

sortingMethod = new JComboBox<>(SortingMethod.values());
northPanel.add(sortingMethod);
}
Expand Down Expand Up @@ -98,6 +130,23 @@ public JPanel getSouthPanel() {
return southPanel;
}

public JSlider getLength () {
return length;
}

public int[] getArray() {
String[] values = setArray.getText().replace("[","").replace("]","").split(",");
int[] array = new int[Math.min(values.length, MainWindow.MAX)];
int i = 0;
for (String s : values) {
if (i > MainWindow.MAX) break;
try {
array[i++] = Integer.parseInt(s.strip());
} catch (NumberFormatException ignored) {}
}
return array;
}

public SortingMethod getSortingMethod() {
return (SortingMethod) sortingMethod.getSelectedItem();
}
Expand All @@ -115,6 +164,10 @@ public void addChangeListeners(ChangeListener changeListener) {
public void addActionListeners(ActionListener actionListener) {
generate.setActionCommand("generate");
generate.addActionListener(actionListener);
upload.setActionCommand("upload");
upload.addActionListener(actionListener);
setArray.setActionCommand("upload");
setArray.addActionListener(actionListener);

start.setActionCommand("start");
start.addActionListener(actionListener);
Expand Down
32 changes: 18 additions & 14 deletions src/SortingPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public class SortingPanel extends JComponent {
private Map<Integer, Color> map;
private int[] array;
private final Random r;
private int size;
private int speed;

private static final Color CURRENT = Color.ORANGE;
Expand All @@ -20,36 +19,41 @@ public class SortingPanel extends JComponent {

public SortingPanel() {
r = new Random(System.nanoTime());
size = MainWindow.MAX / 2;
generateArray();
map = new HashMap<>(size);
generateArray(MainWindow.MAX / 2);
speed = 5;
}

public void paint(Graphics g) {
int y = getHeight() -20;
int scalar = getHeight() / MainWindow.MAX - 1;
int width = Math.min(getWidth() / size, 100);
int width = Math.min(getWidth() / array.length, 100);
g.setFont(new Font("Monospaced", Font.PLAIN, 16));
for (int i = 0; i < size; i++) {
g.setColor(Color.BLACK);
g.drawString(String.valueOf(array[i]), (int) i * width + width / 4, y + 15);
for (int i = 0; i < array.length; i++) {
g.setColor(map.get(i));
g.fillRect(i * width, y, width - 2, -scalar * array[i]);
g.setColor(Color.BLACK);
g.drawString(String.valueOf(array[i]), (int) i * width + width / 4, y + 15);
}
}

public void setArray(int[] array) {
this.array = array;
map = new HashMap<>(array.length);
System.out.println(Arrays.toString(array));
this.repaint();
}

public void generateArray(int size) {
this.size = size;
IntStream ints = r.ints(size, 1, MainWindow.MAX);
size = Math.min(Math.max(size, MainWindow.MIN), MainWindow.MAX);
IntStream ints = r.ints(size, MainWindow.MIN, MainWindow.MAX);
array = ints.toArray();
map = new HashMap<>(size);
System.out.println(Arrays.toString(array));
this.repaint();
}

public void generateArray() {
generateArray(this.size);
generateArray(array.length);
}

public void start(SortingMethod sortingMethod) {
Expand Down Expand Up @@ -77,10 +81,10 @@ public void updateScreen() {
private void selectionSort() {
map.clear();
int numSorted = 0;
for (int n = 0; n < size; n++) {
for (int n = 0; n < array.length; n++) {
int minIndex = n;
map.put(minIndex, MIN);
for (int i = numSorted; i < size; i++) {
for (int i = numSorted; i < array.length; i++) {
if (array[i] < array[minIndex]) {
map.remove(minIndex);
map.put(i, MIN);
Expand Down Expand Up @@ -108,7 +112,7 @@ private void selectionSort() {
private void insertionSort() {
map.clear();
map.put(0, SORTED);
for (int n = 1; n < size; n++) {
for (int n = 1; n < array.length; n++) {
for (int i = n; i > 0; i--) {
if (array[i] < array[i - 1]) {
map.put(i - 1, MIN);
Expand Down
Binary file added src/assets/divider.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/upload.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 714e404

Please sign in to comment.