简体   繁体   English

Java:图形在单击JButton时消失

[英]Java: Graphics Disappear upon clicking JButton

For my algorithms class we have to sort an array using mergesort and at each step draw the array as a histogram to graphically see what is going on. 对于我的算法类,我们必须使用mergesort对数组进行排序,并在每一步将数组绘制为直方图,以图形方式查看正在发生的事情。 I am running into a problem with the paintComponent method updating the array. 我遇到了paintComponent方法更新数组的问题。 When I run the program for the first time, it shows the jumbled array (as it should) and then when I click the mergesort button, I am expecting to see the sorted array but instead, I get nothing. 当我第一次运行程序时,它会显示混乱的数组(应该如此)然后当我单击mergesort按钮时,我希望看到排序的数组,但相反,我什么也得不到。 Any help would be appreciated, thanks. 任何帮助将不胜感激,谢谢。

Note: I know I'm not using mergesort now, I am just trying to get the graphics working first. 注意:我知道我现在不使用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();
    }
}

You need to reset your xPos variable, otherwise the x co-ordinate will appear off-screen: 您需要重置xPos变量,否则x坐标将出现在屏幕外:

int xPos = 0;

Aside: creating a new ArrayPanel is unnecessary — just resetting the variables and calling repaint will work by making xPos a class member variable of ArrayPanel . 另外:创建一个新的ArrayPanel是不必要的 - 只需重置变量并调用repaint就可以使xPos成为ArrayPanel的类成员变量。

You aren't reseting the xPos instance variable which really should not be an instance variable because you only need it in the paintComponent. 您没有重置xPos实例变量,它实际上不应该是实例变量,因为您只需要在paintComponent中使用它。 So get rid of it and move it to the paintComponent() method. 所以摆脱它并将其移动到paintComponent()方法。

You also don't need to remove and re-add the panel. 您也不需要删除并重新添加面板。 So here is what I did (note the removal of the statics! Those should be instance variables): 所以这就是我做的(注意删除静态!那些应该是实例变量):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM