这是有bug版,生成(x,y)点的时候忘了考虑x是越来越大的,即x的值是沿着正方向变大的
import java.awt.*;
import java.applet.Applet;
import java.util.Random;
public class j18draw extends Applet
{
final int LENGTH = 50;
final int MIN_X = 10;
final int MAX_X = 450;
final int MIN_Y = 10;
final int MAX_Y = 290;
int pointsX[];
int pointsY[];
public void init()
{
pointsX = new int[LENGTH];
pointsY = new int[LENGTH];
Random rdX = new Random();
Random rdY = new Random();
for(int i = 0; i
{
int temp = rdX.nextInt(MAX_X + 1); //MAX_X + 1是因为nextInt(int value)生成的随机数区间为[0, value -1]
pointsX[i] = temp
temp = rdY.nextInt(MAX_Y + 1);
pointsY[i] = temp
}
}
public void start()
{
repaint();
}
public void paint(Graphics g)
{
g.drawLine(10, 150, 450, 150); //横轴
g.drawLine(10, 290, 10, 10); //纵轴
for(int i = 0; i
{
//把相邻的两个点连起来
g.drawLine(pointsX[i], pointsY[i], pointsX[i + 1], pointsY[i + 1]);
}
}
}
------------------------------------------------------------------------------------------
在HTML中使用
JAVA小程序 画坐标系
在浏览器中的效果如下图所示: