K12 Java Examples

These examples were developed explicitly for Saturday Java Academy. The suggested exercises are explained in detail on the Java Academy Lab Exercises page.

Applets

Example Synopsis
HelloWorld
(1.1 applet || exercise)
The simplest Java applet
AppletDemo
(1.1 applet)
A demo of the various applet methods called automatically by the Java virtual machine
AppletDemo2
(1.1 applet)
An extension of the previous applet to include the update(..) and repaint() methods

Elementary Java Graphics

Note: These elementary graphics examples were originally taken from chapter 9 of the second edition of Deitel & Deitel's textbook Java How To Program (Prentice Hall, 1998).
Example Synopsis
LineTest
(1.1 applet || exercise)
Drawing lines with the drawLine(..) method
RectTest
(1.1 applet || exercise)
Drawing rectangles with the drawRect(..) and fillRect(..) methods
RoundRectTest
(1.1 applet || exercise)
Drawing rounded rectangles with the drawRoundRect(..) and fillRoundRect(..) methods
Test3DRect
(1.1 applet || exercise)
Drawing 3-d rectangles with the draw3DRect(..) and fill3DRect(..) methods
OvalTest
(1.1 applet || exercise)
(another exercise)
Drawing circles and ellipses with the drawOval(..) and fillOval(..) methods
ArcTest
(1.1 applet || exercise)
(another exercise)
Drawing arcs with the drawArc(..) and fillArc(..) methods

Java Geometry

Example Synopsis
TriangleTest
(1.1 applet || exercise)
Drawing triangles with the drawPolygon(..) and fillPolygon(..) methods
QuadrilateralTest
(1.1 applet || exercise)
Drawing quadrilaterals with the drawPolygon(..) and fillPolygon(..) methods
OctagonTest
(1.1 applet || exercise)
Drawing octagons with the drawPolygon(..) and fillPolygon(..) methods
HexagonTest
(1.1 applet || exercise)
(another exercise)
Drawing hexagons with the drawPolygon(..) and fillPolygon(..) methods

Java Fonts

Note: Each of the test programs listed below has a corresponding exercise. See the Java Fonts lab exercise page for details.
Example Synopsis
FontModel
(1.1 applet)
An illustration of the Java font model
FontMetricsTest
(1.1 applet)
Center a string horizontally in the applet window
FontMetricsTest2
(1.1 applet)
Center a string both vertically and horizontally in the applet window
FontMetricsTest3
(1.1 applet)
Center two strings both vertically and horizontally in the applet window
FontMetricsTest4
(1.1 applet)
Center n strings both vertically and horizontally in the applet window
FontList
(1.1 applet)
An applet for viewing the fonts available on a client machine

Working With Color

Example Synopsis
ColorBoxes
(1.1 applet || exercise)
Display thirteen pre-defined color swatches
ColorBoxes2
(1.1 applet || exercise)
Display color swatches with labels
ColorBoxes3
(1.1 applet || exercise)
Display color swatches with labels using arrays and loops
ColorBoxes4
(1.1 applet || exercise)
Display color swatches with labels using a 2-d array and a nested loop
ColorBoxes5
(1.1 applet || exercise)
Display color swatches with labels using a 2-d array, a nested loop, and some color constants
SystemColors
(1.1 applet)
Display the colors used by system
ColorString
(source)
A ColorString class that may be used in the exercises

Object-Oriented Geometry

Example Synopsis
TriangleTest
(1.1 applet || exercise)
Drawing triangles using a Triangle class
Quadrilateral
(source)
A standalone Quadrilateral class
QuadrilateralTest
(1.1 applet || exercise)
Drawing quadrilaterals using the above Quadrilateral class
OctagonTest
(1.1 applet || exercise)
Drawing a regular octagon using an Octagon class
HexagonTest
(1.1 applet || exercise)
Drawing a regular hexagon using a Hexagon class and an extension of the java.awt.Polygon class called MyPolygon

Graphical User Interfaces

All of the following simple examples are available from a single pop-up menu. Note, however, that none of the examples have event handlers; for working GUI examples, see the 1.1 AWT examples on NPAC's Java Examples page.
Example Synopsis
LabelDemo
(1.1 applet)
Demonstration of Label class
ButtonDemo
(1.1 applet)
Demonstration of Button class
TextFieldDemo
(1.1 applet)
Demonstration of TextField class
TextAreaDemo
(1.1 applet)
Demonstration of TextArea class
CheckboxDemo
(1.1 applet)
Demonstration of Checkbox class
CheckboxGroupDemo
(1.1 applet)
Demonstration of CheckboxGroup class
ChoiceDemo
(1.1 applet)
Demonstration of Choice class
ScrollbarDemo
(1.1 applet)
Demonstration of Scrollbar class
ListDemo
(1.1 applet)
Demonstration of List class

Advanced Fonts

Example Synopsis
DrawableString
(source)
A DrawableString class derived from java.awt.Font, which is based on FontMetricsTest2 above
DrawableStringTest
(1.1 applet)
Using the DrawableString class to center a string in the applet window
DrawableStrings
(source)
A DrawableStrings class derived from java.awt.Font, which is based on FontMetricsTest4 above
DrawableStringsTest
(1.1 applet)
Using the DrawableStrings class to center arrays of strings in the applet window

Advanced Graphics

Example Synopsis
DrawablePolygon
(image map)
A custom graphics package of geometric classes derived from java.awt.Polygon complete with test programs
HexagonTest
(1.1 applet || exercise)
Rotated polygons
QuadrilateralTest
(exercise)
A challenging exercise
MovingSquare
(1.1 applet || exercise)
Animated squares
MovingPolygons
(1.1 applet || exercise)
Animated polygons using a MovablePolygon class
MovingPolygons2
(1.1 applet || exercise)
Animated polygons with double buffering
AnalogClock
(exercise)
Another challenging exercise

Advanced GUIs

Example Synopsis
SystemColors2
(1.1 applet)
The new AWT ScrollPane class and a custom ColorCanvas class