//
Source Mastering Java, Bill Buchanan. import java.util.*; import java.awt.*;
import java.applet.*;
public class chap15_04 extends Applet implements
Runnable { Thread timer = null; int x=100,y=100;//put alien in middle
of //screen public void paint(Graphics g) { g.setColor(getBackground());
alien(g); x+=(Math.random()*10)-5; y+=(Math.random()*10)-5;
if ( x>200) x=200; if ( y>200) y=200; if ( x<0 ) x=0; if ( y<0
) y=0; g.setColor(Color.blue); alien(g); } public void alien(Graphics
g) { g.fillOval(x,y,10,10); g.fillOval(x+30,y,10,10); g.fillOval(x,y+30,40,20);
g.drawLine(x+5,y+10,x+5,y+35); g.drawLine(x+35,y+10,x+35,y+35); }
public void start() { if(timer == null) { timer = new Thread(this);
timer.start(); } } public void stop() { timer = null;
} public void run() { while (timer != null) { delay(100);
// wait 100 ms repaint(); } timer = null; } public void
delay(int ms) { try {timer.sleep(ms);} catch (InterruptedException
e){} } }
|