Given by Nancy McCracken (foils by Tom Scavo) at NPAC/ECS Java Academy on February to April 1998 and 1999. Foils prepared 6 Feb 99
Outside Index
Summary of Material
Introduction |
Java Applets |
Understanding Applet Methods |
Elementary Java Graphics |
Java Geometry |
Outside Index Summary of Material
Tom Scavo |
<trscavo@npac.syr.edu> |
NPAC @ SU |
111 College Place |
Syracuse, NY 13244-4100 |
Syracuse University |
College of Engineering and Computer Science |
Northeast Parallel Architectures Center |
present |
Introduction |
Java Applets |
Understanding Applet Methods |
Elementary Java Graphics |
Java Geometry |
A good working knowledge of HTML and the World Wide Web |
A high-end PC running Windows 95/NT |
A reasonably good network or dialup connection |
The following are helpful, but not necessary:
|
Java Academy Home Page http://www.npac.syr.edu/projects/k12javaspring98/ |
NPAC Java Resources http://www.npac.syr.edu/projects/tutorials/Java/ |
The Java Tutorial http://java.sun.com/tutorial/ |
Other web sites of interest:
|
Sun's Java Development Kit (JDK): http://java.sun.com/products/jdk/ |
Netscape Communicator with Java 1.1 AWT: http://developer.netscape.com/software/jdk/download.html |
Microsoft Internet Explorer: http://www.microsoft.com/ie/download/ |
JavaEdit by Dick Chase (freeware): http://www.tiac.net/users/dchase/javaedit.htm |
TextPad by Helios Software (shareware): http://www.textpad.com/ |
Code is written in a fixed-width, bold yellow font |
Within a code segment, an italicized word denotes a name to be chosen by the programmer |
In the text, we often give a method's signature, while omitting the method body. Thus we write: public void paint( Graphics g ); which tells you everything you need to know to use method paint(...), but (intentionally) says nothing about how it is implemented |
A method's signature along with a short description is sometimes referred to as an applications programming interface (API) |
Much of the official Java documentation and textbooks use APIs to communicate the Java language |
We adopt the same conventions here |
The following variable names are used consistently throughout this document: String s; Graphics g; Font f; FontMetrics fm; Color c; Component c; |
In different |
sections! |
There are two types of Java programs: applets and applications |
A Java applet lives on the web and runs inside an ordinary web browser window |
A Java application runs on any machine with a Java interpreter installed |
In this course, we concentrate on applets... |
Our first applet is as simple as can be: // File: HelloWorld.java import java.applet.Applet; import java.awt.Graphics; public class HelloWorld extends Applet { public void paint( Graphics g ) { g.drawString( "Hello World!", 25, 25 ); } } |
public class HelloWorld extends Applet { ... } |
public class HelloWorld extends Applet { public void paint( Graphics g ) { g.drawString( "Hello World!", 25, 25 ); } } |
// File: HelloWorld.java import java.applet.Applet; import java.awt.Graphics; |
The complete Java source file: // File: HelloWorld.java import java.applet.Applet; import java.awt.Graphics; public class HelloWorld extends Applet { public void paint( Graphics g ) { g.drawString( "Hello World!", 25, 25 ); } } |
The next step is to compile the source file: > javac HelloWorld.java |
The Java compiler (javac) compiles the high-level source code into bytecodes |
The file of bytecodes created by the compiler is called "HelloWorld.class" |
Note carefully the ".class" extension |
<!-- File: HelloWorld.html --> <HTML> <HEAD> <TITLE>HelloWorld applet</TITLE> </HEAD> <BODY BGCOLOR="#FFFFFF"> <APPLET CODE="HelloWorld.class" WIDTH=150 HEIGHT=50> </APPLET> </BODY> </HTML> |
The most important part of the HTML file is the <APPLET> tag: <APPLET CODE="HelloWorld.class" WIDTH=150 HEIGHT=50> </APPLET> which loads the bytecodes into the browser |
Note that the "applet window" is specified to be 150 pixels wide and 50 pixels high |
In HTML 4.0, the <APPLET> tag has been deprecated in favor of the more general <OBJECT> tag : <OBJECT CLASSID="HelloWorld.class" CODETYPE="application/java" WIDTH=150 HEIGHT=50> </OBJECT> |
The above <OBJECT> tag is equivalent to the <APPLET> tag on the previous foil |
Once the source file has been compiled and the HTML file has been created, the applet may be viewed: > appletviewer HelloWorld.html |
Alternatively, you may view the applet with a Java-enabled browser such as Netscape Communicator or Microsoft Internet Explorer |
The following methods are invoked automatically by the Java interpreter: public void init(); public void start(); public void stop(); public void destroy(); |
Initially, these methods of the Applet class all have empty bodies, but... |
An applet may override any or all of these methods, like this: import java.applet.Applet; public class AppletDemo extends Applet { public void init() { /* insert code */ } public void start() { /* insert code */ } public void stop() { /* insert code */ } public void destroy() { /* insert code */ } } |
init() and destroy() are called once and only once during the life of the applet |
start() and stop() are called repeatedly |
Other methods are called automatically by the Java interpreter, for instance: public void paint( Graphics g ); |
The paint(...) method is called immediately after start() and any time the applet window must be redrawn |
Like the others, paint(...) can (and should) be overridden by the programmer |
Override the paint(...) method like this: import java.applet.Applet; import java.awt.Graphics; public class AppletDemo extends Applet { public void init() { /* insert code */ } public void start() { /* insert code */ } public void stop() { /* insert code */ } public void destroy() { /* insert code */ } public void paint( Graphics g ) { // insert code here } } |
Like start() and stop(), the paint(...) method will be called by Java many times during the life of an applet |
All of the methods in the previous diagram are called automatically by the Java VM |
What if the programmer wants to repaint the applet explicitly? |
In that case, call the update(...) method: public void update( Graphics g ); |
The update(...) method simply clears the applet window and calls paint(...) |
Note, however, that update(...) takes a Graphics argument, which is inconvenient |
Instead, call the repaint() method: public void repaint(); which in turn calls update(...) |
Note: The repaint() method is almost never overridden by the programmer |
Let's examine update(...) more closely... |
Given a Graphics object g, the following code fragment clears the applet window: int w = getSize().width; int h = getSize().height; g.setColor( getBackground() ); g.fillRect( 0, 0, w, h ); g.setColor( getForeground() ); |
So here is update(...) in its entirety: public void update( Graphics g ) { // clear the applet and call paint: int w = getSize().width; int h = getSize().height; g.setColor( getBackground() ); g.fillRect( 0, 0, w, h ); g.setColor( getForeground() ); paint( g ); } |
Think of a Java applet as a graphics window on which to draw text and other objects |
The graphics window has a particular width and height as specified in the <APPLET> tag |
The origin is in the top left-hand corner of the graphics window: the x-coordinate increases from left to right, while the y-coordinate increases from top to bottom |
In Java, the width and height of the applet are obtained as follows: int width = getSize().width; int height = getSize().height; |
These are the same width and height given in the <APPLET> tag: <APPLET ... WIDTH=... HEIGHT=... > |
Note: All measurements are in pixels |
drawLine(...) |
drawRect(...) |
drawRoundRect(...) |
draw3DRect(...) |
drawOval(...) |
drawArc(...) |
drawString(...) |
fillRect(...) |
fillRoundRect(...) |
fill3DRect(...) |
fillOval(...) |
fillArc(...) |
Important methods from the Graphics class: |
The drawLine(...) method draws a line segment with endpoints (x1,y1) and (x2,y2): public void drawLine( int x1, int y1, int x2, int y2 ); |
The drawRect(...) method draws a rectangle anchored at point (x,y): public void drawRect( int x, int y, int w, int h ); |
The drawRoundRect(...) method draws a rectangle with rounded corners: public void drawRoundRect( int x, int y, int w, int h, int W, int H ); |
The draw3DRect(...) method draws a 3-d rectangle anchored at point (x,y): public void draw3DRect( int x, int y, int w, int h, boolean raised ); |
The drawOval(...) method draws an ellipse anchored at point (x,y): public void drawOval( int x, int y, int w, int h ); |
The drawArc(...) method draws an arc anchored at point (x,y): public void drawArc( int x, int y, int w, int h, int t1, int t2 ); |
Each "draw" method (except drawLine and drawString) has a corresponding "fill" method |
The syntax is the same except that the word "draw" is replaced by "fill" (e.g., fillRect) |
An object is filled with the current color, chosen as follows: g.setColor( c ); where c is a Color object |
The java.awt.Polygon class is used to represent arbitrary polygons: Polygon p = new Polygon(); p.addPoint( 50, 25 ); p.addPoint( 75, 100 ); p.addPoint( 25, 100 ); |
The above triangle is drawn like this: g.drawPolygon( p ); |
Each call to the addPoint(...) method adds a vertex to the polygon |
When the drawPolygon(...) method is called, the last vertex is automatically joined with the first (except in Java 1.0, which has a permanent bug) |
There is a corresponding fillPolygon(...) method, of course |
Our test applet creates two triangles: p1 = new Polygon(); p1.addPoint( 50, 25 ); p1.addPoint( 75, 100 ); p1.addPoint( 25, 100 ); p2 = new Polygon(); p2.addPoint( 125, 25 ); p2.addPoint( 175, 100 ); p2.addPoint( 125, 100 ); |
The test applet draws one triangle and fills the other: public void paint( Graphics g ) { g.drawPolygon( p1 ); g.fillPolygon( p2 ); } |
In the exercises, you are asked to draw an equilateral triangle... |
An equilateral triangle has three sides of equal length |
To draw an equilateral triangle precisely, we must determine its height h |
Our next test applet draws two quadrilaterals, for example: p = new Polygon(); p.addPoint( 25, 25 ); p.addPoint( 125, 25 ); p.addPoint( 125, 75 ); p.addPoint( 25, 75 ); |
There's another way to do it, however... |
Here's another way to instantiate a polygon: int xValues = { 25, 125, 125, 25 }; int yValues = { 25, 25, 75, 75 }; Polygon p = new Polygon( xValues, yValues, 4 ); |
To fill this quadrilateral, type: g.fillPolygon( p ); |
Note: The number of vertices is required in this version of the Polygon constructor |
Our next test applet draws an octagon inside a square |
It does this by trisecting each side of the square |
This octagon is not regular, however... |
A regular octagon has all sides equal |
Solve for y in terms of x: |
x |
x |
y |
y |
x |
Our final test applet draws a hexagon inside a square |
All sides of the hexagon are equal, but this hexagon is not regular! |
We leave it to you to figure this out... |