// file: DESCapplet.java // author: Robert Keller // purpose: Applet for drawing a Double-Ended Spam-Cruncher in action. /** * DESCapplet.java * @version 1 * @author Robert M. Keller * $Id: DESCapplet.java,v 1.16 2002/11/12 03:35:50 keller Exp $ */ import java.applet.*; // applet classes import java.awt.*; // Abstract Window Toolkit classes /** * Applet for drawing DESC */ public class DESCapplet extends Applet implements Runnable { Label banner = new Label("DESC Applet "); Color backgroundColor = Color.white, foregroundColor = Color.black; Font mainBold = new Font("Helvetica", Font.BOLD, 18); static int delay[] = {25, 50, 100, 200, 400, 800, 1600}; // delay values static int currentDelay = 50; // delay int xGridOffset = 70; // offset for the grid from upper left corner int yGridOffset = 70; int gridSize = 20; // size of one square of the grid int xString = 50; // location of String indicating no. of steps int yString = 50; String stepsTakenString = " steps taken"; // suffix of string for steps int stepsTaken; // number of steps taken by the DESC DESC desc = null; Thread myThread; Image image; Graphics graphics; // graphics buffer Choice sampleChoice; // choice menu for samples Choice delayChoice; // choice menu for delay Sample sample[]; // array of samples AppletGrid grid; // Grid extended for this applet /** * Initialize the applet. */ public void init() { setBackground(backgroundColor); // set the background color image = createImage(size().width, size().height); graphics = image.getGraphics(); // make a graphics buffer graphics.setFont(mainBold); banner.setFont(mainBold); add(banner); // Make Choice menu for selecting example. sampleChoice = new Choice(); sampleChoice.setFont(mainBold); add(sampleChoice); sample = Sample.makeSamples(); setSamples(sampleChoice, sample); // Make Choice menu for selecting delay. delayChoice = new Choice(); delayChoice.setFont(mainBold); add(delayChoice); setDelays(delayChoice, delay); } /** * Act on a choice from a menu. */ public boolean action(Event event, Object arg) { if( event.target == sampleChoice ) { // choose the input sample based on its index in the menu int index = sampleChoice.getSelectedIndex(); if( index > 0 ) { setInput(index-1); stepsTaken = 0; } } else if( event.target == delayChoice ) { // choose a delay value based on its index in the menu int index = delayChoice.getSelectedIndex(); if( index > 0 ) { currentDelay = delay[index-1]; } } return super.action(event, arg); } /** * Act on a mouseDown event. */ public boolean mouseDown(Event e, int x, int y) { int row = (y-yGridOffset)/gridSize; int col = (x-xGridOffset)/gridSize; if( grid.inRange(row, col) ) { if( grid.hasSpam(row, col) ) { grid.setBlank(row, col); } else if( grid.isBlank(row, col) ) { grid.setSpam(row, col); } } return true; } /** * Act on a mouseDrag event. */ public boolean mouseDrag(Event e, int x, int y) { return mouseDown(e, x, y); } /** * Set the sample titles in a choice menu. */ void setSamples(Choice sampleChoice, Sample sample[]) { sampleChoice.addItem("Choose Sample"); int n = sample.length; for( int j = 1; j <= n; j++ ) { sampleChoice.addItem(sample[j-1].getTitle()); } } /** * Set the delay values in a choice menu. */ void setDelays(Choice delayChoice, int delay[]) { delayChoice.addItem("Choose Delay (ms)"); for( int j = 1; j <= delay.length; j++ ) { delayChoice.addItem("" + delay[j-1]); } } /** * start the applet */ public void start() { if( myThread == null ) { myThread = new Thread(this); myThread.start(); } } /** * stop the applet */ public void stop() { myThread.stop(); } void drawSteps(int steps) { graphics.setColor(Color.black); graphics.drawString(stepsTaken + stepsTakenString, xString, yString); } /** * run the applet */ public void run() { while( true ) { if( desc != null ) { drawGrid(); if( desc.moveTowardSpam() ) { stepsTaken++; } } try { myThread.sleep(currentDelay); } catch( Exception e ) { } } } /** * update is implicitly called by repaint() * It calls paint(Graphics) */ public void update(Graphics g) { paint(g); } /** * paint(Graphics) is is called by update(Graphics) */ public void paint(Graphics g) { g.drawImage(image, 0, 0, null); } void drawGrid() { graphics.setColor(backgroundColor); graphics.fillRect(0, 0, size().width, size().height); graphics.setColor(foregroundColor); grid.draw(graphics, xGridOffset, yGridOffset, gridSize); drawSteps(stepsTaken); repaint(); } void setInput(int index) { grid = new AppletGrid(sample[index]); desc = new DESC(grid, DESC.start0, DESC.start1); } } // DESCapplet