\input TeX4ht.sty
\Preamble{html,sty} \EndPreamble
\input ProTex.sty
\AlProTex{java,<<<>>>,list,title,`}
\HCode{}
\HPage{code}
A mini-turtle program.
\<<<
import java.applet.*;
import java.awt.*;
public class turtle extends Applet
{
Graphics g;
`
`
public void init()
{
`
`
}
public void paint(Graphics g) {
this.g = g;
`
}
public boolean action(Event e,Object o)
{
`
repaint();
return true;
}
}
>>>
The turtle can move forward without trace, move forward
while tracing its path, and rotate.
\<<<
void Move(int x, int y){ this.x += x; this.y -= y; }
void Move(int r){ x += (int) (r * Math.cos(d));
y -= (int) (r * Math.sin(d)); }
void Line(int x, int y){
g.drawLine( this.x, this.y, this.x+x, this.y-y );
Move(x,y);
}
void Line(int r){
Line( (new Double(r * Math.cos(d))).intValue(),
(new Double( r * Math.sin(d) )).intValue() ); }
void Rotate(int d){
this.d += d * 3.14 / 180;
while( 6.28 < this.d ){ this.d -= 6.28; }
while( this.d < 0 ){ this.d += 6.28; }
}
>>>
The text field may be modified by the users.
\<<<
Button moveButton,
lineButton,
clearButton,
rotateButton;
TextField in;
>>>
\<<<
clearButton = new Button("Clear"); add(clearButton);
moveButton = new Button("Move"); add(moveButton);
lineButton = new Button("Line"); add(lineButton);
rotateButton = new Button("Rotate"); add(rotateButton);
in = new TextField(3); add(in);
in.setText( "50" );
>>>
\<<<
int i;
x = 150; y = 150; d = 0;
for( i=0; i:{ Line( command[i+1] ); break; }
case `:{ Move( command[i+1] ); break; }
case `:{ Rotate( command[i+1] ); break; }
}
}
>>>
\<<<
if("Clear".equals(o)) { ` }
else{
if( "Line".equals(o)){ command[N]=`; }
if( "Move".equals(o)){ command[N]=`; }
if("Rotate".equals(o)){ command[N]=`; }
String s = new String( in.getText() );
Integer i = new Integer( s );
command[N+1]= i.intValue();
N+=2;
}
>>>
\<<<
int command[] = new int[300];
>>>
\<<<
1
>>>
\<<<
2
>>>
\<<<
3
>>>
\<<<
N = 0;
>>>
\<<<
int N;
int x, y;
double d;
>>>
\OutputCode\
\EndHPage{}
\bye