简体   繁体   English

卡在绘制一组数据点(x,y)上

[英]Stuck on drawing a set of data points (x,y)

Edited with CMilbys suggestion, However i'm getting an error when adding "replayData to the frame. 使用CMilbys建议编辑,但是在框架中添加“ replayData”时出现错误。

Here is the replayData class + paint method 这是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();
   }
}

And here is where I try to call it to print all of the records that have been retrieved from the csv 这是我尝试调用它以打印从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?
        }
    });

I'm getting errors when adding the instance of ReplayData to the frame 将ReplayData实例添加到框架时出现错误

at javax.swing.JFrame.addImpl(Unknown Source) 在javax.swing.JFrame.addImpl(未知源)

at java.awt.Container.add(Unknown Source) 在java.awt.Container.add(未知来源)

 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

In your paintComponent method you should call super.paintComponent. 在paintComponent方法中,应调用super.paintComponent。 That was correct so uncomment that. 没错,这是正确的。

Secondly, your ReplayData class extends JPanel. 其次,您的ReplayData类扩展了JPanel。 Because of this you need to use JFrame's add method and add an instance of the class to the JFrame. 因此,您需要使用JFrame的add方法并将该类的实例添加到JFrame。 This will only allow you to have 1 point though. 不过,这只会让您得到1分。 So I recommend you re-structure your class to have an array of points and not two integer variables which it what it appears to be. 因此,我建议您将类重新构造为具有点数组,而不是看起来像的两个整数变量。 So for example 所以举个例子

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

This will allow you to add any number of points and draw them all. 这将允许您添加任意数量的点并将其全部绘制。 I hope this answered your question. 我希望这能回答您的问题。 If you are still confused just leave a comment and I will try to explain a little more. 如果您仍然感到困惑,请发表评论,我将尝试解释更多。

EDIT: To help with a few more problems... First: When setting the size of a JFrame, yours seems to work since you said nothing about it, however, I've never actually used that method. 编辑:为帮助解决其他一些问题...首先:设置JFrame的大小时,由于您对此一无所知,因此您似乎可以使用它,但是,我从未真正使用过该方法。 For future reference it can also be done like this. 为了将来参考,也可以这样进行。

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

As for when you add a ReplayData instance to your JFrame, I'm not sure about that one... I copied your code into a compiler and it worked fine for me. 至于当您将ReplayData实例添加到JFrame时,我不确定那个实例……我将您的代码复制到了编译器中,并且对我来说效果很好。 Post more code or send me your project and I can take a deeper look. 发布更多代码或将您的项目发送给我,我可以做更深入的了解。

Lastly, you're worried about speed. 最后,您担心速度。 How large are your datasets? 您的数据集有多大? This also depends on your computer. 这也取决于您的计算机。 My computer has a 2.4 GHz Intel Core i5. 我的计算机具有2.4 GHz英特尔酷睿i5。 Since 1 hertz is '1/second', assuming I did this math right, and in an ideal world it could do 2.4 billion operations per second. 因为1赫兹是“ 1 /秒”,所以假设我正确地进行了此数学运算,并且在理想情况下,它每秒可以进行24亿次操作。 This obviously isn't the actual case but my point is even with datasets of ~10,000, you will probably notice a small delay, but it will only be seconds. 显然这不是实际情况,但我的观点是即使使用约10,000个数据集,您也可能会注意到一个小的延迟,但这仅是几秒钟。

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

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