Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m currently working on a java selection sort porgram with some GUI display. I

ID: 3677021 • Letter: I

Question

I'm currently working on a java selection sort porgram with some GUI display.

I've been wondering how do I add a button to the GUI, so the button can run the sort step by step by every click the user make.

And how can I mark the currently moving block to red so it can easily tell where does it move it from.

//Code

import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class AnimatedSelectionSortPanel extends javax.swing.JPanel {

public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new AnimatedSelectionSortPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationByPlatform(true);
   frame.setVisible(true);


}
});
}


private int[] numbers = new int[20];
private int min = 10;
private int max = 80;
private boolean shuffle = false;
public static final int ITERATION_SLEEP = 100;

public AnimatedSelectionSortPanel() {
loadArray();
addMouseListener(new MouseAdapter() {
@Override
   public void mouseClicked(MouseEvent e) {
if (shuffle)
loadArray();
selectionSort(numbers);
shuffle = true;
}
});
}

@Override
   public Dimension getPreferredSize() {
return new Dimension(300, 100);
}

private void loadArray() {
for (int i = 0; i < numbers.length; i++) {
numbers[i] = min + (int) (Math.random() * ((max - min) + 1));
}
}

public void selectionSort(final int[] x) {
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < x.length - 1; i++) {
int minIndex = i; // Index of smallest remaining value.
for (int j = i + 1; j < x.length; j++) {
if (x[minIndex] > x[j]) {
minIndex = j; // Remember index of new minimum
}
}
if (minIndex != i) {
// ... Exchange current element with smallest remaining.
int temp = x[i];
x[i] = x[minIndex];
x[minIndex] = temp;
}
repaint();
try {
Thread.sleep(ITERATION_SLEEP);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}

private void drawPass(Graphics g) {
int rectWidth = 10;

int width = getWidth() - 1;
int height = getHeight() - 1;
int colSpan = Math.round((float)width / (float)numbers.length);
int x = 0;

for (int num : numbers) {
g.setColor(Color.green);
int colHeight = (int) ((float) height * ((float) num / (float) 100));
g.fillRect(x, height - colHeight, rectWidth, colHeight);

   x += colSpan;
}
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
drawPass(g);
}
}

Explanation / Answer

To add a button to the Panel, add the following statement:

@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new AnimatedSelectionSortPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
           JButton sort=new JButton ("Sort");
           sort.addActionListener(new MySortAction());
               frame.add (sort);

}
});
      
Now you can define your sorting action on the click of this "Sort" Button. In the MySortAction method, define your sort logic . :)

Please specify details for the red color query. If you wish to color something, you can use repaint() or setBackGround(COLOR.RED) but I believe you wish to ask something else. Please
provide details.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote