1

我目前正在制作一个算法可视化工具,我正在尝试将所有其他算法放在不同的类中。我收到了这个错误

无法访问 GUI 类型的封闭实例。

所以我 gui在行中添加了(在算法类中):

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

它修复了错误,但没有更新 EDT。我也尝试将所有内容都设为静态,但我的重绘在引用静态和非静态方面存在问题。我试图repaint()gui.repaint()or替换frame.repaint(),但这也不能解决它,所以我又回到了原点。这是我的代码(感叹号:代码已缩短):

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() );
        
    }
    
}

这是另一个类的代码(同一个包)

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) {}
                }
            }
        }   
    }
}

任何帮助将不胜感激。感谢您的阅读。

4

0 回答 0