Creating a different Applet!

This document was written by CS 290W TA Joshua Kay and was last modified

Open a text editor. Note that when you type a Java program, you must save it without any kind of formatting like that which occurs when you save a word document, etc. If this formatting is present it will not compile.

What do we want to do? How about an Applet that prints "CS290w" really big, bold and red? Yes, that sounds easy, let's do that.

First we have to decide how we want to display the Applet. Label seems to be the best choice so we will use that. Where is Label defined, though? Well, from Applet #3 in the examples, we know that Label is stored in the java.awt package, so we know we will need to import that. We are making an Applet, so we need to import part of the java.applet package.

How do we go about setting Font Sizes, and colors though? Well, let's look...

For making a font bigger...

Font bigger=new Font("Helvetica",Font.BOLD,30); This takes three arguments. First is a String, representing the Font type - in this case, "Helvetica". The next represent the style and size. Font.BOLD means we want a bold type font. Finally, 30 says let's make it size 30 point.

So, what if we wanted a Helvetica Font which was italic and size 44 point?

Font italicHelvetica44=new Font("Helvetica",Font.ITALIC,44);

So what about Color? Well, the Label() class has methods for that. setForeground(Color c), and setBackground(Color c)

Simple enough, right?

So let's start writing!

//My First Applet import java.applet.Applet; import java.awt.*; public class MyApplet extends Applet { Label myLabel=new Label("My First Applet"); Font bigger=new Font("Helvetica",Font.BOLD,30); public void init() { myLabel.setForeground(Color.red); myLabel.setBackground(Color.white); myLabel.setFont(bigger); add(myLabel); } } This should all be somewhat understandable. Believe it or not, we have already hurdled a lot of the major obstacles in learning Java. In the setForeground(Color c) class, you'll notice that instead of Color c, the argument sent is Color.red. Well, Color is a predefined class in the java.awt package, or more specifically Java.awt.Color. it has a few different predefined colors, and has them represented as static values in the Color class. By using red and white, we managed to use some of the colors that are predefined for us.

Let's take a look at our Applet!

you! you need java!

Here is some additional helpful information about some of the methods used above:

When creating a new Font, the Font type can be any font type that you might use in a standard HTML <FONT FACE="font type"> tag. Popular choices include Helvetica, TimesRoman, Courier, and Dialog. Your choices for Font style are Font.PLAIN, Font.BOLD, and Font.ITALIC.

The setForeground and setBackground methods if used without an object name set the foreground and background colors for the applet. When used with an object name (as in myLabel.setBackground) they set the foreground and background colors for the object. Your choices for colors include Color.white, Color.gray, Color.black, Color.red, Color.pink, Color.orange, Color.yellow, Color.green, Color.magenta, Color.cyan, Color.blue.