1

I am currently making an Algorithm Visualizer and I am trying to put all the other algorithms in a different class. I was getting this error

No enclosing instance of type GUI is accessible.

So I added gui in the line (in the Algorithms class):

GUI.SortWorker sw = gui.new SortWorker(items);

It fixed the error, but it isn't updating the EDT. I also tried making everything static, but my repaint had issues about referencing static and non static. I tried to replace repaint() with gui.repaint() or frame.repaint(), but that wouldn't fix it either, and so I'm back to square one. Here is my code (exclaimer: code was shortened):

package proj;

@SuppressWarnings("serial")
class GUI extends JPanel
{
    static int screenWidth = 1276;
    static int screenHeight = 720;
    static int[] listArr = new int[126];
    
    public void setItems(int[] listArr)
    {
        GUI.listArr = listArr;
        repaint();
    }
    
    public void sort() {
        new SortWorker(listArr).execute();
    }
    
    public class SortWorker extends SwingWorker<Void, int[]> {
        private int[] items;
        public boolean isPub = false;

        public SortWorker(int[] unsortedItems) {
            items = Arrays.copyOf(unsortedItems, unsortedItems.length);
        }
        
        public void publishData(int[] items) {
            super.publish(Arrays.copyOf(items, items.length));
        }
        
        protected Void doInBackground() {
            Algorithims.HeapSort(items);
            return null;
        }
        

        @Override
        protected void process(List<int[]> list) {
            int[] items = list.get(list.size() - 1);
            setItems(items);
        }

        @Override
        protected void done() {}
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        this.setBackground(Color.darkGray);
        int width = 10;
        int x = 0;
        for (int i = 0; i < (screenWidth - 15) / width; i++) {
            g.setColor(Color.white);
            g.fillRect(x, (screenHeight - listArr[i]) - 40, width, listArr[i]);
            g.setColor(Color.black);
            g.drawRect(x, (screenHeight - listArr[i]) - 40, width, listArr[i]);
            x += width;
        }
        
    }
    
    public static void showGUI() {
        listArr = shuffleRectangles();
        JFrame frame = new JFrame();
        GUI gui = new GUI();
        gui.sort();
        
        
        frame.setTitle("Algorithim Visualizer");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(screenWidth, screenHeight); 
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.add(gui);
    }
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        EventQueue.invokeLater( () -> showGUI() );
        
    }
    
}

Here is the code from another class (same package)

package proj;

import proj.GUI.SortWorker;

public class Algorithims {

    public static void HeapSort(int[] items) {
        GUI gui = new GUI();
        GUI.SortWorker sw = gui.new SortWorker(items);

        for (int i = 0; i < items.length; i++) {
            for (int j = 0; j < items.length; j++) {
                if (items[i] < items[j]) {
                    //swapping i and j
                    int temp = items[i];
                    items[i] = items[j];
                    items[j] = temp;
                    
                    sw.publishData(items);
                    
                    try {
                        Thread.sleep(1); 
                    } catch (Exception e) {}
                }
            }
        }   
    }
}

Any help would be greatly appreciated. Thank you for reading.

Abra
  • 19,142
  • 7
  • 29
  • 41
Anon214
  • 68
  • 5
  • See: [Bubble Sort Animation](https://stackoverflow.com/questions/64196198/bubble-sort-animation) for examples of two different approaches. – camickr Mar 15 '21 at 02:27

0 Answers0