/* Nicole Zachary PROJECT: Smiley Face/Sad Face FILE: SmileySad.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 a 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.lightGray); 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: Write signature g.setFont(f); g.drawString("Applet by Nicole.", 1, 199); } // end of paint } //end of class Smiley