J
J
javalamer2021-05-24 01:08:02
Java
javalamer, 2021-05-24 01:08:02

How to draw sorting step by step?

It is necessary to draw the sorting elements in stages, for example, after each swap. When you start (button start), the elements appear already sorted.
The code:

import javax.swing.*;
import javax.swing.plaf.ColorUIResource;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Window {
    static int[] array=new int[1000];

    static int n;
    public static void main(String[] args) {


        JFrame w=new JFrame("Selection sort visualization");

        w.setSize(new Dimension(1300,800));

        w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        w.setLayout(new BorderLayout(0,-50));


        JPanel panel=new JPanel();
        panel.setBackground(Color.CYAN);

        JButton pauseButton=new JButton("Pause");
        pauseButton.setHorizontalAlignment(SwingConstants.CENTER);
        pauseButton.setPreferredSize(new Dimension(120,40));
        Color purple=new Color(160,150,255);
        pauseButton.setBackground(purple);

        JButton forwardButton=new JButton("Step forward");
        forwardButton.setHorizontalAlignment(SwingConstants.CENTER);
        forwardButton.setPreferredSize(new Dimension(120,40));
        forwardButton.setBackground(purple);

        JButton backButton=new JButton("Step back");
        backButton.setHorizontalAlignment(SwingConstants.CENTER);
        backButton.setPreferredSize(new Dimension(120,40));
        backButton.setBackground(purple);

        panel.setLayout(new FlowLayout(FlowLayout.CENTER,80,50));

        panel.add(pauseButton);
        panel.add(forwardButton);
        panel.add(backButton);

        JPanel panel1=new JPanel();
        panel1.setBackground(Color.CYAN);
        JTextField textField=new JTextField();
        textField.setPreferredSize(new Dimension(120,40));




        JButton startButton=new JButton("Start!");
        startButton.setHorizontalAlignment(SwingConstants.CENTER);
        startButton.setPreferredSize(new Dimension(120,40));
        startButton.setBackground(purple);


        startButton.addActionListener(e -> {

            boolean flag=false;
            try {
                final int k = Integer.parseInt(textField.getText());
            }
            catch (NumberFormatException ex){
                JOptionPane.showMessageDialog(w,
                        new String[] {ex.toString()},
                        "ERROR",
                        JOptionPane.ERROR_MESSAGE);
                flag=true;
            }

            if(!flag) {
                n = Integer.parseInt(textField.getText());

                for (int i = 0; i < n; i++) {
                    array[i] = (int) (Math.random() * 400);
                }
                for (int i = 0; i < n; i++) {
                    int pos = i;
                    int min = array[i];

                    for (int j = i + 1; j < n; j++) {
                        if (array[j] < min) {
                            pos = j;
                            min = array[j];
                        }
                    }
                    array[pos] = array[i];
                    array[i] = min;
                    ;
                }
                for (int i = 0; i < n; i++) {
                    System.out.println(array[i]);

                }
            }
        });

        panel1.setLayout(new FlowLayout(FlowLayout.CENTER,80,50));


        panel1.add(textField);
        panel1.add(startButton);


        Canvas canv=new Canvas();

        w.add(BorderLayout.PAGE_START,panel);
        w.add(BorderLayout.CENTER,canv);
        w.add(BorderLayout.SOUTH,panel1);
        w.setVisible(true);
    }
}
class Canvas extends JComponent implements ActionListener {
    public Canvas(){
        super();


        Timer timer=new Timer(100,this);
        timer.start();
    }

    @Override
    public void actionPerformed(ActionEvent e){



        repaint();

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

        Graphics2D g2d=(Graphics2D)g;
        g2d.setColor(Color.CYAN);
        g2d.fillRect(0,-500,5000,5000);
        g2d.setColor(Color.black);
        g2d.fillRect(95,50,1110,510);
        g2d.setColor(ColorUIResource.white);
        g2d.fillRect(100,55,1100,500);

        //int wid=(1000-Window.n)/Window.n;
        //int x1=1000/(int)Math.pow(Window.n+1,2)+100;


        if(Window.n>0) {
            //int wid=1100/Window.n;
            //int wid=(1000-Window.n)/Window.n;
            int wid=10;
            int x1 = 550-((10+1)*Window.n/2)+100;
            int y=500;
            for (int i = 0; i < Window.n; i++) {
                g2d.setColor(Color.blue);
                g2d.setStroke(new BasicStroke(wid));
                g2d.drawLine(x1, y, x1, y - Window.array[i]);
                x1 += 1 + wid;


            }
        }

        super.repaint();

    }
}
<img src="https://habrastorage.org/webt/60/aa/d2/60aad231d6a27296859603.jpeg" alt="image"/>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmtm, 2021-05-24
@Dmtm

for (int i = 0; i < n; i++) {
int pos = i;
int min = array[i];
for (int j = i + 1; j < n; j++) {
if (array[j] < min) {
pos = j;
min = array[j];
}
}
array[pos] = array[i];
array[i] = min;
;
for (int i = 0; i < n; i++) {
System.out.println(array[i]);
}
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question