/** * Artificial Intelligence applet for the ThinkQuest contest. * This app demonstrates a "bee" moving around a screen, reacting * to its simple environment. It can CHASE, RETREAT, PATTERN, * CIRCLE and RANDOM, and can also incorporate a simple FSM into * it's logic. * @author Thomas Bak **/ import java.awt.*; import java.applet.*; public class AI extends Applet implements Runnable { Creature bee; // The little thing zipping around Thread tRunner; public void init() { super.init(); bee = new Creature(); setBackground(Color.white); //{{INIT_CONTROLS setLayout(null); resize(285,137); group1= new CheckboxGroup(); check1=new Checkbox("FSM",group1, false); check1.setFont(new Font("Courier",Font.BOLD,12)); add(check1); check1.reshape(7,8,84,15); check2=new Checkbox("Chase",group1, true); check2.setFont(new Font("Courier",Font.PLAIN,10)); add(check2); check2.reshape(7,28,84,15); check3=new Checkbox("Retreat",group1, false); check3.setFont(new Font("Courier",Font.PLAIN,10)); add(check3); check3.reshape(7,49,84,15); check4=new Checkbox("Pattern",group1, false); check4.setFont(new Font("Courier",Font.PLAIN,10)); add(check4); check4.reshape(7,68,84,15); check6=new Checkbox("Random",group1, false); check6.setFont(new Font("Courier",Font.PLAIN,10)); add(check6); check6.reshape(7,88,84,15); check5=new Checkbox("Circle",group1, false); check5.setFont(new Font("Courier",Font.PLAIN,10)); add(check5); check5.reshape(7,109,84,15); //}} } /** * Thread stuff **/ public void start() { if (tRunner == null) { tRunner = new Thread(this); tRunner.start(); } } /** * Thread stuff **/ public void stop() { if (tRunner != null) { tRunner.stop(); tRunner = null; } } /** * Use the sleep command as a timer for the bee. * When it finishes sleeping, move the bee a little, * draw it, and then sleep again. **/ public void run() { int i; while (true) { // Draw the bee repaint(); // Sleep a little (20X / second) try { Thread.sleep(50); } catch (InterruptedException e) { } // Move the bee a little bee.move(); } } /** * Get rid of that dang flicker! **/ public void update(Graphics g) { paint(g); } public void paint(Graphics g) { //g.setColor(Color.white); //g.fillRect(0,0, this.size().width, this.size().height); // Paint the bee bee.display(g); // Draw a pretty borderline g.setColor(Color.red); g.fillRect(95, 10, 2, 120); g.fillRect(95, 10, 180, 2); g.fillRect(95, 130, 180, 2); g.fillRect(275, 10, 2, 120); } public boolean handleEvent(Event event) { if (event.id == Event.ACTION_EVENT) { gotFocusCheck(event); return true; } else if (event.id == Event.MOUSE_MOVE && event.target == this) { mouseMoveThis(event); return true; } return super.handleEvent(event); } //{{DECLARE_CONTROLS CheckboxGroup group1; Checkbox check1; Checkbox check2; Checkbox check3; Checkbox check4; Checkbox check6; Checkbox check5; //}} public void mouseMoveThis(Event ev) { // Tell the bee that it's target has changed bee.setTarget(ev.x, ev.y); } public void gotFocusCheck(Event ev) { // Change the logic type of the bee int iLog = 1; if ( ev.target == check1 ) iLog = 1; if ( ev.target == check2 ) iLog = 2; if ( ev.target == check3 ) iLog = 3; if ( ev.target == check4 ) iLog = 4; if ( ev.target == check5 ) iLog = 5; if ( ev.target == check6 ) iLog = 6; bee.setLogic(iLog); } }