Java 2D API Graphics

Java 2D API Graphics

4.5 2
by Vincent J. Hardy
     
 

Awesome graphics with the Java 2D API--step by step!

Sophisticated graphics are crucial to a wide range of applications. Now you can create awesome graphics using Java technology without the expense and hassle of platform-specific development. Sun Microsystems Java technology architect Vincent Hardy shows you how-in full-color! From text manipulation to geometry

Overview

Awesome graphics with the Java 2D API--step by step!

Sophisticated graphics are crucial to a wide range of applications. Now you can create awesome graphics using Java technology without the expense and hassle of platform-specific development. Sun Microsystems Java technology architect Vincent Hardy shows you how-in full-color! From text manipulation to geometry rendering, compositions and image manipulation, you'll see exactly what can be done-and learn how to do it, step-by-step with detailed code examples.

Java 2D API Graphics offers an in-depth overview of the Java 2D API, its architecture, features, components and possibleapplications. It also introduces Hardy's exclusive Graphic Layers Framework-a high-level programming model that dramatically simplifies programming with the Java 2D API (included free on the accompanying CD-ROM).

Key topics covered include:

  • Fundamental Java 2D API principles and concepts: shapes, text, images, color management, compositing, transformations and more
  • Rendering tradeoffs between speed and quality
  • Simple to advanced code examples
  • Leveraging the power and complexity of the Java 2D API with tools and higher level programming models, such as the Graphic Layers Framework
  • Combination and extension of the Java 2D API features: custom paints, strokes, composites, filters and more
  • Composition techniques for special rendering effects such as shadowing, neon glow, textures, embossing, lighting and more

Product Details

ISBN-13:
9780130142665
Publisher:
Prentice Hall Professional Technical Reference
Publication date:
01/26/2000
Series:
Sun Microsystems Press Series
Edition description:
BK&CD-ROM
Pages:
509
Product dimensions:
7.04(w) x 9.23(h) x 0.80(d)

Read an Excerpt

PREFACE:

PREFACE

The Java 2D API provides the underlying support for graphics in the Java platform (see Figure P.1). Other APIs that need graphic support, such as the Abstract Window Toolkit (AWT) or Swing, rely on the Java 2D API for all drawing operations.

Figure P.1
The Java 2D API in the Java platform

The Java 2D API became available to programmers in its beta form during the second half of 1997 when the first early-access releases of the Java 2 platform (JDK 1.2) became available. The specification had been out for some time before that, and I (although not working ar Sun at the time), along with many, had great expectations. Why?

The Java 2D API: An Amazing Addition to the Java Platform

If you had a look at the specification back then, you soon realized that Java graphics were about to change dramatically. The announced feature set was impressive:

  • Transparency support
  • Generalized geometry support
  • Output device resolution independence
  • Improved font support
  • Sophisticated fills such as gradient fills
  • New, simplified, and powerful imaging model
  • and a lot more

Expectations ran high, and when the API finally became available, many programmers started using all these promising features. Anybody who went through this experience soon noticed a number of key points about the Java 2D API.

  1. The Java 2D API is a sophisticated API. It is easy to program, but it is not simple. Similar to Swing in that regard, the Java 2D API has an extremely powerful and extensibledesign, but it is not simple. It is important to understand this design well. Then, the API becomes easy to program.
  2. The Java 2D API is a rich, generic, and low-level API. The API covers a broad portion of computer graphics (but not 3D graphics), and it is designed to serve all kinds of graphics needs. As a consequence, it is feature rich and has a generalized model that fits the largest possible range of requirements: it is generic and low level.

Purpose of This Book

The Java 2D API delivers a wealth of features, but it may not be obvious how they can or should be used. Actually, it depends on the type of application; the way features are used or the reason for which they are used depends on whether you are, for example, writing a satellite image processing program or a web animation.

This book looks at how the Java 2D API features can be used, combined, and extended.

When we say the API is feature rich, we really mean that there has been a huge feature improvement from earlier versions of the JDK. To use a musical analogy, earlier versions of the Java platform provided a flute. The Java 2D API gives us an orchestra. With a flute, a music score is useful to play the proper sounds. This is even more true for a symphonic orchestra: guidance is needed to have all the instruments and musicians play in harmony and produce a pleasing result. The goal of this book is to provide guidance so that programmers can write beautiful partitions with the Java 2D API.

For example, the images in Table P.1 are completely created with the Java 2D API and the Graphic Layers Framework (GLF) provided in this book. These examples show the type of visual output the Java 2D API can create; this book explains how to obtain this type of result and provides all the related software.

Table P.1
Example Rendering Effects; Examples from Part III

Book Structure

We said this book explains how to use, combine, and extend the Java 2D API features. The book structure reflects this purpose; it has three parts.

Part I contains a detailed overview of the Java 2D API. It starts with an introduction to the API rendering model (Chapter 1) and a summary of its features (Chapter 2). Chapters 3, 4, and 5 study the different parts of the API in greater detail.

Part II introduces the Graphic Layers Framework (GLF) that builds on the Java 2D API and provides a higher-level programming model to create and compose rendering effects. I developed the Graphic Layers Framework for this book: it provides an advanced example of how the Java 2D API can be used and extended. GLF contains the following:

  1. A layer framework that allows programmers to create visual compositions by stacking up simple layers and specifying how each layer should combine with the other ones.
  2. A set of extensions to the Java 2D API. GLF offers custom types of fills, brushes, filters, and more that build on the Java 2D API's extensible design.
  3. A set of tools and utilities. They help configure, save, and restore graphical compositions in the form of Java beans. The tools make it easier to deal with the large and sometimes overwhelming number of parameters the API contains. These tools let us change parameters dynamically and view the effect of the change.

The different GLF packages are the foundation for the rendering effects in Part III. That part is about combining features; it demonstrates how sophisticated the graphics support of the Java platform now is. This part shows how to create effects such as shadowing, lighting, and carving. The final chapter of Part III contains a gallery of images that further illustrates the sophistication of the Java 2D API and shows GLF at work.

How to Read This Book

This book can be read in different ways. It can be read linearly, from start to end. This is the recommended way if you are not familiar with the Java 2D API or with computer graphics. This approach will give you an in-depth understanding of the Java 2D API and how this book uses it to create rendering effects.

However, you might decide that you are not interested in the inner workings of the Java 2D API or of the Graphic Layers Framework. In that case, you can start with Part II directly; Chapters 6 and 8 provide enough information about GLF to enable you to use it with no need to understand its implementation or the underlying Java 2D concepts. You will learn how to use the GLF capabilities to create rendering effects. The "Hello Layers" example (Chapter 6) can get you started with GLF quickly. You can then proceed to Part III and start experimenting with advanced rendering effects. If you choose that route, you will likely need to refer back to Parts I and II, where you will find explanations for issues that might seem obscure.

No matter how you read this book, you will find that it contains many code samples (there are about ninety examples, in addition to the Graphic Layers Framework itself). We usually chose to first show the code and then to explain it. This approach gives you a chance to read the code in its entirety first and then to read explanations. So, don't be discouraged if some point seems difficult while you are reading the code: it usually is the point that the following explanation details.

Installing the Book Software

The CD-ROM that comes with this book contains two files:

  • readme.txt
  • glfall.jar

Please refer to the readme file for instructions on how to install the Graphic Layers Framework and the book code examples on your computer.

Running the Code Samples

Throughout the book, you will see code examples and references to the source code on the CD-ROM. For example, on page 238, you can see that the code example is AnchorPlacementTransform.java. We use the name of the corresponding class to refer to the example name, for example, AnchorPlacementTransform.

There are two ways to run the examples provided in this book. One way is to use a tool with a user interface. The other one is to use a script. The former lets you browse through the book's examples. The latter lets you run a specific code example directly.

Using demorunner

At the command prompt, type:

cd <installDir>demorunner

Figure P.2
demorunner startup window

This last commands summons a window, as shown in Figure P.2. The window contains folders for each part of the book. Each folder can be expanded into the different chapter folders, and each chapter folder can in turn be expanded to show its code samples, as illustrated in Figure P.3.

Figure P.3
Expanding demorunner's code example tree

Double-click on an example name to start it. The status bar, at the bottom of the window, shows a message reading Starting <ExampleName> in separate VM. Please wait. For example, in Figure P.4, we double-clicked on the AnchorPlacementTransform example. The example starts after a couple of seconds.













Figure P.4
Starting an example with demorunner

Using the runsnippet script

An alternate way to run a specific example directly is to use the runsnippet script. At the command line, type the following:

cd <installDir>runsnippet <exampleName>

For example:

runsnippet AnchorPlacementTransform

Meet the Author

Vincent Hardy is a Java technology architect at Sun Microsystems in Menlo Park, CA. Vincent is a regular columnist on the Java 2D API for Java Report.

Customer Reviews

Average Review:

Write a Review

and post it to your social network

     

Most Helpful Customer Reviews

See all customer reviews >

Java 2D API Graphics 4.5 out of 5 based on 0 ratings. 2 reviews.
Guest More than 1 year ago
Currently this is the best book on the Java 2D API. It is especially nice because it is printed on glossy paper in color so the graphics show up well. The authors use a good amount of code to illustrate their concepts and they have have a much better understanding and deeper treatment of some of the more complex issues like using rasters. These areas are where the competitive books are really terrible. Another complex area where this book shines is the explanation of affine transforms. The only major weakness of the book (which it shares with the competition) is that its coverage of the API is not comprehensive and many important calls are not really explained. Another problem is that the index is not that great.
Guest More than 1 year ago
>This is one of the most beautiful computer books I have every opened. The pictures are stunning in their brightness and sharpness. This is something I almost never see in a computer book. The pictures and graphics in this book are an incredible aid in learning the material in the book. For a graphical point of view, this book should be the gold standard for computer graphic books in the future. The text is also very good. I highly recommend this book.