我编写的这个程序应该绘制一个Sierpinski三角形,但每当我用appletviewer在低端计算机上打开它时,它通常会绘制4到60个点。如果我重新打开它而不重新编译它,点数偶尔会从通常的数量上升4.在家里的高端计算机上运行它最好在大约5 000 000次迭代。然而,虽然它绘制的形状相当饱满,但它开始画点,在那里不应该有点。这仅在数字开始变高时才会发生。为什么我的applet不能在低端计算机上运行,为什么它会以更大的值在不正确的位置绘制像素?这是代码:
import java.applet.Applet;
import java.awt.*;
import java.util.Random;
public class Triangle extends Applet implements Runnable {
Thread runner;
Random rand = new Random();
int xCord, yCord;
double lastX, lastY, currentX, currentY;
final double topX = 500, topY = 100, leftX = 200, leftY = 700,
rightX = 800, rightY = 700;
public void start() {
if (runner == null)
{
runner = new Thread(this);
runner.start();
}
}
public void init() {
lastX = 500; //Top dot
lastY = 100;
}
public void run() {
for(int counter = 1; counter <= 5000000; counter++) {
int n = rand.nextInt(3) + 1;
if (n == 1) { //Top dot
currentX = topX + (lastX - topX) / 2;
currentY = topY + (lastY - topY) / 2;
lastX = currentX;
lastY = currentY;
xCord = (int)Math.round(currentX);
yCord = (int)Math.round(currentY);
repaint();
} else if (n == 2) { //Left dot
currentX = leftX + (lastX - leftX) / 2;
currentY = leftY + (lastY - leftY) / 2;
lastX = currentX;
lastY = currentY;
xCord = (int)Math.round(currentX);
yCord = (int)Math.round(currentY);
repaint();
} else { //Right dot
currentX = rightX + (lastX - rightX) / 2;
currentY = rightY + (lastY - rightY) / 2;
lastX = currentX;
lastY = currentY;
xCord = (int)Math.round(currentX);
yCord = (int)Math.round(currentY);
repaint();
}
}
}
public void update(Graphics g) {
paint(g);
}
public void paint(Graphics g) {
g.fillRect(500, 100, 2, 2); //Top dot, dot 1
g.fillRect(200, 700, 2, 2); //Bottom left dot, dot 2
g.fillRect(800, 700, 2, 2); //Bottom right dot, dot 3
g.fillRect(xCord, yCord, 2, 2);
}
}
这是我的HTML文件:
<html>
<body>
<applet code = "Triangle.class" width=999 height=799>
</applet>
</body>
</html>