简体   繁体   中英

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."

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() . I would recommend using it to determing the time that has passed during the DrawCircle constructor.

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.

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. 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:

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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