CS 110 Introduction to Computing
Fall 2000
Homework #2: Smiley/Sad Applet
Due in class on Tuesday, February 5, 2002
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, 2 and 3 from the lab manual (Getting
Wired With java). Do this while sitting in front of the computer
carrying out all of the steps (especially the ones in Chapters
2 and 3).
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:
- In CodeWarrior, use the Java 1.2/Applet stationery.
- You will not be able to view your applets in Netscape on Macs due to a shortcoming
in Netscape's Java compatibity. However, all current versions of Internet
Explorer (on Macs and PCs), and netscape (on PCs) will work just fine. For
all your CodeWarrior work continue to use the Applet Viewer.
- When you look at the program below, do not worry about the details of all
the instructions. Remember, this project is just so you can get fully acquainted
with the lab environment. We will be spending the rest of the semester learning
Java.
- Play close attention to the syntax. Every Java instruction has to be written
with proper punctuations as well as with consistent use of upper/lower case
character combinations. When you type the program below, enter it exactly
as it is listed.
- Make sure you enter your name in the NAME slot
at the top of the program. Also, modify the SIGNATURE
slot in the program (where it has Deepak's name),
- Try and do this exercise during one of the TA sessions so there will be
some help available in case there are any difficulties.
- Once you are done with the exercise, hand in a printed (stapled) report
that contains the following:
- A printout of your Java program (this will be the contents of the file
Smiley.java)
- A screen snapshot of your applet. To do this, press the keys APPLE+SHIFT+CAPS
LOCK+4 all at once (on Macs). The mouse cursor will change to a round
bullet. Move it into the applet window and press the mouse button. You
will hear a click (the snapshot was taken). The snapshot gets stored in
a file called Picture1 (or Picture 2, etc.) on the main disk of the computer.
You can then paste it into any document or print it out. On PCs press
ALT+PRINT SCREEN. The snapshot captues the active window into the clipboard.
You can then paste it into a document in MS Word and print it.
- A printout of your project window. To do this, simply click the mouse
in the project window and then chose print from the File menu.
- A short essay on your experience in doing this assignment. We want to
know how it all worked out for you and how you feel about completing this
assignment.
- Create a web page containing this applet. You may want to post your essay
(see above) in this page as well. Upload this page to your web site and link
it so it is accessible from your home page (or your CS110 page).
/*
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