Java:图形在单击JButton时消失

时间:2013-03-28 00:34:15

标签: java arrays swing graphics jpanel

对于我的算法类,我们必须使用mergesort对数组进行排序,并在每一步将数组绘制为直方图,以图形方式查看正在发生的事情。我遇到了paintComponent方法更新数组的问题。当我第一次运行程序时,它会显示混乱的数组(应该如此)然后当我单击mergesort按钮时,我希望看到排序的数组,但相反,我什么也得不到。任何帮助将不胜感激,谢谢。

注意:我知道我现在没有使用mergesort,我只是想让图形先工作。

package a2;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;

import javax.swing.JButton; 
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class GraphicalSort extends JFrame implements ActionListener {
    static int[] data = new int[200];
    JPanel panel = new JPanel(); //Panel to hold graphical display of array
    JPanel buttonsPanel = new JPanel();
    JButton mButton = new JButton("Mergesort");

    int xPos = 0;
    static int barWidth = 8;
    static int barHeight = 1;

    public GraphicalSort() { 
        setLayout(new BorderLayout());
        mButton.addActionListener(this);
        buttonsPanel.add(mButton);
        for (int i = 0; i < data.length; i++) {
            data[i] = (int) (500 * Math.random() + 1);
        }
        setSize(barWidth * data.length, barHeight * 500 + buttonsPanel.getHeight());
        panel = new ArrayPanel();
        add(buttonsPanel, BorderLayout.NORTH);
        add(panel, BorderLayout.CENTER);
        repaint();
        validate();
    }

    public static void main(String[] args) {
        GraphicalSort gs = new GraphicalSort();
        gs.setTitle("Graphical Sort");
        gs.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gs.setLocationRelativeTo(null);
        gs.setResizable(false);
        gs.setVisible(true);
    }

    @SuppressWarnings("serial")
    class ArrayPanel extends JPanel {
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.BLACK);
            for (int i = 0; i < data.length; i++) {
                g.fillRect(xPos, (barHeight * 500) - (barHeight * data[i]), barWidth, barHeight * data[i]);
                xPos += barWidth;
            }   
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        remove(panel);
        if (e.getSource() == mButton) {
            Arrays.sort(data);
            panel = new ArrayPanel();
    }
        add(panel, BorderLayout.CENTER);
        repaint();
        validate();
    }
}

2 个答案:

答案 0 :(得分:2)

您需要重置xPos变量,否则x坐标将显示在屏幕外:

int xPos = 0;

除此之外:创建一个新的ArrayPanel是不必要的 - 只需重置变量并调用repaint就可以使xPos成为ArrayPanel的类成员变量。

答案 1 :(得分:2)

您没有重置xPos实例变量,它实际上不应该是实例变量,因为您只需要在paintComponent中使用它。所以摆脱它并将其移动到paintComponent()方法。

您也无需删除并重新添加面板。所以这就是我所做的(注意删除静态!那些应该是实例变量):

int[] data = new int[200];
JPanel panel;
JPanel buttonsPanel = new JPanel();
JButton mButton = new JButton("Mergesort");

int barWidth = 8;
int barHeight = 1;

class ArrayPanel extends JPanel {
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.BLACK);
        int xPos = 0;
        for (int i = 0; i < data.length; i++) {
            System.out.println("Drawing " + i);
            g.fillRect(xPos, (barHeight * 500) - (barHeight * data[i]), barWidth, barHeight * data[i]);
            xPos += barWidth;
        }   
    }
}


public void actionPerformed(ActionEvent e) {
    Arrays.sort(data);
    panel.repaint();
}