This is a bug version. When generating (x, y) points, I forgot to consider that X is getting bigger and bigger, that is, the value of X is increased along the square direction.
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 <length; ++ I)
{
Int temp = RDX. nextint (max_x + 1); // max_x + 1 is because the random number range generated by nextint (INT value) is [0, value-1]
Pointsx [I] = temp <min_x? Temp + 10: temp; // generate X,
Temp = rdy. nextint (max_y + 1 );
Pointsy [I] = temp <min_y? Temp + 10: temp;
}
}
Public void start ()
{
Repaint ();
}
Public void paint (Graphics g)
{
G. drawline (10,150,450,150); // horizontal axis
G. drawline (10,290, 10, 10); // vertical axis
For (INT I = 0; I <(length-1); ++ I)
{
// Connect two adjacent points
G. drawline (pointsx [I], pointsy [I], pointsx [I + 1], pointsy [I + 1]);
}
}
}
Bytes ------------------------------------------------------------------------------------------
Use in HTML
<HTML>
<Head>
<Title>
Java Applet draw Coordinate System
</Title>
</Head>
<Body>
<APPLET code = "j18draw. Class" width = 500 Height = 300>
</APPLET>
</Body>
</Html>
Shows the effect in the browser: