坚持绘制一组数据点(x,y)

时间:2015-03-31 23:08:47

标签: java swing

使用CMilbys建议编辑,但是在向框架添加“replayData”时出现错误。

这是replayData类+ paint方法

public class ReplayData extends JPanel {
/**
 * 
 */
private static final long serialVersionUID = 1L;
private ArrayList<Point> points;

// Create new list of points when ready then call Redraw()
public void ReplaceData() {
    points = new ArrayList<Point>();
}

public void addPoint(Point point) {
    points.add(point);
}

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);

    for (Point p : points) 
        g.fillRect(p.x, p.y, 2, 2);
}

public void draw() {
    repaint();
   }
}

这里是我尝试调用它来打印从csv中检索到的所有记录的地方

    JButton button_KMeans = new JButton("View K-Means");
    button_KMeans.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            kMeans.initialize();
            kMeans.kMeanCluster();

            //for (Point p : kMeans.getPoints() )
            // Will this be very slow? Data sets are going to be large
            Point temp = new Point();
            for (int i = 0; i < kMeans.TOTAL_DATA; i++)
            {           
                temp.x = (int)TrackerData.getRecordNumber(i).getEyeX();
                temp.y = (int)TrackerData.getRecordNumber(i).getEyeY();
                replayData.addPoint(temp); // Add points to JPanel
            }
            replayData.draw();  
            // How could I make it so this data shows over like 5 seconds, or over 30 etc?
        }
    });

将ReplayData实例添加到框架

时出现错误

at javax.swing.JFrame.addImpl(Unknown Source)

at java.awt.Container.add(Unknown Source)

 private ReplayData replayData;

 private void initialize() {

    frame = new JFrame();
    frame.setBounds(100, 100, 1920, 1080);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);
    frame.add(replayData);  // if I comment this line the program starts fine

1 个答案:

答案 0 :(得分:1)

在paintComponent方法中,您应该调用super.paintComponent。这是正确的,所以没有注释。

其次,您的ReplayData类扩展了JPanel。因此,您需要使用JFrame的add方法并将类的实例添加到JFrame。这只会让你有1分。所以我建议你重新构造你的类,以获得一个点数组,而不是两个整数变量,它看起来像它。例如,

class ReplayDate extends JPanel {
    private List<Point> points;

    public ReplaceData() {
        points = new ArrayList<Point>();
    }

    public void addPoint(Point point) {
        points.add(point);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        for (Point p : points) 
            g.fillRect(point.x, point.y, 2, 2);
    }

    public void draw() {
        repaint();
    }
}

// In your Main class
private ReplayData replayData;

private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 1920, 1080);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);
    frame.add(replayData); // Add replay data to jframe
    JButton button_KMeans = new JButton("View K-Means");
    button_KMeans.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        kMeans.initialize();
        kMeans.kMeanCluster();
        for (Point p : kMeans.getPoints()
            replayData.addPoint(p); // Add points to JPanel
        replayData.draw();  
    } 
}

这将允许您添加任意数量的点并将它们全部绘制出来。我希望这能回答你的问题。如果您仍然感到困惑,请发表评论,我会尝试解释一下。

编辑:帮助解决更多问题...... 第一:当设置JFrame的大小时,你的工作似乎没有任何关系,但是,我从来没有真正使用过这种方法。为了将来参考,它也可以这样做。

jframe.getContentPane().setPreferredSize(new Dimension(width, height));

至于在你的JFrame中添加一个ReplayData实例时,我不确定那个...我将你的代码复制到编译器中,它对我来说很好。发布更多代码或将项目发送给我,我可以深入了解一下。

最后,你担心速度。您的数据集有多大?这还取决于您的计算机。我的电脑配备2.4 GHz Intel Core i5。因为1赫兹是'1 /秒',假设我做了正确的数学运算,并且在理想的世界中,它每秒可以进行24亿次运算。这显然不是实际情况,但我的观点是即使数据集大约为10,000,您可能会注意到一个小延迟,但它只会是几秒钟。