This project is designed to introduce you to the programming process: the laboratory/computer environment, and the Metrowerks CodeWarrior IDE for developing Java Applets.
First, read Chapters 1 and 2 from the lab manual (Getting Wired With java). Do this while sitting in front of the computer carrying out all of the steps.
After reading and working through the chapters in the lab manual, you will implement the following applet (Note: you will not be able to view the applet using Netscape on a MAC. See (2) below):
Notes:
/* NAME : <your name here> 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 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 Deepak.", 1, 199); } // end of paint } // end of class Smiley
Back to: CS110 Course Materials