Newsletter sign-up
View all newsletters

Sign up for our Enterprise Java Newsletter

Enterprise Java

Getting started with Java 2D

New support for 2D shapes, transforms, curves, and fonts enters the core environment with Java 1.2

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
The Java 2D API is a core Java 1.2 platform API (see Resources for a variety of information on the API and its implementations). Implementations of the API are available as a part of the Java Foundation Classes (JFC) in the current beta releases of the Sun JDK for Windows NT/95 and Solaris. As Java 1.2 is finalized, Java 2D should become available on more platforms.

Note that although Java 2D has been developed somewhat independently of the other parts of the JFC, it is nonetheless a core part of the 1.2 AWT. We will make the distinction and point out 2D-specific features for discussion, but you should remember that this functionality is just as central to 1.2 graphics as the old 1.0 and 1.1 AWT support.

Java 2D extends the previous AWT mechanisms for drawing 2D graphics, manipulating text and fonts, loading and using images, and defining and dealing with colors and color spaces. We will be exploring these new mechanisms in this and future columns.

A note about nomenclature and conventions

For this column, my primary development platform will be a PC running Windows 95 or Windows NT. I hope to provide other platform-specific tips and tricks where possible, but I will focus on Windows since that's where I will be spending most of my time.

When I write a method name, it should always be of the form methodname(). The trailing parentheses are meant to set this apart as a method. The method may or may not take parameters. In practice, the context should always make this clear.

Source code listings will be given with line numbers included. I plan to use the line numbers to cross-reference the article text and the code listings as appropriate. This should also make it much easier for you to annotate the column, should you chose to print a copy. Please note, however, that the source files linked from the column will be regular *.java files (sans line numbers) so that you can download and develop with them.

Because I will be writing about many of the Media and Communications APIs in the coming months, I want to make sure that all of the sample code makes sense as a whole as well as in its individual parts. I will attempt to consistently name my examples and place them into sensical packages.

The top of my package hierarchy will be:

com.javaworld.media


Each API or topic that I write about will have at least one subpackage under this top level. For instance, all of the code for this Java 2D article will be in:

com.javaworld.media.j2d


So, to invoke the first example application on Java 2D, you would download the code, put it in your classpath, then use:

java com.javaworld.media.j2d.Example01


(If the namespace is too long for your liking or for some other reason you want to use the example code without having to use the fully qualified name, simply comment out the package line at the beginning of each source code file.)

I will generate a Java Archive (jar) file for each article's example code and class files. This archive will be made available in the Resources of each column, should you wish to download it and execute the examples from within the archive.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources
  • Download the source and class files as a jar file. By placing this jar in your classpath, you can execute the examples in this column without unzipping and compiling the code yourself. http://www.javaworld.com/jw-07-1998/media/jw-07-media.jar
  • Sun's Java 2D home page. This page links to many of the other available Java 2D references, such as the Sun FAQ, the API Specification and User Guide, and sample Java 2D applications. Sun's Windows NT/95 and Solaris implementations are available from this site. http://java.sun.com/products/java-media/2D/index.html
  • This Java Tip documents a workaround for the font-related bug in the Windows version of JDK 1.2 Beta 3. If you are using 1.2B3 on Windows NT/95, you should check this out. http://www.javaworld.com/javaworld/javatips/jw-javatip55.html
  • Haven't used AWT graphics much? Get a firm grounding in JDK 1.0 and 1.1 AWT graphics by reading up on the Graphics class. http://www.javaworld.com/javaworld/jw-11-1996/jw-11-howto.html
  • Developer.com published an article on getting started with Java 2D, titled "An introduction to the Java 2D API." This article goes into good depth explaining AffineTransforms and GeneralPaths, so I would encourage you to read through it if you need to know more about them. http://www.developer.com/news/techworkshop/020598_java2d.html
  • Foley et al.'s text, "Introduction to Computer Graphics", provides a great deal of information on the choice of Bezier and other higher-ordered curves. Chapter 9 explains the preference for cubic curves in the context of both 2D and 3D graphics. A classic graphics reference. http://www.amazon.com/exec/obidos/ASIN/0201609215/billday/ http://www.clbooks.com/sqlnut/SP/search/gtsumt?source=javaworld&isbn=0201609215
  • Knudsen's "Curves Ahead" explains the relationships between character glyphs and the Shape object for a string of text. It also discusses how paths are described using PathIterators and gives a little bit more information on how control points are used to generate curves in Java 2D. http://java.oreilly.com/news/knudsen/java_0398.html
  • Archive of the Java 2D interest mailing list. Where to go with in-depth technical questions about making Java 2D work in your programs. Be sure to search the archive before posting. http://java.sun.com/products/java-media/mail-archive/2D/index.html
  • Follow the thread discussing the Windows NT problem involving shapes and text strings. Begins with the first post relating to the bug. http://java.sun.com/products/java-media/mail-archive/2D/1409.html
  • I wrote a graphics feature for the JavaOne Today show daily for JavaOne 1998. This discusses changes and updates to the Java 2D, Advanced Imaging, and Printing APIs. http://www.javaworld.com/javaworld/javaone98/j1-98-graphics.html
  • Bill's other Media Programming columns /javaworld/topicalindex/jw-ti-media.html