Java applet: StarApplet
Java source code
import java.applet.* ;
import java.awt.* ;
// applet
public class StarApplet extends Applet {
private Star star;
public void init() {
setBackground( Color.white );
star = new Star( getGraphics() );
}
public void paint( Graphics g ) {
double tmp;
int x, y, steps, unit ;
//for( int i = 0 ; i < 5 ; i ++ ) {
while( true ) {
// random x-coordinate
tmp = Math.random() * 78 ; x = (int) tmp;
tmp = Math.random() * 86 ; y = (int) tmp;
tmp = Math.random() * 15 ; steps = (int) tmp + 3;
tmp = Math.random() * 10 ; unit = (int) tmp + 10;
//System.out.println(" x = " + x + ", y = " + y + ", steps = " + steps + ", unit = " + unit);
star.draw(x, y, steps, unit);
//sleep( 200 );
}
}
public void pauze( int millisec ) {
try { Thread.sleep( millisec ); }
catch( InterruptedException e ) { }
}
}
// star
class Star {
private Graphics g;
// default constructor
public Star( Graphics g ) {
this.g = g;
}
// draw a star
public void draw(int a, int b, int steps, int unit) {
int x1 = a,
x2 = a + (steps * unit),
y1 = b + (steps * unit),
y2 = b + (steps * unit),
opY = -1 ,
opX = 1 ;
g.setColor( Color.blue );
for( int i = 0 ; i <= steps * 4 ; i++ ) {
g.drawLine( x1, y1, x2, y2) ;
pauze( 100 ) ;
x1 = x1 + ( opX * unit );
y2 = y2 + ( opY * unit );
if( y2 == b ) { opY = 1; }
if( x1 == a + (steps * unit)*2 ) { opX = -1; }
if( y2 == b + (steps * unit)*2 ) { opY = -1; }
}
x1 = a ;
y2 = b + (steps * unit) ;
opY = -1 ;
opX = 1 ;
g.setColor( Color.white );
for( int i = 0 ; i <= steps * 4 ; i++ ) {
g.drawLine( x1, y1, x2, y2) ;
pauze( 100 ) ;
x1 = x1 + ( opX * unit );
y2 = y2 + ( opY * unit );
if( y2 == b ) { opY = 1; }
if( x1 == a + (steps * unit)*2 ) { opX = -1; }
if( y2 == b + (steps * unit)*2 ) { opY = -1; }
}
}
// sleep
public void pauze( int millisec ) {
try { Thread.sleep( millisec ); }
catch( InterruptedException e ) { }
}
}