CS111 Lab 11 Exercises

Task 1: Applet from scratch

This is a step-by-step follow along exercise for how to create an applet from scratch and how to use Symantec Cafe to set up projects. To start, download the lab11_programs folder from the CS111 download folder on nike. We will be working with the MyApplet folder which contains one HTML file.
import java.applet.*; // use applet
import java.awt.*; // use graphics

public class MyApplet extends Applet {
  
  public void paint (Graphics g) {
    g.drawString("Smile!", 30, 30);
  }
  
}

Task 2: Exploring Fonts and Colors

Now we have a working applet, we can practice drawing shapes in it. We can also write text by using the drawString method as illustrated above. To write in different colors, we must set the color of the graphics context before writing each piece of text. We saw the Color contract earlier when working with BuggleWorld. We used Java's Color class then so the same colors are available to us now. The Color constants are listed below for your convenience. We can also create other colors by using one of the Color constructors. See the Color contract for details.
Color.black        Color.green       Color.red
Color.blue         Color.lightGray   Color.white
Color.cyan         Color.magenta     Color.yellow
Color.darkGray     Color.orange
Color.gray         Color.pink
Similarly, we can change the current Font of the graphics context by using the setFont(Font f) method. A Font object is created with three parameters specifying the font family, style, and size. Some examples of how to specify and set fonts follow (assume g is a Graphics context):
Font f1 = new Font("Helvetica", Font.PLAIN, 18);
g.setFont(f1);
Font f2 = new Font("TimesRoman", Font.BOLD, 48);
g.setFont(f2);
g.setFont(new Font("Courier", Font.BOLD+Font.Italic, 36));

The FontMetrics class is used to measure the size of the text. There are lots of details involved and you can read the FontMetrics contract to learn more about it. For now, we'll tell you how to figure out the width of a String (the most useful feature).

Knowing the width of Strings makes it possible to write Strings in multiple colors or fonts next to each other precisely. We can practice using Fonts and Colors by adding to the paint() method in the applet from Task 1.

Task 3: Bouncing Balls

In this problem we will create a Sprite which represents a bouncing ball that bounces around the animation frame but stays within it. Step by step directions are given below. To start this problem, open up the project inside the AnimationLab folder which is in the lab11_programs folder available on nike. This folder contains the minimum files necessary to run AnimationWorld. If you want examples, download the AnimationLecture folder or read the documentation. The Test folder includes a working example of Task 3 and 4.

Task 4: Bouncing Balls which change color.

Extend your BouncingBall class from task 2 to change colors periodically.
public static Color getRandomColor () {
  int randomIndex = (int)(Math.random()*colors.length);
  return colors[randomIndex];
}