Java Class Samples Page
This page provides a few sample Java classes that I've written
in response to questions. If you have a Java programming question,
visit the Java area
Except where indicated otherwise, all of this code is portable to all
recent versions of Java.
Click on one of the names below to jump directly to that sample.
- SensitivePolygon - AWT example, a drawable shape that accepts mouse-clicks
- DoubleHash - A bi-directional hash table example
- RightFormat - An example of a java.text.Format that does right-justification
- ClockLabel - An AWT component that displays the time of day (uses threads)
- VectorSched - Algorithm example, a simple constraint scheduler using Vectors
- DayServer - A simple java.net.Socket example of the Unix daytime protocol
- BinCat - An IO example showing how to output binary data
- BigFibonacci - Sample that uses java.math.BigInteger to compute Fibonacci numbers
- DualServer - A network coding example: a dual TCP/UDP server
- HmacUtility - A sophisticated utility that compute Hash-MAC values using SHA.1
- InvertedIndexDoclet - An example Javadoc Doclet that creates inverted indices
- TrieStringSet - A collection Set data structure that uses a trie to store Strings
- MailMerge - Network code sample that uses SMTP to send form letters
- TryDateFormat - A little utility for experimenting with DateFormat specifications
- WebSkeletonizerServlet - An example servlet that produces web page skeletons
- Drag1 - A simple demo of Java drag-n-drop data transfer
- FileWalker - Sample console app showing how to walk directories with java.io.File
- HtmlParseDemo - Demo app illustrating how to use Swing's HTML parser
- XmlView - A substantial example illustrating the XML DOM API and Swing
- GradientPainter - Graphics package and sample app for painting gradient fills
- CircleTextDemo - Graphics2D demo showing how to draw text along a circle
- JTiledPanel - a handy subclass of JPanel that supports a tiled image background
- ImageCombiner - an example of how to combine two images using java.awt.image facilities
- IIOTest1 - an example program showing how to use JAI's Image I/O facility
- JManEx - a full-up Mandelbrot Set explorer application
- ImageLabelServlet - an example servlet that labels images
- ProgressMonitoredDataTransfer - Swing utility for using the ProgressMonitor
- TextPrinter - Demonstration how using the java.awt.print package to print text
- XML Hierarchical Structure Creator - XML utility for generating nested structure
- Two Port Server - Socket example showing how to support multiple clients, threads, and more
- Print Text Demo - a Swing application showing how to do simple printing from Java
This class is an extension to java.awt.Polygon. It knows how to draw
itself, how to change color on mouse entry/exit. It can act as a
source of mouse events, accepting one or more java.awt.event.MouseListener
event recipients. This class requires JDK 1.1 or later.
This class is a utility data structure that implements a 2-way
associative mapping; it is essentially a hash table that allows look-up
in both directions. DoubleMap is an interface which DoubleHash implements.
This class is an implementation of java.text.Format, it handles
right-justified string fields. Use it in conjunction with NumberFormat
and DateFormat for formatting tabular data.
This extension to java.awt.Label provides a time-of-day clock.
It is suitable for use in applications and applets, and may be used
anywhere that a Label can be used.
This class illustrates a solution to a peculiar subtype of constrained
scheduling problem. Briefly, the class accepts a set of time-slots and
a set of courses to be scheduled in those time-slots, along with a set
of constraints preventing certain pairs of classes from being scheduled
at the same time. This solution uses mostly the java.util.Vector
data structure, which was a condition imposed by the instructor of the
person who originally asked me the question; certainly Vector is not
ideal for supporting a program of this kind, but it works.
This is an interesting example of implementing recursive search
with backtracking in Java, a technique
that is commonly used in teaching Lisp. The class TestVectorSched
illustrates how to set up and invoke the scheduler.
- Dayclient and Dayserver
These two classes show an extremely simple example of java.net socket
programming. They implement the Unix 'daytime' protocol, an extremely
simple protocol that consists entirely of the server sending its
current local time and date to the client as an ASCII string.
The server, to keep it very simple, does not use multiple threads.
This code requires JDK 1.1 or later, but can easily be adapted to JDK 1.0.
The BinCat class reads raw data bytes and prints them in binary
representation. This small class illustrates some simple
concepts in bit manipulation and I/O streams in Java.
Note that the output handling is far less efficient than
it might be, in order to keep the code simple.
- BinCat.java - source code, 55 lines, includes self-test
The BigFib class computes and prints Fibonacci numbers.
This small class illustrates the use of the java.math.BigInteger
class in Java. This class includes a self-test, which accepts an
integer from the command line and prints that many Fibonnaci numbers.
This class requires JDK 1.1 or later.
- BigFib.java - source code, 52 lines, includes self-test
This small class, and its inner classes,
illustrate the use of Sockets and Datagram sockets. This class
was written in response to an email question, where the questioner
asked if it was possible for a single application to provide service
on both TCP and UDP sockets. Of course, it is. This application
provides the 'echo' service on both TCP and UDP port 7.
You can test the TCP part with telnet; for testing the UDP part,
use the provided DatagramTest class.
These classes require JDK 1.1 or later.
- HMAC Utility
illustrates very very simple use of the Java Cryptography Extension (JCE),
as well a way to obtain simple text data off the clipboard.
These classes require JDK 1.2 (Java 2) and a JSA-compliant crypto
library, like Sun's JCE 1.2.
- Inverted Index Doclet
This code sample shows how to write a simple Javadoc 1.2 Doclet.
Used with Javadoc, it can generate a Unix-style inverted index for
a set of Java classes, with links to real Java API documentation.
Includes directions and source code.
This doclet requires JDK 1.2.1 or later, it will not work with
- TrieStringSet Collection
This code sample shows how to implement an idiosyncratic data structure
within the Java 2 Collection Framework (to learn more about collections,
see this collections tutorial). This package uses a Trie, a peculiar
data structure that can store and retrieve strings, to implement the
collection Set interface. The packages below come with the
source code, class files, a test harness, and full documentation.
This class requires JDK 1.2.1 or later, it will not work with
- MailMerge GUI Application
This code sample shows a simple but complete application: a mail
merge tool. It accepts data files, produces e-mail messages, and
sends them using SMTP. This modest program (only 4 classes)
also illustrates some simple Socket
client programming and the use of java.util.StringTokenizer.
This application requires JDK 1.1 or later.
- Date Format GUI
This code sample shows a small application that mimics an old
console-based application that I wrote a long time ago. This
program allows a user to enter a time value and a SimpleDateFormat
specification, and then shows the results of formatting the time
with that specification. Uses simple AWT facilities and the
This application requires JDK 1.1.5 or later.
- Web Skeletonizer Servlet
This code sample is a modest servlet built to run under the
Servlet API 2.0 or later. It analyzes HTML web pages and
extracts the links and other URL references from them.
As an example, this code shows how to use the URL and
URLConnection classes, the HttpServlet class and related
servlet facilities, and the HttpSession class. (Note: this
servlet uses the
GNU Regexp 1.0.8 package, included
in the distribution below.)
This application requires JDK 1.2.1 or later, JSDK 2.0 or later.
- wss-servlet.jar - Jar file with source, classes, and Gnu regexp pkg. (JDK 1.3rc3)
- Drag1 Drag-and-Drop Demo
This small program shows how to make simple use of the facilities
of the java.awt.datatransfer and java.awt.dnd packages.
To run it, just compile the source
file below, and run it with no arguments.
This sample requires JDK 1.2.1 or later.
- FileWalk directory walker
This small program illustrates simple recursion and use of the
java.io.File class facilities. To run it, just compile the source
file below, and run it with a command-line argument of the
base directory you want to examine.
This sample requires JDK 1.2.1 or later, but by commenting out just
one line, it can run with JDK 1.1.7.
- Swing HTML Parse Demo
The Java Platform 2 includes HTML support as part of the Java Foundation
Classes (Swing). This small program shows how to use the HTMLEditorKit.Parser
and ParserDelegator classes to parse HTML code from a file or URL.
This sample requires JDK 1.2.1 or later.
- XML Parsing Demos
Several XML parsers have been written for Java. One set is called
and it was released by JavaSoft. A stable and mature XML implementation,
the Project X packages are easy to use and pretty fast. There are two
XML parsing models supported by the packages: the serial access model (SAX)
and the document object model (DOM). Both programs parse an XML file, and
display the results in an indented list. XmlView1 shows off SAX, and XmlView2
shows off DOM. Both of them feature very simple AWT interfaces.
These samples require JDK 1.2.1 or later, plus the JavaSoft 'Project X' release
2 package or any later implementation of SAX and XML DOM.
- Gradient Painter package
This is a simple graphics package that illustrates how to use AWT
Image and Color objects.
The gradient painter package works in Java 1.1 and Java 2.
- Text on a Circle
This code example illustrates using the Java2D graphics (java.awt.geom)
package to draw text along the outside of a circle. This is something
that was nearly impossible with straight AWT graphics,
but is quite feasible using affine transforms in Java2D.
This example requires Java 2 (JDK 1.2.1 or later).
- JPanel with a Tiled Image Background
This code example illustrates subclassing JPanel to provide slightly
customized painting, and some Image handling. This example is for
Swing, but the technique shown
here could easily be adapted to AWT.
This example requires Java 2 (JDK 1.2.1 or later).
- Combining Two Images by weighted addition
This code example illustrates using the facilities of the java.awt
and java.awt.image packages to combine two images into one.
The program accepts two images, displays them, and displays the
This example requires Java 1.1.
- Using JAI Image I/O to Read & Write Images
This code example illustrates using the facilities of the javax.imageio
and java.awt.image packages to read, process, and write out image files.
This program also prints a little bit of diagnostic information about
This example requires Java 2 and J2SE 1.4 beta 2 or later, or J2SE 1.3
and the JAI-IIO package.
- Java Mandelbrot Explorer (JManEx 0.6)
This program illustrates a great many Java programming techniques,
including AWT usage, color handling, image I/O with and without JAI-IIO,
interfacing to native code, using serialization and resources, writing
a custom ImageProducer, and much more. It also draws really cool pictures.
This example requires Java 2 and J2SE 1.2.2 or later, preferably
- Image Label Servlet
This modest servlet illustrates how to use the AWT and JAI IIO facilities to
modify an image in a servlet, and then send the image to the browser.
It applies a parameter-specified text label in the upper-left corner of
This example requires the servlet framework and J2SE 1.4.
This Swing utility class incorporates concepts from several parts
of the JFC to provide a useful service: a ProgressMonitor for the
transfer of data from one stream to another (e.g. from a file to a
Socket). It illustrates correct Swing thread handling, use of the
Swing Timer class, and use of the Swing ProgressMonitor.
This sample requires the Java 2 and JDK 1.3 or later.
This code example shows how to use the facilities of the java.awt.print
package to send a print job to the printer. Printing from Java is
rather tricky, but it is platform-independent. This code example
illustrates some of the tricks you need to perform to get
text printed. For a more information about printing, see this
part of the Java tutorial:
This sample requires Java 2 and JDK 1.3 or later. I've only tested
it with Java 1.5.
- Xhsc - XML Hierarchical Structure Creator
This rather specialized XML utility program and package supports
generation of deeply nested XML structures from flat XML and certain
kinds of plain text.
This system requires the Java 2 and JDK 1.4 or later.
- TwoPort - Two port socket server example
This sample program is a Socket server that serves two groups of clients.
A line of text received from any client in group A gets send to all clients
in group B, and vice-versa. It reduces the chances of blocking by using
multiple threads. This code also illustrates use a Fixed Thread Pool
and an ExecutorService.
This sample requires the Java 5.0 (JDK 1.5) or later.
- PrintTextDemo - Simple PrinterJob printing example
This sample program presents a simple Swing GUI, and uses the java.awt.print
package to print a text file. Use this example to learn about the
PrinterJob class and implementing the Printable interface.
Note: while this program works properly on most printers, there are
some where the print appears too large. I haven't been able to
figure out why this occurs.
This sample requires the Java 1.4.2 or later. It has only been
tested with 1.5 and 1.6.
For a many more program samples, visit one of the links below,
or visit my java links list.
Java FAQ and Sample Code - Answers for various Java questions, with code examples
Java Developer Connection Online Training
Netscape Java Samples - Sample Applet and other code for Netscape products
COP-3530 Java Samples - Sample code for various data structures in Java
The Java Repository - A searchable stockpile of Java code and other stuff
Planet Source Code Java world - searchable Java code archive
[Main Java Page] [Neal
This page written by
Ziring, last modified 11/12/08.