连续绘制点之间的线

时间:2014-04-28 21:30:17

标签: graph panel paintcomponent repaint points

您好我正在尝试实施流行的Wator模拟,但我的鱼群图表无法正常工作。出于某种原因,图表会上下移动一条线,而不是根据需要绘制每条曲线。这是代码:

/**Draws the graphs that will display the populations of Fish and Shark over time*/
public void paintComponent (Graphics g)
{
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    /**Graph the fish population */
    g2.setColor (Color.GREEN);
    ArrayList<Integer> pixelFish = convertToPixels(numFish);            //returns fish population as pixel values
    ArrayList<Point> points = new ArrayList<Point>();
    Point oldPoint = new Point (0, pixelFish.get(0));
    points.add(oldPoint);

    //adds all points to point arrayList
    for (int i = 1; i < pixelFish.size(); i++)
    {
        oldPoint = new Point (count,pixelFish.get(count));
        points.add(oldPoint);
    }

    //draws lines
    for (int j = 1; j < points.size(); j++)
    {
         g2.drawLine ( (int) points.get(j-1).getX(), (int) points.get(j-1).getY(), (int) points.get(j).getX(), (int) points.get(j).getY());     
    } 
}

0 个答案:

没有答案