在java中使用秒表记录绘图的经过时间

时间:2014-07-22 23:37:18

标签: java stopwatch

我的部分工作遇到了麻烦。我没有遇到任何问题,我已经完成了第一部分,但是当我尝试记录时间时,我似乎无法让它发挥作用。在作业中说明了

"圆形图代码应使用StopWatch进行经过时间测量 提供课程。"

以下是圈子代码和提供的秒表课程,任何人都可以告诉我如何使用圈子代码?

import java.awt.*;
import java.awt.geom.GeneralPath;
import javax.swing.*;

public class DrawCircle extends JPanel
{
Point[] points;
GeneralPath circle;
final int INC = 5;

public DrawCircle()
{
    initPoints();

    initCircle();
}

private void initPoints()
{
    int numberOfPoints = 360/INC;
    points = new Point[numberOfPoints];
    double cx = 200.0;
    double cy = 200.0;
    double r = 100.0;
    // Dimension variables
    int count = 0;
    for(int theta = 0; theta < 360; theta+=INC)
    {
        int x = (int)(cx + r * Math.cos(Math.toRadians(theta)));
        int y = (int)(cy + r * Math.sin(Math.toRadians(theta)));
        points[count++] = new Point(x, y);
    }
}

private void initCircle()
{
    circle = new GeneralPath();
    for(int j = 0; j < points.length; j++)
    {
        if(j == 0)
            circle.moveTo(points[j].x, points[j].y);
        else
            circle.lineTo(points[j].x, points[j].y);
    }
    circle.closePath();
}

protected void paintComponent(Graphics g)
{
    // fill and color
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setPaint(Color.red);
    g2.fill(circle);
    g2.setPaint(Color.red);
    Point p1 = points[0];
    for(int j = 1; j <= points.length; j++)
    {
        Point p2 = points[j % points.length];
        g2.drawLine(p1.x, p1.y, p2.x, p2.y);
        // Line coordinates
        p1 = p2;
    }
}

public static void main(String[] args)
{
    //Main functions
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setContentPane(new DrawCircle());
    f.setSize(400,400);
    // Frame
    f.setLocation(200,200);
    // Center
    f.setVisible(true);
    }
}

这是我提供的秒表课程。

public class StopWatch {
    private final long before;

    StopWatch() {
        before = System.currentTimeMillis();
        //before = System.nanoTime();
    }

    public long elapsedTime() {
        long after = System.currentTimeMillis();
        //long after = System.nanoTime();
        return after - before;
   }
}

3 个答案:

答案 0 :(得分:0)

你真的必须问你的老师或TA这个,你的指示不清楚。但是,我最好的猜测是:

public static void main(String[] args)
{
    //Main functions
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    StopWatch sw = new StopWatch();   // <-- HERE
    f.setContentPane(new DrawCircle());
    f.setSize(400,400);
    // Frame
    f.setLocation(200,200);
    // Center
    f.setVisible(true);
    long time = sw.elapsedTime();  // <-- HERE
}

答案 1 :(得分:0)

构建秒表时,秒表会启动。您可以通过调用elapsedTime()来获取自开始以来经过的时间。我建议用它来确定DrawCircle构造函数中经过的时间。

将构造函数更改为:

public DrawCircle()
{
    StopWatch sw = new StopWatch(); // start stopwatch
    initPoints();
    initCircle();
    long time = sw.elapsedTime(); // end stopwatch
    System.out.println(time); // do something with the time. I chose to print it
}

如果您想要包含将圆圈添加到框架所需的时间,您可以使用@ markspace的答案。

答案 2 :(得分:0)

时序代码应放在组件的实际绘图代码中。 Java Swing在一个单独的线程中进行渲染,因此从主线程进行计时只会测量初始化swing所需的时间。构造函数也在主线程上执行,因此计时不会测量绘图时间。圆圈的绘制将排队到一个单独的线程中。实际绘图由paintComponent方法完成,该方法在上述线程中触发:

protected void paintComponent(Graphics g)
{
    // fill and color
    super.paintComponent(g);
    StopWatch sw = new StopWatch();   // <-- HERE
    Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setPaint(Color.red);
    g2.fill(circle);
    g2.setPaint(Color.red);
    Point p1 = points[0];
    for(int j = 1; j <= points.length; j++)
    {
        Point p2 = points[j % points.length];
        g2.drawLine(p1.x, p1.y, p2.x, p2.y);
        // Line coordinates
        p1 = p2;
    }
    long time = sw.elapsedTime();  // <-- HERE
    System.out.println("Circle took " + time + "ms to draw.");
}

请注意,super必须是第一个被调用的方法,因此它不能包含在测量中(也不应该包含在内)。