CSCI A201/A597

Lecture Notes Eight

Second Summer 2000


Exercises with classes and objects. Introduction to applets.

Bring your take home quizzes with you today, as I would like to offer some help and answer some questions (maybe even ask a few) about the problems on the quiz. You will only have one lab, tonight, and then next Monday the quiz is due. Then Tuesday your program 3 is due, and assignment 4 is in the wind, so a bit of help would be OK right now, I think.

Seeing that these notes have no dialogue at all one might expect them to change soon, into the format you should be used with already. Meanwhile I wanted to put together some information that would be of help should we finish the questions quickly in class, and should you need to have something to guide you in lecture today as we move to applets.

Notes for the applets sections on pages 139-161.

We, of course, will tell you all about the history of it. Some of the notes from the first set of lecture notes will be relevant, from a historical point of view. But beyond that we want to make sure that if you want to experience this first hand in a lab, you should be able to rely on a clear, pithy set of instructions, and hopefully this document will turn into that.

1. A Simple Applet

Create a file One.html with the following contents.

<HTML>
<HEAD>
<TITLE>Applet One</TITLE>
</HEAD>
<BODY>
<P>Here is my <U>first applet</U>.</P> 

<APPLET CODE="One.class" WIDTH=300 HEIGHT=300>

</APPLET> 

<P> Its title is <I>"Picasso in disbelief"</I>.</P> 


</BODY>
</HTML>
Create a file One.java with the following contents.
import java.applet.Applet;
import java.awt.*;

public class One extends Applet {
    public void paint (Graphics g) {
	Graphics2D g2 = (Graphics2D)g; 
	Rectangle a = new Rectangle(5, 10, 20, 30); 
	g2.draw(a); 
	a.translate(15, 25); 
	g2.draw(a); 
    }
}
Compile One.java and look at One.html with either Netscape or an appletviewer.

2. Graphical Shapes

Create a file Two.html with the following contents.

<HTML>
<HEAD>
<TITLE>Applet Two</TITLE>
</HEAD>
<BODY>
<P>Here is my <U>second applet</U>.</P> 

<APPLET CODE="Two.class" WIDTH=300 HEIGHT=300>

</APPLET> 

<P> Its title is <I>"Graphical Shapes"</I>.</P> 


</BODY>
</HTML>
Create a file Two.java with the following contents.
import java.applet.*;
import java.awt.*;
import java.awt.geom.*;

public class Two extends Applet {
    public void paint (Graphics g) {
	Graphics2D g2 = (Graphics2D)g; 

	Ellipse2D.Double m = new Ellipse2D.Double(5, 10, 150, 80); 
	g2.draw(m); // an ellipse 

        int x = 90; 
	int y = 90;
	int diam = 70; 
	Ellipse2D.Double q = new Ellipse2D.Double(x, y, diam, diam); 
	g2.draw(q); // a circle 

        Line2D.Double segment = new Line2D.Double(15, 150, 55, 90); 
	g2.draw(segment); // a line

        Point2D.Double start = new Point2D.Double(20, 155); 
        Point2D.Double stop = new Point2D.Double(60, 95); 
        segment = new Line2D.Double(start, stop); 
        g2.draw(segment); // another line

    }
}
Compile Two.java and look at Two.html with either Netscape or an appletviewer.

3. Color and Shapes

Create a file Three.html with the following contents.

<HTML>
<HEAD>
<TITLE>Applet Three</TITLE>
</HEAD>
<BODY>
<P>Here is my <U>third applet</U>.</P> 

<APPLET CODE="Three.class" WIDTH=300 HEIGHT=300>

</APPLET> 

<P> Its title is <I>"Colored Graphical Shapes"</I>.</P> 


</BODY>
</HTML>
Create a file Three.java with the following contents.
import java.applet.*;
import java.awt.*;
import java.awt.geom.*;

public class Three extends Applet {
    public void paint (Graphics g) {
	Graphics2D g2 = (Graphics2D)g; 

	Color magenta = new Color(1.0F, 0.0F, 1.0F); 
	g2.setColor(magenta); 
	Ellipse2D.Double m = new Ellipse2D.Double(5, 10, 150, 80); 
	g2.draw(m); // an ellipse 
	g2.fill(m); // fill it with the current color
	// magenta is still current color here 
        int x = 90; 
	int y = 90;
	int diam = 70; 
	g2.setColor(Color.red); // predefined color 
	Ellipse2D.Double q = new Ellipse2D.Double(x, y, diam, diam); 
	g2.draw(q); // a circle 
	g2.fill(q); // fill it with the current color
	g2.setColor(Color.blue); // another predefined color 
        Line2D.Double segment = new Line2D.Double(15, 150, 55, 90); 
	g2.draw(segment); // a line

        Point2D.Double start = new Point2D.Double(20, 155); 
        Point2D.Double stop = new Point2D.Double(60, 95); 
        segment = new Line2D.Double(start, stop); 
        g2.draw(segment); // another line
        // current color is blue here 
    }
}
Compile Three.java and look at Three.html with either Netscape or an appletviewer.

4. Text in Applets

Create a file Four.html with the following contents.

<HTML>
<HEAD>
<TITLE>Applet Four</TITLE>
</HEAD>
<BODY>
<P>Here is my <U>fourth applet</U>.</P> 

<APPLET CODE="Four.class" WIDTH=300 HEIGHT=300>

</APPLET> 

<P> Its title is <I>"Applet Applet"</I>.</P> 


</BODY>
</HTML>
Create a file Four.java with the following contents.
import java.applet.Applet;
import java.awt.*;

public class Four extends Applet {
    public void paint (Graphics g) {
	Graphics2D g2 = (Graphics2D)g; 

	g2.drawString("Applet", 50, 160); 

	final int HUGE_SIZE = 36; // fotn point size 
	String message = "Applet"; // actual text 
        // create Font object then call setFont on it 
	Font hugeFont = new Font("Serif",    // font face name 
				 Font.BOLD,  // style 
				 HUGE_SIZE); // point size   
        g2.setFont(hugeFont); // now g2 will write that way 
	g2.setColor(Color.green); // set color to predefined green 
	g2.drawString(message, 50, 100); // then write string 
    }
}
Compile Four.java and look at Four.html with either Netscape or an appletviewer.

5. Simple Drawing

Create a file Five.html with the following contents.

<HTML>
<HEAD>
<TITLE>Applet Five</TITLE>
</HEAD>
<BODY>
<P>Here is my <U>fourth applet</U>.</P> 

<APPLET CODE="Five.class" WIDTH=300 HEIGHT=300>

</APPLET> 

<P> Its title is <I>"Happy Hour"</I>.</P> 

</BODY>
</HTML>
Create a file Five.java with the following contents.
import java.applet.*;
import java.awt.*;
import java.awt.geom.*;

public class Five extends Applet {
    public void paint (Graphics g) {
	Graphics2D g2 = (Graphics2D)g; 

	Ellipse2D.Double e1 = new Ellipse2D.Double( 75, 40, 30, 70); 
	Ellipse2D.Double e2 = new Ellipse2D.Double(115, 40, 30, 70); 
	Ellipse2D.Double c1 = new Ellipse2D.Double( 85, 85, 15, 15); 
	Ellipse2D.Double c2 = new Ellipse2D.Double(125, 85, 15, 15); 
	Ellipse2D.Double n = new Ellipse2D.Double(55, 120, 110, 25); 

	Arc2D.Double m = 
           new Arc2D.Double(-40, -120, 300, 300, 230, 80, Arc2D.OPEN); 

	g2.draw(e1); 
	g2.draw(e2); 
	g2.fill(c1);
 	g2.fill(c2); 

	g2.draw(n); 

	g2.draw(m); 

    }
}
Compile Five.java and look at Five.html with either Netscape or an appletviewer.


Last updated: June 29, 2000 by Adrian German for A201