import java.applet.*;
import java.awt.*;
public class Simple extends Applet implements Runnable {
Controller controller = null;
Thread thread = null;
Image sheep = null;
int x = 50; // sprite's x axis
int y = 50; // sprite's y axis
Graphics buffer;
Image bufferImage;
int width; // applet's width
int height; // applet's width
public void init() {
this.sheep = getImage(getCodeBase(), "killer_sheep.gif");
}
public void start() {
Dimension d = getSize();
this.width = d.width;
this.height = d.height;
this.bufferImage = createImage(this.width, this.height);
this.buffer = bufferImage.getGraphics();
this.thread = new Thread(this);
}
public void stop() {
this.thread = null;
}
public void attach() {
try {
this.controller = new Joystick(this,
getParameter("stickarray"),
getParameter("device 1"));
} catch (Exception e) {
System.err.println("Couldn't attach to joystick "
+ "so switching to key based control: " + e);
this.controller = new Keyboard(this);
}
this.thread.start();
}
public void run() {
while (Thread.currentThread() == this.thread) {
// reads the state of the device
this.controller.poll();
// gets the axes
int x = this.controller.getX();
int y = this.controller.getY();
// advances the sprites position accordingly
if (x < 28672) this.x += (x - 28672)/2048;
else if (x > 36864) this.x += (x - 36864)/2048;
if (y < 28672) this.y += (y - 28672)/2048;
else if (y > 36864) this.y += (y - 36864)/2048;
repaint();
try {
Thread.sleep(40);
} catch (InterruptedException e) {}
}
}
public void paint(Graphics g) {
update(g);
}
public void update(Graphics g) {
this.buffer.setColor(getBackground());
this.buffer.fillRect(0, 0, this.width, this.height);
this.buffer.drawImage(this.sheep, this.x, this.y, this);
g.drawImage(bufferImage, 0, 0, this);
}