Full HTML for

Basic foilset Introductory Lectures on Java -- An Overview Mixing Code and Comments

Given by Geoffrey C. Fox at Trip to China and Icase Tutorial on July 12-28 and June 10-13 96. Foils prepared July 10 1996
Outside Index Summary of Material


Java is an object-oriented language based on C++ suitable for general distributed applications programming. In this course, we will concentrate on Java applets to program application interfaces on the World Wide Web.
These lecture slides on programming in Java will show a series of small programming examples, designed to illustrate the main features of the language.
They accompany the Java Course Module, by Geoffrey Fox, which covers more details about the language.
Some examples were constructed from "Teach yourself Java in 21 days", by Laura Lemay and Charles L. Perkins, February 1996, Sams.net Publishing, and from "core Java", by Gary Cornell and Cay Horstmann, the SunSoft Press Java Series, 1996.

Table of Contents for full HTML of Introductory Lectures on Java -- An Overview Mixing Code and Comments

Denote Foils where Image Critical
Denote Foils where HTML is sufficient

1 Introduction to Java Programming:
a Stroll Through Java

2 A Stroll through Java
3 Applet basics and Graphics window examples
4 The Simplest Java Application: Hello, World!
5 The Simplest Java Applet: Hello, World!
6 Displaying your applet from a Web page.
7 The Graphics class
8 Using Graphics properties
9 Drawing simple shapes
10 Passing parameters to an applet: the HTML
11 Passing parameters to an applet: the applet
12 Centering a string in the applet window.
13 Applet Flow of Control
14 Random color boxes example
15 Arrays of random colors
16 Object-Oriented Programming Examples: Defining parent and child classes, overloading constructors, class hierarchy
17 The Class Hierarchy of this example
18 Object-Oriented Programming in JAVA - defining a class
19 More methods in the class definition
20 Defining a child class
21 Using parent and child classes
22 Parent and child example, continued
23 Overloading Constructors
24 More child classes
25 Basic Thread Example
26 Introduction to Threads
27 Example showing the standard thread methods
28 Example showing thread methods, continued
29 More Basic Applet Examples:
Graphics Images and Double Buffering

30 An Image Drawing Example
31 Double Buffering
32 Event Handling
33 Using Mouse Events for User Interaction
34 Mouse Events: Initializing the movable objects
35 Draw the objects with double buffering
36 Mouse Move Event
37 The Set of Mouse Dragging Events
38 More Methods in the Movable Point class
39 Keyboard Events
40 Abstract Windowing Toolkit: Layouts and Components
41 AWT - Grid Layout with Two Components
42 AWT - Adding Components to a Layout
43 AWT - Canvas Component
44 AWT - Handling Actions from Components
45 AWT - Panel Component with Buttons
46 I/O and Networking
47 I/O: Reading a File known by URL
48 I/O: Setting up the URL, Layout and Thread
49 I/O: Reading the Text File

Outside Index Summary of Material



HTML version of Basic Foils prepared July 10 1996

Foil 1 Introduction to Java Programming:
a Stroll Through Java

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
Nancy McCracken
NPAC
Syracuse University
111 College Place
Syracuse NY 13244-4100
May 1996
Click here for body text

HTML version of Basic Foils prepared July 10 1996

Foil 2 A Stroll through Java

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
Java is an object-oriented language based on C++ suitable for general distributed applications programming. In this course, we will concentrate on Java applets to program application interfaces on the World Wide Web.
These lecture slides on programming in Java will show a series of small programming examples, designed to illustrate the main features of the language.
They accompany the Java Course Module, by Geoffrey Fox, which covers more details about the language.
Some examples were constructed from "Teach yourself Java in 21 days", by Laura Lemay and Charles L. Perkins, February 1996, Sams.net Publishing, and from "core Java", by Gary Cornell and Cay Horstmann, the SunSoft Press Java Series, 1996.

HTML version of Basic Foils prepared July 10 1996

Foil 3 Applet basics and Graphics window examples

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
Examples also demonstrate Java language basics, arrays, and the math package. They illustrate the following sections from JavaTutorial:
  • Program Structure
  • Java in Practice
  • Java Language Basics
  • Applets and Graphics

HTML version of Basic Foils prepared July 10 1996

Foil 4 The Simplest Java Application: Hello, World!

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
Since Java is object-oriented, programs are organized into modules called classes, which may have data in variables and functions called methods.

HTML version of Basic Foils prepared July 10 1996

Foil 5 The Simplest Java Applet: Hello, World!

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
Java applets are part of the class hierarchy that can call methods to display on a screen (within the browser window). This example defines the public method paint in this class and calls a method drawString defined in the class Graphics.

HTML version of Basic Foils prepared July 10 1996

Foil 6 Displaying your applet from a Web page.

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
You should name the file with your applet name, HelloWorldApplet.java, run the compiler (javac), getting a bytecode file HelloWorldApplet.class, which you put in a web directory.

HTML version of Basic Foils prepared July 10 1996

Foil 7 The Graphics class

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
An object of the graphics class represent a rectangular drawing grid, with a coordinate system in pixels.
When you draw objects, there is a current "state" consisting of a font and a color.

HTML version of Basic Foils prepared July 10 1996

Foil 8 Using Graphics properties

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
Fonts and colors are objects (sometimes called instances) of the font and color class in the awt package.

HTML version of Basic Foils prepared July 10 1996

Foil 9 Drawing simple shapes

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
Methods are available for drawing lines, rectangles, rounded rectangles, ovals, arcs, and polygons. This example draws a rectangle filled with green with a black border, and a circle filled with magenta.

HTML version of Basic Foils prepared July 10 1996

Foil 10 Passing parameters to an applet: the HTML

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
Within the applet tags, any number of param tags may occur. Attributes can control the alignment of the applet window on the html page.

HTML version of Basic Foils prepared July 10 1996

Foil 11 Passing parameters to an applet: the applet

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
A standard method, init(), is executed when your applet is loaded; it can call the method getParameter.

HTML version of Basic Foils prepared July 10 1996

Foil 12 Centering a string in the applet window.

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
The method applet.size() returns the width and height as a dimension object, which has width and height variables. FontMetrics has variables for various attributes of the font, including width and height.

HTML version of Basic Foils prepared July 10 1996

Foil 13 Applet Flow of Control

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
Each applet's flow of control goes through a sequence of standard methods:
  • public void init() { . . . }
  • executed once when applet is downloaded to the browser.
  • public void start(){ . . . }
  • executed each time the browser window is entered.
  • public void paint(){ . . . }
  • executed whenever you want to draw in the window.
  • public void stop(){ . . . }
  • executed each time the browser window in exited.
  • public void destroy(){ . . . }
  • executed just before the applet exits.
See ColorBoxes examples with init and start.

HTML version of Basic Foils prepared July 10 1996

Foil 14 Random color boxes example

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
This example fills the applet's drawing area with square boxes, each of which has a randomly chosen color.

HTML version of Basic Foils prepared July 10 1996

Foil 15 Arrays of random colors

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
This demonstrates 2-dimensional arrays by first filling arrays with random colors, and then drawing, uses a fixed size drawing window for simplicity.

HTML version of Basic Foils prepared July 10 1996

Foil 16 Object-Oriented Programming Examples: Defining parent and child classes, overloading constructors, class hierarchy

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
Illustrates Object-Oriented Programming section of the JavaTutorial

HTML version of Basic Foils prepared July 10 1996

Foil 17 The Class Hierarchy of this example

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index

HTML version of Basic Foils prepared July 10 1996

Foil 18 Object-Oriented Programming in JAVA - defining a class

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
We define a parent class for movable point objects. Each instance of this class is represented by an x,y location, by a dx,dy offset for the object to move, and a color for the object.

HTML version of Basic Foils prepared July 10 1996

Foil 19 More methods in the class definition

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
We include methods to change the default values for the offsets and color, a method to move the object, and one to paint a point.

HTML version of Basic Foils prepared July 10 1996

Foil 20 Defining a child class

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
We define a class for movable rectangle objects. It uses x,y from the parent class mPoint for the location of the rectangle, but adds h,w to specify height and width of the rectangle.

HTML version of Basic Foils prepared July 10 1996

Foil 21 Using parent and child classes

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
This applet creates 2 mRectangles and loops to move them.

HTML version of Basic Foils prepared July 10 1996

Foil 22 Parent and child example, continued

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
The repaint method calls update, which is overridden here to move the objects, and paint, which is overridden here to redraw whole graphics area.

HTML version of Basic Foils prepared July 10 1996

Foil 23 Overloading Constructors

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
You are allowed to have more than one constructor in a class, if the constructors have different types or numbers of arguments. For example, for people who are using the java.awt.Point class, you can add a constructor to mRectangle which creates a rectangle by giving the top left and bottom right corner points.

HTML version of Basic Foils prepared July 10 1996

Foil 24 More child classes

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
Once we have structured the concepts of our data into classes, it is easy to add new shapes of movable objects as child classes of mRectangle. These classes only have to redefine paint to draw a differently shaped object.

HTML version of Basic Foils prepared July 10 1996

Foil 25 Basic Thread Example

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
Illustrating Threads section of the JavaTutorial

HTML version of Basic Foils prepared July 10 1996

Foil 26 Introduction to Threads

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
A thread is a single sequential flow of control within a process.
If a process has more than one thread, then each thread executes concurrently.
Any Java applet which has extensive execution or loops to repaint the window must run as a concurrent thread with the browser window.
To make an applet with a thread, which is almost always recommended:
  • Change your applet definition to add "implements Runnable", the interface for threads.
  • Include an instance variable for the thread of your applet.
  • Have a start() method which creates a thread and starts it running and a stop() method which stops it running.
  • Have a run() method containing the body of your applet code.

HTML version of Basic Foils prepared July 10 1996

Foil 27 Example showing the standard thread methods

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
These start and stop methods can always be used

HTML version of Basic Foils prepared July 10 1996

Foil 28 Example showing thread methods, continued

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
The body of the applet is in the run method, in this case a loop to keep showing the date.

HTML version of Basic Foils prepared July 10 1996

Foil 29 More Basic Applet Examples:
Graphics Images and Double Buffering

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
Illustrating section on "Back to Details on Graphics and Animation" from the JavaTutorial

HTML version of Basic Foils prepared July 10 1996

Foil 30 An Image Drawing Example

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
This example shows how to get the actual height and width of the image to use in scaling the image under java program control.

HTML version of Basic Foils prepared July 10 1996

Foil 31 Double Buffering

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
This technique is most effective in eliminating flicker, but does take longer and use more memory.

HTML version of Basic Foils prepared July 10 1996

Foil 32 Event Handling

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
Illustrating Mouse and Keyboard events, in the AWT section of the JavaTutorial

HTML version of Basic Foils prepared July 10 1996

Foil 33 Using Mouse Events for User Interaction

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
We set up a test program that creates 3 movable objects, a rectangle, circle and triangle, as in the earlier example. In this program, we start with them all red. Whenever the mouse is detected to be over one of the objects, its color is changed to cyan. If the mouse button is used to drag the object, we move the object to the mouse location.
Note that it is not necessary to introduce a thread for this applet since it is not running continuously - it is mostly waiting for mouse events.

HTML version of Basic Foils prepared July 10 1996

Foil 34 Mouse Events: Initializing the movable objects

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index

HTML version of Basic Foils prepared July 10 1996

Foil 35 Draw the objects with double buffering

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
In this example, update does all the drawing. It also sets a background color rectangle with a 1 pixel border in the foreground color.

HTML version of Basic Foils prepared July 10 1996

Foil 36 Mouse Move Event

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
Whenever the user moves the mouse, a message is sent from the client workstation to the Java system. It generates a form of interrupt called an event. Your Java applet can choose to provide a method which does appropriate response for any event. This is called an event handler - you must return true to show the general event handler that you have intervened.

HTML version of Basic Foils prepared July 10 1996

Foil 37 The Set of Mouse Dragging Events

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
These handle the sequence of: the user clicks the mouse (mouse down), moves the mouse (mouse drag) and releases the mouse button (mouse up).

HTML version of Basic Foils prepared July 10 1996

Foil 38 More Methods in the Movable Point class

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
We add methods to detect when the mouse is inside an object, and a more general method moveTo for large moves under dragging.

HTML version of Basic Foils prepared July 10 1996

Foil 39 Keyboard Events

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
You may also choose to respond to individual keys being pressed from the keyboard. (Later we will learn of a more general way to handle text input.)

HTML version of Basic Foils prepared July 10 1996

Foil 40 Abstract Windowing Toolkit: Layouts and Components

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
illustrating the AWT section of the JavaTutorial

HTML version of Basic Foils prepared July 10 1996

Foil 41 AWT - Grid Layout with Two Components

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
A simple example of using the Abstract Windowing Toolkit divides the applet space into two components. One is a Canvas for drawing graphics or images; one is a Panel which has three buttons. We choose to draw the Canvas within the Applet and to create the Panel in another class.

HTML version of Basic Foils prepared July 10 1996

Foil 42 AWT - Adding Components to a Layout

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index

HTML version of Basic Foils prepared July 10 1996

Foil 43 AWT - Canvas Component

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
The initialization of the Canvas was done in the applet init method; the other method for painting the Canvas simply changes the background color. About the only thing a Canvas can do is explicit drawing and graphics.

HTML version of Basic Foils prepared July 10 1996

Foil 44 AWT - Handling Actions from Components

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
The Button component generates an action whenever the button is clicked on. In this example, we call the method that changes the color of the Canvas in another part of the layout.

HTML version of Basic Foils prepared July 10 1996

Foil 45 AWT - Panel Component with Buttons

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
This class does not need to be a subclass of Applet - like all components except Canvas, you don't need to paint, just add components and handle events and actions.

HTML version of Basic Foils prepared July 10 1996

Foil 46 I/O and Networking

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
illustrating the section Networking, Web Access and I/O in the JavaTutorial

HTML version of Basic Foils prepared July 10 1996

Foil 47 I/O: Reading a File known by URL

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
This applet creates a text area in the window, reads text from a file known by its URL, and displays the text in the text area. It uses the class URL from java.net, to set up a socket to the server, and the class InputStream from java.io.

HTML version of Basic Foils prepared July 10 1996

Foil 48 I/O: Setting up the URL, Layout and Thread

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index

HTML version of Basic Foils prepared July 10 1996

Foil 49 I/O: Reading the Text File

From Introductory Lectures on Java -- An Overview Mixing Code and Comments Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. *
Full HTML Index
The openStream method opens the socket connection and returns an object of type InputStream. We convert this to a BufferedInputStream for performance and then to DataInputStream, where we have methods to read lines or characters of data.

Northeast Parallel Architectures Center, Syracuse University, npac@npac.syr.edu

If you have any comments about this server, send e-mail to webmaster@npac.syr.edu.

Page produced by wwwfoil on Sun Dec 14 1997