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

Study the attached AnimatedSort source file which simulates Bubble Sort (select

ID: 3636279 • Letter: S

Question

Study the attached AnimatedSort source file which simulates Bubble Sort (select size
first to start the program) and modify it to simulate the Selection Sort algorithm.

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class AnimatedSort extends JFrame implements ChangeListener
{
private JPanel panel1 = new JPanel();
private ArrayList<JButton> btns = new ArrayList<JButton>();
private ArrayList<Integer> nos = new ArrayList<Integer>();
private Font fnt = new Font("SansSerif", 1, 30);
private int delay = 200;
private int size;
private boolean flag;
private JButton btn3;
private JComboBox sizeList;
JLabel passNo;

public AnimatedSort()
{
super("Animated Sort");
setSize(1000, 150);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());

JPanel panel2 = new JPanel();
panel1.setLayout(new GridLayout(1, size));
panel2.setLayout(new FlowLayout());

JButton btn1 = new JButton("Sort");
btn1.addActionListener(new List1());
JButton btn2 = new JButton("Stop");
btn2.addActionListener(new List2());
btn3 = new JButton("Restart");
btn3.setEnabled(false);
btn3.addActionListener(new List3());

JSlider slider = new JSlider(SwingConstants.HORIZONTAL, 50, 1050, 500);
slider.setMinorTickSpacing(100);
slider.setMajorTickSpacing(200);
slider.setPaintTicks(true);
slider.setSnapToTicks(true);


slider.addChangeListener(this);

Integer[] sizes = {4, 5, 6, 7, 8, 9, 10, 11, 12};
sizeList = new JComboBox(sizes);
// sizeList.setSelectedIndex(6);
sizeList.addActionListener(new List4());


size = (Integer)sizeList.getSelectedItem();

passNo = new JLabel("");
panel2.add(passNo);
panel2.add(new JLabel("Size : "));
panel2.add(sizeList);
panel2.add(btn1);
panel2.add(btn2);
panel2.add(btn3);
panel2.add(new JLabel(" Speed : "));
panel2.add(slider);

add(panel1, BorderLayout.CENTER);
add(panel2, BorderLayout.SOUTH);

}

public void stateChanged(ChangeEvent e)
{
JSlider source = (JSlider)e.getSource();
delay = (int)source.getValue();

}


private class List1 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{

flag = true;
new Sort().start();
}
}

private class List2 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
flag = false;
btn3.setEnabled(true);
}
}

private class List3 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Random ran = new Random();
for(int i = 0; i < size; ++i)
{
nos.set(i, ran.nextInt(100));
JButton bt = btns.get(i);
bt.setForeground(null);
bt.setText(nos.get(i) + "");
}

}
}


private class List4 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Integer sizeSel = (Integer)sizeList.getSelectedItem();
size = sizeSel;

Random ran = new Random();

for(int i = 0; i < size; ++i)
{
nos.add(ran.nextInt(100));
btns.add(new JButton());
JButton bt = btns.get(i);
bt.setFont(fnt);
bt.setText(nos.get(i) + "");
panel1.add(bt);
}

panel1.revalidate();
sizeList.setEnabled(false);


}
}



private class Sort extends Thread
{
public void run()
{
btn3.setEnabled(false);
passNo.setFont(new Font("SanSerif", Font.BOLD, 20));
passNo.setForeground(Color.red);

for (int pass=1; pass < size; pass++)
{
for (int i=0; i < size-pass; i++)
{
passNo.setText("Pass no " + pass + " ");
if( nos.get(i) > nos.get(i+1) )
{
Collections.swap(nos, i, i+1);
if(!flag) return;

btns.get(i).setBackground(Color.yellow);
btns.get(i+1).setBackground(Color.yellow);
pause(delay);

btns.get(i).setBackground(null);
btns.get(i+1).setBackground(null);
pause(delay);

flip(btns.get(i), btns.get(i+1));

btns.get(i).setText(nos.get(i) + "");
btns.get(i+1).setText(nos.get(i+1) + "");


}


}
btns.get(size-pass).setForeground(Color.blue);
}
btns.get(0).setForeground(Color.blue);
}

}
private void flip(JButton b1, JButton b2)
{
int firstLocation = b1.getX();
int secondLocation = b2.getX();
int width = panel1.getWidth();
int slide = width/size;
for (int i = 0; i <= slide; i++)
{
b1.setLocation(firstLocation++,0);
b2.setLocation(secondLocation--,0);
pause(6);
}
}



private void pause(int ms)
{
try
{
Thread.sleep(ms);
}
catch(Exception e)
{
System.exit(0);
}
}

public static void main(String[] args)
{
new AnimatedSort().setVisible(true);
}
}

Explanation / Answer

Dear Friend here is the code fixed if u need any help message me Please Rate import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class AnimatedSort extends JFrame implements ChangeListener { private JPanel panel1 = new JPanel(); private ArrayList btns = new ArrayList(); private ArrayList nos = new ArrayList(); private Font fnt = new Font("SansSerif", 1, 30); private int delay = 200; private int size; private boolean flag; private JButton btn3; private JComboBox sizeList; JLabel passNo; public AnimatedSort() { super("Animated Sort"); setSize(1000, 150); setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(new BorderLayout()); JPanel panel2 = new JPanel(); panel1.setLayout(new GridLayout(1, size)); panel2.setLayout(new FlowLayout()); JButton btn1 = new JButton("Sort"); btn1.addActionListener(new List1()); JButton btn2 = new JButton("Stop"); btn2.addActionListener(new List2()); btn3 = new JButton("Restart"); btn3.setEnabled(false); btn3.addActionListener(new List3()); JSlider slider = new JSlider(SwingConstants.HORIZONTAL, 50, 1050, 500); slider.setMinorTickSpacing(100); slider.setMajorTickSpacing(200); slider.setPaintTicks(true); slider.setSnapToTicks(true); slider.addChangeListener(this); Integer[] sizes = {4, 5, 6, 7, 8, 9, 10, 11, 12}; sizeList = new JComboBox(sizes); // sizeList.setSelectedIndex(6); sizeList.addActionListener(new List4()); size = (Integer)sizeList.getSelectedItem(); passNo = new JLabel(""); panel2.add(passNo); panel2.add(new JLabel("Size : ")); panel2.add(sizeList); panel2.add(btn1); panel2.add(btn2); panel2.add(btn3); panel2.add(new JLabel(" Speed : ")); panel2.add(slider); add(panel1, BorderLayout.CENTER); add(panel2, BorderLayout.SOUTH); } public void stateChanged(ChangeEvent e) { JSlider source = (JSlider)e.getSource(); delay = (int)source.getValue(); } private class List1 implements ActionListener { public void actionPerformed(ActionEvent e) { flag = true; new Sort().start(); } } private class List2 implements ActionListener { public void actionPerformed(ActionEvent e) { flag = false; btn3.setEnabled(true); } } private class List3 implements ActionListener { public void actionPerformed(ActionEvent e) { Random ran = new Random(); for(int i = 0; i