简体   繁体   English

在Java中使用秒表来记录图形的经过时间

[英]Using stopwatch in java to record elapsed time of a drawing

I'm having trouble with a part of my assignment. 我在部分作业中遇到麻烦。 I've done the first part with no trouble, but when I try and record the time I can't seem to get it to work. 我已经很轻松地完成了第一部分,但是当我尝试记录时间时,似乎无法正常工作。 In the assignment it states 在作业中说明

"The circle drawing code shall be enclosed by elapsed time measurement using the StopWatch class provided." “必须使用提供的StopWatch类,通过经过时间的测量来附上圆圈绘图代码。”

Here is the circle code and the stopwatch class that was provided can anyone educate me as to how I can use this with the circle code? 这是圈子代码和提供的秒表类,任何人都可以教育我如何将其与圈子代码一起使用?

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

Here is the stopwatch class I was provided with. 这是我提供的秒表类。

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

You really must ask your teacher or TA about this, your instructions are unclear. 您确实必须向您的老师或助教问一下,您的指示不清楚。 However, my best guess is: 但是,我最好的猜测是:

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
}

The stopwatch starts when you construct it. 构造秒表时,它会启动。 You can get the time that has passed since it started by calling elapsedTime() . 您可以通过调用elapsedTime()获得自开始以来已经过去的时间。 I would recommend using it to determing the time that has passed during the DrawCircle constructor. 我建议使用它来确定DrawCircle构造函数中经过的时间。

Change the constructor to this: 将构造函数更改为此:

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
}

If you want to include the time that it takes to add the circle to the frame, you can use @markspace's answer. 如果要包括将圆添加到框架所需的时间,则可以使用@markspace的答案。

The timing code should be placed within the actual drawing code of your component. 时序代码应放在组件的实际工程图代码中。 Java Swing does its rendering in a separate thread so timing it from the main thread would simply measure the amount of time taken to initialize swing. Java Swing在一个单独的线程中进行渲染,因此从主线程对其进行计时将仅测量初始化swing所花费的时间。 The constructor is also executed on the main thread, so timing it does not measure the drawing time. 构造函数也在主线程上执行,因此计时不会测量绘制时间。 The drawing of the circle would be queued into a separate thread. 圆的绘制将排队到单独的线程中。 The actual drawing is done by the paintComponent method which fires in the aforementioned thread: 实际的绘制是通过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.");
}

Note that super must be the first method called so it can not be included in the measurement (nor should it be). 请注意,必须首先调用super,这样它才不能包含在测量中(也不应该包含在测量中)。

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

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