Java 2D API Graphics

From the Book: 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. 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 andextensibledesign, but it is not simple. It is important to understand this design well. Then, the API becomes easy to program. 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: 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. 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. 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 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 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 runsnippet For example: runsnippet AnchorPlacementTransform