// The ehtBall is a magic eight ball emulator that gives answers in // Canadian slang. // Written By: Kevin VanSchepen // Fall Semester, Calvin College // For: CPSC 280 - Programming Languages // Requirements: the 11 gif images that are displayed are located in the same // directory as the class file. // width and height have to be set greater than the image sizes // no additional parameters. // Options: you may change the images to give different responses // as long as they are named the same import java.awt.*; import java.applet.*; import java.util.*; import java.io.*; import java.lang.Math; public class ehtBall extends Applet { protected Button answerButton = new Button("GO"); // Button to get answer protected Button resetButton = new Button("Reset"); // Button to reset protected String imageName = "8ball2.gif"; // filename of the image protected Image eightBall; Graphics g; // init(): Initializer function that runs automatically // sets: background, the 8 ball image, and the buttons public void init() { setBackground(Color.white); // sets the background eightBall = getImage(getDocumentBase(), imageName); // defines the image answerButton.setForeground(Color.white); // button initialization answerButton.setBackground(Color.black); this.add(answerButton); resetButton.setForeground(Color.white); resetButton.setBackground(Color.black); this.add(resetButton); g = this.getGraphics(); // defines g } // action: functions that handles the event of a button click public boolean action(Event e, Object arg) { // what to do when a button is pushed if (e.target == answerButton) { // if it is the "GO" button imageName = new String("8ballani.gif"); // animate repaint(); // repaint the screen getAnswer(); // get an answer repaint(); // repaint the screen return true; } else if (e.target == resetButton) { // if it is the "RESET" button imageName = new String("8ball2.gif"); // reset to the initial image repaint(); // repaint the screen return true; } else return false; } // getAnswer(): generates a random number and then sets the // image according to that number - which gives the answer // to a question public void getAnswer() { int theInt = (int)(Math.random()*10); // gets a random int from 0 to 9 switch(theInt) // sets the images depending on theInt { case 0: { imageName = new String("8ball-0.gif"); break; } case 1: { imageName = new String("8ball-1.gif"); break; } case 2: { imageName = new String("8ball-2.gif"); break; } case 3: { imageName = new String("8ball-3.gif"); break; } case 4: { imageName = new String("8ball-4.gif"); break; } case 5: { imageName = new String("8ball-5.gif"); break; } case 6: { imageName = new String("8ball-6.gif"); break; } case 7: { imageName = new String("8ball-7.gif"); break; } case 8: { imageName = new String("8ball-8.gif"); break; } case 9: { imageName = new String("8ball-9.gif"); break; } default: { imageName = new String("8ball2.gif"); break; } } } // paint: a function that is called automatically at init // paints the image on the screen public void paint(Graphics g) { eightBall = getImage(getDocumentBase(), imageName); // sets the image g.drawImage(eightBall, 19, 40, this); // places the image } }