\input TeX4ht.sty
\Preamble{html,sty} \EndPreamble
\input ProTex.sty
\AlProTex{java,<<<>>>,list,title,`}
\HCode{<APPLET CODE="turtle.class" WIDTH="300" HEIGHT="300"
> </APPLET>}
\HPage{code}
A mini-turtle program.
\<turtle\><<<
import java.applet.*;
import java.awt.*;
public class turtle extends Applet
{
Graphics g;
`<vars`>
`<turtle functions`>
public void init()
{
`<initial state`>
`<draw interface`>
}
public void paint(Graphics g) {
this.g = g;
`<execute commands`>
}
public boolean action(Event e,Object o)
{
`<get command`>
repaint();
return true;
}
}
>>>
The turtle can move forward without trace, move forward
while tracing its path, and rotate.
\<turtle functions\><<<
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.
\<vars\><<<
Button moveButton,
lineButton,
clearButton,
rotateButton;
TextField in;
>>>
\<draw interface\><<<
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" );
>>>
\<execute commands\><<<
int i;
x = 150; y = 150; d = 0;
for( i=0; i<N; i+=2 ){
switch( command[i] ) {
case `<Line op`>:{ Line( command[i+1] ); break; }
case `<Move op`>:{ Move( command[i+1] ); break; }
case `<Rotate op`>:{ Rotate( command[i+1] ); break; }
}
}
>>>
\<get command\><<<
if("Clear".equals(o)) { `<initial state`> }
else{
if( "Line".equals(o)){ command[N]=`<Line op`>; }
if( "Move".equals(o)){ command[N]=`<Move op`>; }
if("Rotate".equals(o)){ command[N]=`<Rotate op`>; }
String s = new String( in.getText() );
Integer i = new Integer( s );
command[N+1]= i.intValue();
N+=2;
}
>>>
\<vars\><<<
int command[] = new int[300];
>>>
\<Line op\><<<
1
>>>
\<Move op\><<<
2
>>>
\<Rotate op\><<<
3
>>>
\<initial state\><<<
N = 0;
>>>
\<vars\><<<
int N;
int x, y;
double d;
>>>
\OutputCode\<turtle\>
\EndHPage{}
\bye
[download source] |
code
|