| CSCI A201/A597Lecture Notes Eight Second Summer 2000 |
| There are three kinds of Java programs that you will learn to write. | We already know how to write console applications. |
| Console applications run in a single, usually rather plain-looking terminal window, |
... read input from the keyboard, using
ConsoleReader-like objects, ...
|
| ... and display text output in the same window. | There are also graphical applications and applets. |
| Graphical applications use one or more windows, usually filled with user interface components... | ... such as buttons, text input fields, menus. |
| Graphical programs can display both text and graphical shapes and images. | Most of the programs you are familiar with, such as word processors, and web browsers, are graphical programs. |
| Applets are similar to graphical applications, but they run inside a web browser. | In this chapter you will learn how to write simple applets that display graphical shapes. |
| Applets are programs that run inside a web browser. | The code for the applet is on a web server, and has been compiled to bytecode. |
| The bytecode is downloaded into the browser... | ... whenever you access a web page that contains an applet. |
| Applets are embedded inside web pages. | A web pages is written in a language called HTML. |
| HTML files are made up of text and tags, that tell the browser how to render the text. | Nowadays, there are dozens of HTML tags. |
| The APPLET tag is used to include an applet into a web page. | To display the applet, you need first to write and compile a Java file, to generate the applet code. |
| Then you tell the browser how to find the code for the applet and how much screen space to reserve for the applet. | Here's an example: |
<APPLET CODE="One.class" WIDTH=400 HEIGHT=300> </APPLET>
| In our first applet we will simply draw a couple of rectangles. | You will soon see how to produce more interesting drawings. |
| The purpose of this applet is to show you the basic outline of an applet that creates a drawing. | This applet will be implemented in a single class. |
The name of the class will be One.
| We will have five examples today. |
| To run this applet you will need an HTML file with an APPLET tag. |
Here's the simplest possible HTML file to display the applet.
(Call it One.html).
|
<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>
| To run the applet you have two choices. | You can use the appletviewer, a program that comes with the JDK from Sun. |
| To use the appletviewer you simply start it from the command line, | ... giving it the name of the HTML file that contains your applet: |
appletviewer One.html
| You can also show the applet inside any Java-2 enabled browser. | An applet is programmed as a class, like any other program in Java. |
However, the class is declared public and it extends Applet.
|
That means that our rectangle applet inherits the behaviour of the Applet class.
|
| We will discuss inheritance in a few more chapters. |
Unlike applications, applets don't have a main method.
|
| The web browser (or appletviewer) is responsible for starting up the Java virtual machine, for loading the applet code, and for starting the applet. |
Our applets (in this lecture) will implement just one method: paint.
|
| There are other methods you may implement; we will discuss them next time. |
The window manager calls the paint method whenever the surface of the applet
needs to be filled in.
|
| Of course, when the applet is shown for the first time, its contents need to be painted. | If the user visits another web page and then goes back to the web page containing the applet, |
| ... the surface must be painted again,... |
... and the window manager calls the paint method once more on the occasion.
|
Thus, you must put all drawing instructions inside the paint method,
|
... and you must be aware that the window manager can call the paint method many times.
|
The paint receives an object of type Graphics.
| Actually let's see our first applet. |
import java.applet.Applet;
import java.awt.*;
public class One extends Applet {
public void paint (Graphics g) {
Graphics2D g2d = (Graphics2D)g;
Rectangle a = new Rectangle(5, 10, 20, 30);
g2d.draw(a);
a.translate(15, 25);
g2d.draw(a);
}
}
As you can see, the paint receives an object of type Graphics.
|
The Graphics object stores the graphics state.
|
| .. the current color, font, and so on, ... | ... that are used for the drawing operations. |
We will always convert the Graphics object to
an object of type Graphics2D.
| To understand why you need to know a little about the history of these classes. |
The Graphics class was included with the
first version of Java.
| It is suitable for very basic drawings, but it does not use an object-oriented approach. |
After some time programmers clamored for a more powerful graphics package,
and the designers of Java created the Graphics2D class.
|
They did not want to inconvenience those programmers who had produced programs
that used simple graphics, so they did not change the paint method./
|
It still expects to receive a Graphics object when called.
|
Instead they made the Graphics2D class extend the
Graphics class, a process that uses the Java class extension
mechanism,
|
| ... also loosely known as inheritance. |
Whenever the window manager calls the paint method,
it actually passes a parameter of type Graphics2D.
|
| Because of polymorphism that's OK. | Polymorphism is related to inheritance and not as difficult to understand as it sounds. |
Programs with simple graphics need not know about this and they can still
work with a Graphics context in their painting,
|
... but if you want to use the more sophisticated 2D graphics methods, you
recover the Graphics2D reference by using a cast.
|
| Pointing out that when using classes from the Java API we need to look up their packages, and to import them, | .. we're now done describing our first new applet, and ready to move on. The next four examples will be variations on the same theme. |
| What is an ellipse? | Let me draw what I mean by one. |
| Well, that's how you specify, but that's not a definition. | Yes, but the definition is built in (in Java), so I need not worry about it. |
| OK. Let's write an applet that draws an ellipse, a circle, and two lines. | A circle is just a particular case of an ellipse. Here's my program: |
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
}
}
The class name Ellipse2D.Double looks different from the
class names that you have seen so far.
|
This indicates that Ellipse2D.Double is a so-called inner class
inside Ellipse2D.
|
| For us that doesn't mean anything just yet. | When constructing an ellipse just think the class has a long name and give the constructor the right number of arguments. |