如何更新排序算法可视化?

发布于 2025-02-12 19:15:51 字数 12322 浏览 1 评论 0原文

我一直在使用Java Swing和AWT和编码插入排序在Java中制作分类算法可视化器,以查看它是否可以工作。该算法在某种意义上起作用的是,在运行此程序时,您可以使用插入短片进行洗牌和对算法进行排序,但实际上并未看到该算法在作用中。它只是立即发生,一直在寻找添加某种延迟的方法,但我找不到任何来源来帮助我。

我将其设置为4个Java类,这是INIT是窗口,窗口类,数组可视化器类和插入排序类的主。

window.java:arrayvisualizer.java:insertionsort.java:update

public class Window implements ActionListener  {
    
    //Window width
    protected static int WINDOW_WIDTH = 1980;
    //Window height
    protected static int WINDOW_HEIGHT = 1080;
    
    private int delay = 100;
    
    //This will draw all our rectangles 
    protected static ArrayVisualizer arrayVisualizer;
    
    //Make a new JFrame
    JFrame window = new JFrame();
    
    JButton shuffleBut = new JButton("Shuffle");
    
    JButton startSort = new JButton("Start Sorting");
    
    public Window() {
        initWindow();
    }
    
    private void addButtons() {
        
        //Shuffle Button
        shuffleBut.setBounds(100, 50, 100, 100);
        shuffleBut.setBackground(Color.white);
        shuffleBut.addActionListener(this);
        
        startSort.setBounds(500, 50, 100, 100);;
        startSort.setBackground(Color.white);
        startSort.addActionListener(taskPerformer);
        
        window.add(shuffleBut);
        window.add(startSort);
    }
    
    private void initWindow() {
        ImageIcon logo = new ImageIcon();
        
        window = new JFrame();
        window.setTitle("JAlgorithms");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        window.setLocationRelativeTo(null);
        window.setResizable(false);
        addButtons();
        
        arrayVisualizer = new ArrayVisualizer();
        window.add(arrayVisualizer);
        arrayVisualizer.repaint(); //Will call paint component method
        
        window.pack();
        window.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if(e.getSource() == shuffleBut) {
            arrayVisualizer.shuffle(ArrayVisualizer.array);
            arrayVisualizer.repaint();
        }
    }
    ActionListener taskPerformer = new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            if(event.getSource() == startSort) {
                arrayVisualizer.sort(ArrayVisualizer.array);
                arrayVisualizer.repaint();
            }   
        }
    };
    
}

这是修改的

public class ArrayVisualizer extends JPanel {
    
    protected static int[] array;
    private final int REC_WIDTH = 1; //1980 rectangles
    private final int NUMBER_OF_RECS = Window.WINDOW_WIDTH / REC_WIDTH;
    
    public ArrayVisualizer() {
        array = new int[NUMBER_OF_RECS];
        generateRandom(array);
    }
    
    
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D graphics = (Graphics2D) g.create();
        graphics.setColor(Color.orange);
        for(int i = 0; i < NUMBER_OF_RECS; i++) {
            
            int height = array[i] * 4; //Done for scaling
            int recX = i + (REC_WIDTH - 1) * i; //Read fillRect documentation
            int recY = Window.WINDOW_HEIGHT - height; //Read fillRect documentation
            
            graphics.fillRect(recX, recY, REC_WIDTH, height);
        }
    }
    
    
    //This will return the Dimension of the actual rectangles. i.e the rectangles will only exist when this exists, almost like a canvas in javascript
    @Override
    public Dimension getPreferredSize() {   
        return new Dimension(Window.WINDOW_WIDTH, Window.WINDOW_HEIGHT);
    }
    
    //Creates a random unsorted array with numbers 1-200
    protected void generateRandom(int[] array) {
        Random number = new Random();
        for(int i = 0; i < NUMBER_OF_RECS; i++) {
            array[i] = number.nextInt(200);
        }
    }
    
    protected void shuffle(int[] array) {
         generateRandom(array);
    }
    
    protected void sort(int[] array) {
         InsertionSort.insertionSort(array);
    }
}

public class InsertionSort {
    public static void insertionSort(int arr[])
    {
        int n = arr.length;
        
        
        for (int i = 1; i < n; ++i) {

            int key = arr[i];
            int j = i - 1;
    
            /* Move elements of arr[0..i-1], that are
               greater than key, to one position ahead
               of their current position */
            while (j >= 0 && arr[j] > key) {
                arr[j + 1] = arr[j];
                j = j - 1;
                
            }
            arr[j + 1] = key;
            
        }
    }
}

代码,我还添加了选择排序,并重新修改了我检查的建议):

main.java:

public class Main {

    public static void main(String[] args) {
        
        //Opens window
        Window window = new Window();
    }
}

window.java

import java.awt.Color;
import java.awt.Image;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.EventQueue;
import javax.swing.ImageIcon;
import java.awt.Graphics2D;
import java.awt.Graphics;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class Window implements ActionListener  {
    
    //Window width
    protected static int WINDOW_WIDTH = 1440;
    //Window height
    protected static int WINDOW_HEIGHT = 1080;
        
    //This will draw all our rectangles 
    protected static ArrayVisualizer arrayVisualizer;
    
    //Make a new JFrame
    JFrame window = new JFrame();
    
    JButton shuffleBut = new JButton("Shuffle");
    
    JButton startSort = new JButton("Start Sorting");
    
    public Window() {
        initWindow();
    }
    
    private void addButtons() {
        
        //Shuffle Button
        shuffleBut.setBounds(100, 50, 100, 100);
        shuffleBut.setBackground(Color.white);
        shuffleBut.addActionListener(this);
        
        startSort.setBounds(500, 50, 100, 100);;
        startSort.setBackground(Color.white);
        
        window.add(shuffleBut);
        window.add(startSort);

    }
    
    private void initWindow() {
        ImageIcon logo = new ImageIcon();
        
        window = new JFrame();
        window.setTitle("JAlgorithms");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        window.setLocationRelativeTo(null);
        window.setResizable(false);
        addButtons();
        
        startSort.addActionListener(taskPerformer);

        arrayVisualizer = new ArrayVisualizer();
        window.add(arrayVisualizer);
        arrayVisualizer.repaint(); //Will call paint component method       
        
        window.pack();
        window.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if(e.getSource() == shuffleBut) {
            arrayVisualizer.shuffle(ArrayVisualizer.array);
            arrayVisualizer.repaint();
        }
    }
    
    
    ActionListener taskPerformer = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            if(event.getSource() == startSort) {
                if(!timer.isRunning()) { //If timer is not running
                    timer.setInitialDelay(0); //Set initial delay
                    timer.start();  //Start the timer
                }
            }   
        }
    };
    
    ActionListener sortWithDelay = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if(sorted(ArrayVisualizer.array)) { //If it is sorted
                timer.stop();   //Stop the timer
                return;
            } else {
                arrayVisualizer.sort(ArrayVisualizer.array); //If it is not sorted continue the sort
                arrayVisualizer.repaint();  //Called after each swap
            }
        }
    };
    
    private int delay = 10; //Milliseconds
    private Timer timer = new Timer(delay, sortWithDelay);
    
    private boolean sorted(int[] array) {
        for(int i = 0; i < array.length - 1; i++) {
            if(array[i] > array[i+1]) {
                return false;
            }
        }
        return true;
    }
    
}

import java.awt.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Array;

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

public class ArrayVisualizer extends JPanel {
    
    private static final long serialVersionUID = 1L;
    private InsertionSort insertionSort = new InsertionSort();
    private SelectionSort selectionSort = new SelectionSort();
    protected static int[] array;
    private final int REC_WIDTH = 1; //1980 rectangles
    private final int NUMBER_OF_RECS = Window.WINDOW_WIDTH / REC_WIDTH;
    private final int[] barColors;
    
    public ArrayVisualizer() {
        setBackground(Color.black);
        array = new int[NUMBER_OF_RECS];
        barColors = new int[NUMBER_OF_RECS];
        generateRandom(array);
    }
    
    
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D graphics = (Graphics2D) g.create();
        graphics.setColor(Color.white);
        for(int i = 0; i < NUMBER_OF_RECS; i++) {
            
            int height = array[i] * 4; //Done for scaling
            int recX = i + (REC_WIDTH - 1) * i; //Read fillRect documentation
            int recY = Window.WINDOW_HEIGHT - height; //Read fillRect documentation
            
            
            graphics.fillRect(recX, recY, REC_WIDTH, height);
            
            
        }
    }
    
    
    //This will return the Dimension of the actual rectangles. i.e the rectangles will only exist when this exists, almost like a canvas in javascript
    @Override
    public Dimension getPreferredSize() {   
        return new Dimension(Window.WINDOW_WIDTH, Window.WINDOW_HEIGHT);
    }
    
    //Creates a random unsorted array with numbers 1-200
    protected void generateRandom(int[] array) {
        Random number = new Random();
        for(int i = 0; i < NUMBER_OF_RECS; i++) {
            array[i] = number.nextInt(200);
        }
    }
    
    protected void shuffle(int[] array) {
         generateRandom(array);
    }
    
    protected void sort(int[] array) {
        selectionSort.sortWithDelay(array);
    }
}

arrayvisualizer.java :

public class InsertionSort {
    
        private int i = 1;
        private int j = 0;

        public void sortWithDelay(int arr[])
        {
            int n = arr.length;
            
            if(i < n) {

                int key = arr[i];
                j = i - 1;
        
                /* Move elements of arr[0..i-1], that are
                   greater than key, to one position ahead
                   of their current position */
                while(j >= 0 && arr[j] > key) {
                    arr[j + 1] = arr[j];
                    j = j - 1;
                }
                arr[j + 1] = key;
                ++i;
            }
        }
}

selectionsort.java:

public class SelectionSort {

    private int i = 1;
    private int j = 0;
    
      public void sortWithDelay(int arr[])
        {
            int n = arr.length - 1;
      
            if(i < n) {
                
                int min_index = i;
                
                for(int j = i + 1; j < n; j++) {
                    if(arr[j] < arr[min_index]) {
                        min_index = j;
                    }
                }
                    int temp = arr[min_index];
                    arr[min_index] = arr[i];
                    arr[i] = temp;
                    ++i;
            }          
         }
      }

I have been making a sorting algorithm visualizer in Java using Java Swing and AWT and coded insertion sort to see if it would be able to work. The algorithm works in the sense that when this program is run you can shuffle and sort the algorithm using insertion short but you don't actually see the algorithm in action. It just happens instantly and have been looking for ways to add some sort of delay but I can't find any sources to help me.

I set it up with 4 java classes, the main which just init's the window, the window class, array visualizer class, and the insertion sort class.

Window.java:

public class Window implements ActionListener  {
    
    //Window width
    protected static int WINDOW_WIDTH = 1980;
    //Window height
    protected static int WINDOW_HEIGHT = 1080;
    
    private int delay = 100;
    
    //This will draw all our rectangles 
    protected static ArrayVisualizer arrayVisualizer;
    
    //Make a new JFrame
    JFrame window = new JFrame();
    
    JButton shuffleBut = new JButton("Shuffle");
    
    JButton startSort = new JButton("Start Sorting");
    
    public Window() {
        initWindow();
    }
    
    private void addButtons() {
        
        //Shuffle Button
        shuffleBut.setBounds(100, 50, 100, 100);
        shuffleBut.setBackground(Color.white);
        shuffleBut.addActionListener(this);
        
        startSort.setBounds(500, 50, 100, 100);;
        startSort.setBackground(Color.white);
        startSort.addActionListener(taskPerformer);
        
        window.add(shuffleBut);
        window.add(startSort);
    }
    
    private void initWindow() {
        ImageIcon logo = new ImageIcon();
        
        window = new JFrame();
        window.setTitle("JAlgorithms");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        window.setLocationRelativeTo(null);
        window.setResizable(false);
        addButtons();
        
        arrayVisualizer = new ArrayVisualizer();
        window.add(arrayVisualizer);
        arrayVisualizer.repaint(); //Will call paint component method
        
        window.pack();
        window.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if(e.getSource() == shuffleBut) {
            arrayVisualizer.shuffle(ArrayVisualizer.array);
            arrayVisualizer.repaint();
        }
    }
    ActionListener taskPerformer = new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            if(event.getSource() == startSort) {
                arrayVisualizer.sort(ArrayVisualizer.array);
                arrayVisualizer.repaint();
            }   
        }
    };
    
}

ArrayVisualizer.java:

public class ArrayVisualizer extends JPanel {
    
    protected static int[] array;
    private final int REC_WIDTH = 1; //1980 rectangles
    private final int NUMBER_OF_RECS = Window.WINDOW_WIDTH / REC_WIDTH;
    
    public ArrayVisualizer() {
        array = new int[NUMBER_OF_RECS];
        generateRandom(array);
    }
    
    
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D graphics = (Graphics2D) g.create();
        graphics.setColor(Color.orange);
        for(int i = 0; i < NUMBER_OF_RECS; i++) {
            
            int height = array[i] * 4; //Done for scaling
            int recX = i + (REC_WIDTH - 1) * i; //Read fillRect documentation
            int recY = Window.WINDOW_HEIGHT - height; //Read fillRect documentation
            
            graphics.fillRect(recX, recY, REC_WIDTH, height);
        }
    }
    
    
    //This will return the Dimension of the actual rectangles. i.e the rectangles will only exist when this exists, almost like a canvas in javascript
    @Override
    public Dimension getPreferredSize() {   
        return new Dimension(Window.WINDOW_WIDTH, Window.WINDOW_HEIGHT);
    }
    
    //Creates a random unsorted array with numbers 1-200
    protected void generateRandom(int[] array) {
        Random number = new Random();
        for(int i = 0; i < NUMBER_OF_RECS; i++) {
            array[i] = number.nextInt(200);
        }
    }
    
    protected void shuffle(int[] array) {
         generateRandom(array);
    }
    
    protected void sort(int[] array) {
         InsertionSort.insertionSort(array);
    }
}

InsertionSort.java:

public class InsertionSort {
    public static void insertionSort(int arr[])
    {
        int n = arr.length;
        
        
        for (int i = 1; i < n; ++i) {

            int key = arr[i];
            int j = i - 1;
    
            /* Move elements of arr[0..i-1], that are
               greater than key, to one position ahead
               of their current position */
            while (j >= 0 && arr[j] > key) {
                arr[j + 1] = arr[j];
                j = j - 1;
                
            }
            arr[j + 1] = key;
            
        }
    }
}

UPDATE (Here is the modified code, I added selection sort as well and heavily modified the suggestion I checkmarked):

Main.java:

public class Main {

    public static void main(String[] args) {
        
        //Opens window
        Window window = new Window();
    }
}

Window.java:

import java.awt.Color;
import java.awt.Image;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.EventQueue;
import javax.swing.ImageIcon;
import java.awt.Graphics2D;
import java.awt.Graphics;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class Window implements ActionListener  {
    
    //Window width
    protected static int WINDOW_WIDTH = 1440;
    //Window height
    protected static int WINDOW_HEIGHT = 1080;
        
    //This will draw all our rectangles 
    protected static ArrayVisualizer arrayVisualizer;
    
    //Make a new JFrame
    JFrame window = new JFrame();
    
    JButton shuffleBut = new JButton("Shuffle");
    
    JButton startSort = new JButton("Start Sorting");
    
    public Window() {
        initWindow();
    }
    
    private void addButtons() {
        
        //Shuffle Button
        shuffleBut.setBounds(100, 50, 100, 100);
        shuffleBut.setBackground(Color.white);
        shuffleBut.addActionListener(this);
        
        startSort.setBounds(500, 50, 100, 100);;
        startSort.setBackground(Color.white);
        
        window.add(shuffleBut);
        window.add(startSort);

    }
    
    private void initWindow() {
        ImageIcon logo = new ImageIcon();
        
        window = new JFrame();
        window.setTitle("JAlgorithms");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        window.setLocationRelativeTo(null);
        window.setResizable(false);
        addButtons();
        
        startSort.addActionListener(taskPerformer);

        arrayVisualizer = new ArrayVisualizer();
        window.add(arrayVisualizer);
        arrayVisualizer.repaint(); //Will call paint component method       
        
        window.pack();
        window.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if(e.getSource() == shuffleBut) {
            arrayVisualizer.shuffle(ArrayVisualizer.array);
            arrayVisualizer.repaint();
        }
    }
    
    
    ActionListener taskPerformer = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            if(event.getSource() == startSort) {
                if(!timer.isRunning()) { //If timer is not running
                    timer.setInitialDelay(0); //Set initial delay
                    timer.start();  //Start the timer
                }
            }   
        }
    };
    
    ActionListener sortWithDelay = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if(sorted(ArrayVisualizer.array)) { //If it is sorted
                timer.stop();   //Stop the timer
                return;
            } else {
                arrayVisualizer.sort(ArrayVisualizer.array); //If it is not sorted continue the sort
                arrayVisualizer.repaint();  //Called after each swap
            }
        }
    };
    
    private int delay = 10; //Milliseconds
    private Timer timer = new Timer(delay, sortWithDelay);
    
    private boolean sorted(int[] array) {
        for(int i = 0; i < array.length - 1; i++) {
            if(array[i] > array[i+1]) {
                return false;
            }
        }
        return true;
    }
    
}

ArrayVisualizer.java:

import java.awt.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Array;

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

public class ArrayVisualizer extends JPanel {
    
    private static final long serialVersionUID = 1L;
    private InsertionSort insertionSort = new InsertionSort();
    private SelectionSort selectionSort = new SelectionSort();
    protected static int[] array;
    private final int REC_WIDTH = 1; //1980 rectangles
    private final int NUMBER_OF_RECS = Window.WINDOW_WIDTH / REC_WIDTH;
    private final int[] barColors;
    
    public ArrayVisualizer() {
        setBackground(Color.black);
        array = new int[NUMBER_OF_RECS];
        barColors = new int[NUMBER_OF_RECS];
        generateRandom(array);
    }
    
    
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D graphics = (Graphics2D) g.create();
        graphics.setColor(Color.white);
        for(int i = 0; i < NUMBER_OF_RECS; i++) {
            
            int height = array[i] * 4; //Done for scaling
            int recX = i + (REC_WIDTH - 1) * i; //Read fillRect documentation
            int recY = Window.WINDOW_HEIGHT - height; //Read fillRect documentation
            
            
            graphics.fillRect(recX, recY, REC_WIDTH, height);
            
            
        }
    }
    
    
    //This will return the Dimension of the actual rectangles. i.e the rectangles will only exist when this exists, almost like a canvas in javascript
    @Override
    public Dimension getPreferredSize() {   
        return new Dimension(Window.WINDOW_WIDTH, Window.WINDOW_HEIGHT);
    }
    
    //Creates a random unsorted array with numbers 1-200
    protected void generateRandom(int[] array) {
        Random number = new Random();
        for(int i = 0; i < NUMBER_OF_RECS; i++) {
            array[i] = number.nextInt(200);
        }
    }
    
    protected void shuffle(int[] array) {
         generateRandom(array);
    }
    
    protected void sort(int[] array) {
        selectionSort.sortWithDelay(array);
    }
}

InsertionSort.java:

public class InsertionSort {
    
        private int i = 1;
        private int j = 0;

        public void sortWithDelay(int arr[])
        {
            int n = arr.length;
            
            if(i < n) {

                int key = arr[i];
                j = i - 1;
        
                /* Move elements of arr[0..i-1], that are
                   greater than key, to one position ahead
                   of their current position */
                while(j >= 0 && arr[j] > key) {
                    arr[j + 1] = arr[j];
                    j = j - 1;
                }
                arr[j + 1] = key;
                ++i;
            }
        }
}

SelectionSort.java:

public class SelectionSort {

    private int i = 1;
    private int j = 0;
    
      public void sortWithDelay(int arr[])
        {
            int n = arr.length - 1;
      
            if(i < n) {
                
                int min_index = i;
                
                for(int j = i + 1; j < n; j++) {
                    if(arr[j] < arr[min_index]) {
                        min_index = j;
                    }
                }
                    int temp = arr[min_index];
                    arr[min_index] = arr[i];
                    arr[i] = temp;
                    ++i;
            }          
         }
      }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱殇璃 2025-02-19 19:15:51

致电A swing Timer 每秒都会返回的每秒一个中间阵列。然后绘制此中间阵列。重复直到排序阵列。

要获得中间数组,请将插入式座从静态方法修改为类方法,该方法将存储变量i,以便我们可以在再次调用时恢复排序。

public class Window implements ActionListener  {
    
    //Window width
    protected static int WINDOW_WIDTH = 1980;
    //Window height
    protected static int WINDOW_HEIGHT = 1080;
    
    //This will draw all our rectangles 
    protected static ArrayVisualizer arrayVisualizer;
    
    //Make a new JFrame
    JFrame window = new JFrame();
    
    JButton shuffleBut = new JButton("Shuffle");
    
    JButton startSort = new JButton("Start Sorting");
    
    public Window() {
        initWindow();
    }
    
    private void addButtons() {
        
        //Shuffle Button
        shuffleBut.setBounds(100, 50, 100, 100);
        shuffleBut.setBackground(Color.white);
        shuffleBut.addActionListener(this);
        
        startSort.setBounds(500, 50, 100, 100);;
        startSort.setBackground(Color.white);
        startSort.addActionListener(taskPerformer);
        
        window.add(shuffleBut);
        window.add(startSort);
    }
    
    private void initWindow() {
        ImageIcon logo = new ImageIcon();
        
        window = new JFrame();
        window.setTitle("JAlgorithms");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        window.setLocationRelativeTo(null);
        window.setResizable(false);
        addButtons();
        
        arrayVisualizer = new ArrayVisualizer();
        window.add(arrayVisualizer);
        arrayVisualizer.repaint(); //Will call paint component method
        
        window.pack();
        window.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if(e.getSource() == shuffleBut) {
            arrayVisualizer.shuffle(ArrayVisualizer.array);
            arrayVisualizer.repaint();
        }
    }

    private int delay = 1000; // delay is 1s
    private Timer timer = new Timer(delay, sortWithDelay);

    ActionListener taskPerformer = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            if(event.getSource() == startSort) {
                // Start the timer that shows a swap every second
                if(!timer.isRunning()) {
                    timer.setInitialDelay(0);
                    timer.start();
                }
            }   
        }
    };

    ActionListener sortWithDelay = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            if(isSorted(ArrayVisualizer.array)) {
                timer.stop();
                return;
            }
            arrayVisualizer.sort(ArrayVisualizer.array);
            arrayVisualizer.repaint();
        }
    };

    private boolean isSorted(int[] arr) {
        for (int i = 0; i < a.length - 1; i++) {
            if (a[i] > a[i + 1]) {
                return false;
            }
        }
        return true;
    }   
}
public class ArrayVisualizer extends JPanel {
    
    private InsertionSort insertionSort = new InsertionSort();
    protected static int[] array;
    private final int REC_WIDTH = 1; //1980 rectangles
    private final int NUMBER_OF_RECS = Window.WINDOW_WIDTH / REC_WIDTH;
    
    public ArrayVisualizer() {
        array = new int[NUMBER_OF_RECS];
        generateRandom(array);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D graphics = (Graphics2D) g.create();
        graphics.setColor(Color.orange);
        for(int i = 0; i < NUMBER_OF_RECS; i++) {
            
            int height = array[i] * 4; //Done for scaling
            int recX = i + (REC_WIDTH - 1) * i; //Read fillRect documentation
            int recY = Window.WINDOW_HEIGHT - height; //Read fillRect documentation
            
            graphics.fillRect(recX, recY, REC_WIDTH, height);
        }
    }
        
    //This will return the Dimension of the actual rectangles. i.e the rectangles will only exist when this exists, almost like a canvas in javascript
    @Override
    public Dimension getPreferredSize() {   
        return new Dimension(Window.WINDOW_WIDTH, Window.WINDOW_HEIGHT);
    }
    
    //Creates a random unsorted array with numbers 1-200
    protected void generateRandom(int[] array) {
        Random number = new Random();
        for(int i = 0; i < NUMBER_OF_RECS; i++) {
            array[i] = number.nextInt(200);
        }
    }
    
    protected void shuffle(int[] array) {
         generateRandom(array);
    }
    
    protected void sort(int[] array) {
         insertionSort.sortWithDelay(array);
    }
}
public class InsertionSort {

    private int i = 1;

    public void sortWithDelay(int arr[])
    {
        int n = arr.length;
        
        if(i < n) {

            int key = arr[i];
            int j = i - 1;
    
            /* Move elements of arr[0..i-1], that are
               greater than key, to one position ahead
               of their current position */
            while(j >= 0 && arr[j] > key) {
                arr[j + 1] = arr[j];
                j = j - 1;
            }
            arr[j + 1] = key;
            ++i;
        }
    }
}

Call an action with a swing timer every second that will return an intermediate array. Then paint this intermediate array. Repeat until the array is sorted.

To obtain an intermediate array, modify the InsertionSort from a static method to a class method that will store the variable i so that we can resume sorting when called again.

public class Window implements ActionListener  {
    
    //Window width
    protected static int WINDOW_WIDTH = 1980;
    //Window height
    protected static int WINDOW_HEIGHT = 1080;
    
    //This will draw all our rectangles 
    protected static ArrayVisualizer arrayVisualizer;
    
    //Make a new JFrame
    JFrame window = new JFrame();
    
    JButton shuffleBut = new JButton("Shuffle");
    
    JButton startSort = new JButton("Start Sorting");
    
    public Window() {
        initWindow();
    }
    
    private void addButtons() {
        
        //Shuffle Button
        shuffleBut.setBounds(100, 50, 100, 100);
        shuffleBut.setBackground(Color.white);
        shuffleBut.addActionListener(this);
        
        startSort.setBounds(500, 50, 100, 100);;
        startSort.setBackground(Color.white);
        startSort.addActionListener(taskPerformer);
        
        window.add(shuffleBut);
        window.add(startSort);
    }
    
    private void initWindow() {
        ImageIcon logo = new ImageIcon();
        
        window = new JFrame();
        window.setTitle("JAlgorithms");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        window.setLocationRelativeTo(null);
        window.setResizable(false);
        addButtons();
        
        arrayVisualizer = new ArrayVisualizer();
        window.add(arrayVisualizer);
        arrayVisualizer.repaint(); //Will call paint component method
        
        window.pack();
        window.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if(e.getSource() == shuffleBut) {
            arrayVisualizer.shuffle(ArrayVisualizer.array);
            arrayVisualizer.repaint();
        }
    }

    private int delay = 1000; // delay is 1s
    private Timer timer = new Timer(delay, sortWithDelay);

    ActionListener taskPerformer = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            if(event.getSource() == startSort) {
                // Start the timer that shows a swap every second
                if(!timer.isRunning()) {
                    timer.setInitialDelay(0);
                    timer.start();
                }
            }   
        }
    };

    ActionListener sortWithDelay = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            if(isSorted(ArrayVisualizer.array)) {
                timer.stop();
                return;
            }
            arrayVisualizer.sort(ArrayVisualizer.array);
            arrayVisualizer.repaint();
        }
    };

    private boolean isSorted(int[] arr) {
        for (int i = 0; i < a.length - 1; i++) {
            if (a[i] > a[i + 1]) {
                return false;
            }
        }
        return true;
    }   
}
public class ArrayVisualizer extends JPanel {
    
    private InsertionSort insertionSort = new InsertionSort();
    protected static int[] array;
    private final int REC_WIDTH = 1; //1980 rectangles
    private final int NUMBER_OF_RECS = Window.WINDOW_WIDTH / REC_WIDTH;
    
    public ArrayVisualizer() {
        array = new int[NUMBER_OF_RECS];
        generateRandom(array);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D graphics = (Graphics2D) g.create();
        graphics.setColor(Color.orange);
        for(int i = 0; i < NUMBER_OF_RECS; i++) {
            
            int height = array[i] * 4; //Done for scaling
            int recX = i + (REC_WIDTH - 1) * i; //Read fillRect documentation
            int recY = Window.WINDOW_HEIGHT - height; //Read fillRect documentation
            
            graphics.fillRect(recX, recY, REC_WIDTH, height);
        }
    }
        
    //This will return the Dimension of the actual rectangles. i.e the rectangles will only exist when this exists, almost like a canvas in javascript
    @Override
    public Dimension getPreferredSize() {   
        return new Dimension(Window.WINDOW_WIDTH, Window.WINDOW_HEIGHT);
    }
    
    //Creates a random unsorted array with numbers 1-200
    protected void generateRandom(int[] array) {
        Random number = new Random();
        for(int i = 0; i < NUMBER_OF_RECS; i++) {
            array[i] = number.nextInt(200);
        }
    }
    
    protected void shuffle(int[] array) {
         generateRandom(array);
    }
    
    protected void sort(int[] array) {
         insertionSort.sortWithDelay(array);
    }
}
public class InsertionSort {

    private int i = 1;

    public void sortWithDelay(int arr[])
    {
        int n = arr.length;
        
        if(i < n) {

            int key = arr[i];
            int j = i - 1;
    
            /* Move elements of arr[0..i-1], that are
               greater than key, to one position ahead
               of their current position */
            while(j >= 0 && arr[j] > key) {
                arr[j + 1] = arr[j];
                j = j - 1;
            }
            arr[j + 1] = key;
            ++i;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文