/*Name: Rosie Moriyama Project: Smiley Face/Sad Face FIle: Smiley.java Purpose: Project #1 for CS110 Description: A simple Applet that illustrates some of the basic drawing and event handling capabilities of Java. The Applet contains two buttons, labeled "Smile" and "sad." Pressing the "Smile" button produces a smiley face, pressing the "Sad" button produces the sad face. */ import java.awt.*; import java.applet.Applet; import java.awt.event.*; public class Smiley extends Applet implements ActionListener { private boolean SMILE = true; private Font f = new Font ("Helvetica", Font.PLAIN, 9); public void init() { //Define the GUI //The applet has a "Smile" button Button smileButton = new Button("Smile"); //create button add(smileButton); //add to applet's GUI smileButton.addActionListener(this); //register event listener // The applet has a "Sad" button Button sadButton = new Button("Sad"); add(sadButton); sadButton.addActionListener(this); //set initial background setBackground (Color.yellow); } // end of init public void actionPerformed(ActionEvent e) { //The event handler //Get the command (which button was pressed?) String cmd = e.getActionCommand(); if (cmd.equals("Smile")) { //"Smile" was pressed SMILE = true; setBackground(Color.yellow); repaint(); } else if (cmd.equals("Sad")) { //"Sad" SMILE = false; setBackground(Color.gray); repaint(); } } //end of actionPerformed public void paint( Graphics g ) { //Draw face g.drawOval(50, 75, 100, 100); //face g.drawLine(100, 110, 100, 130); //nose g.drawLine(70, 100, 90, 100); //left eye g.drawLine(110, 100, 130, 100); //right eye //Draw smiley or sad if (SMILE) { g.drawArc(70, 95, 60, 60, 225, 90); } else { g.drawArc(70, 145, 60, 60, 45, 90); } //SIGNATURE: ROSIE MORIYAMA g.setFont (f); g.drawString("Applet by ROSIE.", 1, 199); } //end of paint } // end of class Smiley