[英]Using stopwatch in java to record elapsed time of a drawing
我在部分作业中遇到麻烦。 我已经很轻松地完成了第一部分,但是当我尝试记录时间时,似乎无法正常工作。 在作业中说明
“必须使用提供的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;
}
}
您确实必须向您的老师或助教问一下,您的指示不清楚。 但是,我最好的猜测是:
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
}
构造秒表时,它会启动。 您可以通过调用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的答案。
时序代码应放在组件的实际工程图代码中。 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,这样它才不能包含在测量中(也不应该包含在测量中)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.