// file: DESCapplet.java // author: Robert Keller // purpose: Applet for DESC simulation /** * DESCapplet.java * @version 1 * @author Robert M. Keller */ import java.applet.*; // applet classes import java.awt.*; // Abstract Window Toolkit classes import java.awt.event.*; /** * Applet for demonstrating breadth-first search */ public class DESCapplet extends BaseApplet { /** * the index of the initial sample grid to be used */ static int initialSampleIndex = 4; /** * suffix of a String for showing the number of steps */ static String stepsString = " steps "; /** * font used for graphics */ Font mainBold = new Font("Helvetica", Font.BOLD, 18); /** * possible delay values in the menu */ static int delayOption[] = {25, 50, 100, 200, 400, 800, 1600}; /** * x offset for the Grid */ int xGridOffset = 70; /** * y offset for the Grid */ int yGridOffset = 70; /** * size of one square of the grid */ int gridSize = 20; /** * x location of String indicating steps */ int xString = 50; /** * y location of String indicating steps */ int yString = 50; /** * default delay in ms */ int defaultDelay = 100; /** * choice menu for samples */ Choice sampleChoice; /** * choice menu for delay values */ Choice delayChoice; /** * array of samples */ Sample sample[]; /** * Grid extended for this applet */ AppletGrid grid; /** * number of steps taken by game */ int stepsTaken; /** * a special parent value indicating "no parent" */ static Pair nilParent = new Pair(-1, -1); /** * the Double-Ended SpamCruncher */ DESC desc = null; /** * Initialize the applet. */ public void init() { super.init(); // Initialize BaseApplet. offScreen.setFont(mainBold); // Set the font used in drawing. // Create Choice menu for selecting example. sampleChoice = new Choice(); sampleChoice.setFont(mainBold); add(sampleChoice); sample = Sample.makeSamples(); setSamples(sampleChoice, sample); sampleChoice.addItemListener(new SampleChoiceListener()); // Set the initial sample. setInput(initialSampleIndex); // Create Choice menu for selecting delay. delayChoice = new Choice(); delayChoice.setFont(mainBold); add(delayChoice); setDelays(delayChoice, delayOption); delayChoice.addItemListener(new DelayChoiceListener()); // Set the initial delay. setDelay(defaultDelay); } /** * An inner class defining a listener for ItemEvents to the sample choice. */ class SampleChoiceListener implements ItemListener { /** * Act on a choice from a menu. */ public void itemStateChanged(ItemEvent event) { // choose the input sample based on its index in the menu int index = sampleChoice.getSelectedIndex(); // 0 is used as an instructional comment, not as a specific item chosen if( index > 0 ) { setInput(index-1); stepsTaken = 0; } } } /** * An inner class defining a listener for ItemEvents to the delay choice. */ class DelayChoiceListener implements ItemListener { public void itemStateChanged(ItemEvent event) { // choose a delay value based on its index in the menu int index = delayChoice.getSelectedIndex(); // 0 is used as an instructional comment, not as a specific item chosen if( index > 0 ) { setDelay(delayOption[index-1]); } } } /** * 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 delayOption[]) { delayChoice.addItem("Choose Delay (ms)"); for( int j = 1; j <= delayOption.length; j++ ) { delayChoice.addItem("" + delayOption[j-1]); } } /** * Set the input sample and create a new Grid. */ void setInput(int index) { grid = new AppletGrid(sample[index]); // Initialize the breadth-first search. desc = new DESC(grid, DESC.start0, DESC.start1); } /** * Take a single step. * This over-rides the corresponding method in BaseApplet. */ public void step() { if( desc.moveTowardSpam() ) { stepsTaken++; } drawEverything(); } /** * Draw the simulation in progress. */ void drawEverything() { clearOffScreenBuffer(); drawGrid(); drawSteps(stepsTaken); repaint(); } /** * Draw the grid and its contents. */ void drawGrid() { offScreen.setColor(foregroundColor); grid.draw(offScreen, xGridOffset, yGridOffset, gridSize); } /** * Draw the String indicating number of steps. */ void drawSteps(int steps) { offScreen.setColor(Color.black); offScreen.drawString(stepsTaken + stepsString, xString, yString); } /** * Add spam when mouse clicked on a blank square. * Remove spam when mouse clicked on spam. * Add blockage when mouse shift-clicked on a blank square. * Remove blockage when mouse shift-clicked on blockage. */ public void mouseClicked(MouseEvent e) { int row = (e.getY() - yGridOffset) / gridSize; int col = (e.getX() - xGridOffset) / gridSize; if( e.isShiftDown() ) { if( grid.isBlank(row, col) ) { grid.set(row, col, Grid.BLOCKAGE); } else if( grid.isBlocked(row, col) ) { grid.set(row, col, Grid.BLANK); } } else if( grid.isBlank(row, col) ) { grid.set(row, col, Grid.SPAM); } else if( grid.hasSpam(row, col) ) { grid.set(row, col, Grid.BLANK); } } } // DESCapplet