Java 2D Graphics features. For heavy-duty processing of large images, check out JAI, which is described at http://java.sun.com/products/java-media/jai/. 1.4 Genesis The Java people at Sun have a crazy ambition to redefine all of computing. Each new version of the Java platform includes vastly expanded capabilities. Between the JDK itself and the extension APIs, Sun seems intent on making Java able to do anything you could possibly want to do with a computer. In order to create the 2D API, the good people at Sun conspired with several industry partners, including the following four companies. 1.4.1 Adobe Sun’s most important partner for the 2D API was Adobe Systems, Inc. These are the people who developed the PostScript language as well as an impressive lineup of graphics and text applications, including Framemaker, Acrobat, Illustrator, and others. Adobe helped Sun design the 2D API. If you’re familiar with PostScript, you’ll probably see echoes of it in the classes and methods of the 2D API. Adobe’s web site is at http://www.adobe.com/. 1.4.2 Ductus A small company called Ductus provided a key piece of the 2D API’s implementation, called a rasterizer. The rasterizer handles the task of representing idealized mathematical shapes on output devices with pixels, like monitors and printers. You can read more about Ductus at their web site, http://www.ductus.com/. 1.4.3 Kodak Another important partner was Eastman Kodak (http://www.kodak.com/). Sun worked closely with Kodak to develop the imaging and color management classes in the 2D API. Some of the implementation of these classes is based on technology licensed from Kodak. Just as Adobe helped design the graphics part of the 2D API, Kodak helped with the design and implementation of the imaging and color management portions of the 2D API. 1.4.4 Taligent It’s a funny industry we work in: Taligent, one of Sun’s partners in the 2D API, no longer exists. Formerly an IBM subsidiary, Taligent has now been reabsorbed into the mother ship. During Taligent’s independent existence, however, Sun licensed two technologies from them for use in the 2D API: bidirectional text layout and constructive area geometry. 1.5 Where Do I Get a Graphics2D? Shapes, text, and images are all ultimately drawn by a Graphics2D object. But where does the Graphics2D come from? As usual, there’s more than one way to do it. 1.5.1 Drawing on Components Every Component that AWT shows on the screen has a paint() method. The system passes a Graphics to this method. In JDK 1.1 and earlier, you could draw on Components by overriding the paint() method and using the Graphics to draw things. page 11
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Java Web Hosting services
Java 2D Graphics The Abstract Windowing Toolkit (AWT) that comes with JDK 1.0 and 1.1 is a large set of classes that encapsulate windows, controls, fonts, images, and drawing. However, the AWT lacks a number of important features, as users of more mature graphics toolkits were quick to point out. Instead of applying a quick fix, the engineers at Sun created the largest, most powerful graphics toolkit yet, the Java Foundation Classes (JFC). JFC is included with Java 2. The 2D API is part of JFC. It is a “core” API, which means that it is present in every implementation of Java 2. It cannot run in older versions of the JDK, however. To understand how 2D fits into the larger scheme of things, it’s helpful to examine how it evolved from AWT. Conceptually, AWT can be split into two pieces: a user interface (UI) toolkit and a drawing toolkit. Between JDK 1.1 and Java 2 (JDK 1.2), these two pieces evolved considerably. The UI toolkit became Swing, and the drawing toolkit became the 2D API. In this section, I’ll explain how Java 2D relates to some other APIs and buzzwords: Java Foundation Classes (JFC) Java 2D is one part of JFC. The other parts are AWT, Swing, the Accessibility API, and the Drag and Drop API. See http://java.sun.com/products/jfc/ for details. AWT In Java 2, you can use the 2D API to draw on AWT components. AWT is described in books such as John Zukowski’s Java AWT Reference (published by O’Reilly & Associates, Inc.). Swing As with AWT components in Java 2, you can use 2D to draw on Swing components.[2] You may want to use 2D to develop your own components or your own look and feel. For more on Swing, see Java Swing , by Robert Eckstein, Marc Loy, and Dave Wood (O’Reilly). Online information is also available at http://java.sun.com/products/jfc/tsc/. [2] This is only true if you’re using Swing in Java 2. Although it is possible to use Swing in JDK 1.1, the 2D API runs only in Java 2 (JDK 1.2). Java Media APIs The Java Media APIs are designed to provide Java multimedia capabilities. The 2D API is part of the Java Media APIs. Other APIs in this collection include the 3D API, the Sound API, and the Advanced Imaging API. The Java Media APIs are described at http://java.sun.com/products/jfc/tsc/. Java 3D Although the 2D and 3D APIs aren’t tightly integrated, you can use 2D to create textures for 3D. You can read more about the 3D API at http://java.sun.com/products/java-media/3D/. Java Advanced Imaging API (JAI) Of all the JFC and Media APIs, JAI is the most closely related to 2D because it builds on the image handling classes in 2D. JAI offers sophisticated image processing and handling page 10
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Java Web Hosting services
Java 2D Graphics Compositing is the process of adding new elements to an existing drawing. The 2D API gives you considerable flexibility by using the Porter-Duff compositing rules, which are described in Chapter 5. clipping Clipping is the process of limiting the extent of drawing operations. For example, drawing in a window is normally clipped to the window’s bounds. In the 2D API, however, you can use any shape for clipping. This process is described in Chapter 5. antialiasing Antialiasing is a technique that reduces jagged edges in drawings. It’s fully described in Chapter 5. The 2D API takes care of the details of producing antialiased drawing. text The 2D API can use any TrueType or Type 1 font installed on your system.[1] You can render strings, retrieve the shapes of individual strings or letters, and manipulate text in the same ways that shapes are manipulated. Drawing text is fully covered in Chapter 6, and Chapter 7. [1] TrueType is a font standard originally developed at Apple and now widespread in the MacOS and Windows platforms. Type 1 fonts are based on Adobe’s PostScript language. Both standards have their merits. See http://www.truetype.demon.co.uk/ for a fascinating description of both formats. color It’s hard to show colors correctly. The 2D API includes classes and methods that support representing colors in ways that don’t depend on any particular hardware or viewing conditions. Chapter 8, discusses these issues in detail. images The 2D API supports doing the same neat stuff with images that you can do with shapes and text. Specifically, you can transform images, use clipping shapes, and use alpha compositing with images. Java 2 also includes a set of classes for loading and saving images in the JPEG format. Chapter 9, has the scoop on drawing images. Chapter 11, describes how image data is stored and interpreted. image processing The 2D API also includes a set of classes for processing images. Image processing is used to highlight certain aspects of pictures, to achieve aesthetic effects, or to clean up messy scans. For full coverage of the 2D API’s image processing capabilities, see Chapter 10. printing Finally, Java developers have a decent way to print. The Printing API is part of the 2D API and provides a compact, clean solution to the problem of producing output on a printer. This API is covered in Chapter 13. 1.3 Relatives page 9
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services
Java 2D Graphics You should see a window that looks like Figure 1.1. Each of the tabs across the top displays a set of 2D’s features. Spend some time with this application. Then come back and read about all the things 2D can do, including: shapes Arbitrary geometric shapes can be represented by combinations of straight lines and curves. The 2D API also provides a useful toolbox of standard shapes, like rectangles, arcs, and ellipses. See Chapter 3, for details. stroking Lines and shape outlines can be drawn as a solid or dotted line of any width a process called stroking. You can define any dotted-line pattern and specify how shape corners and line ends should be drawn. Chapter 4, has all the details. filling Shapes can be filled using a solid color, a pattern, a color gradient, or anything else you can imagine. See Chapter 4 for more information. transformations Everything that’s drawn in the 2D API can be stretched, squished, and rotated. This applies to shapes, text, and images. You tell 2D what transformation you want and it takes care of everything. For more information, see Chapter 5. alpha compositing page 8
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services
Chapter 1. Introduction This chapter describes Java 2D’s roots, contributors, related technologies, and capabilities. I’ll also explain how you can obtain a Graphics2D object in your application, and then I’ll present a useful class that will be used throughout the book. Finally, the chapter concludes with a “teaser” example that shows off some of Java 2D’s features. 1.1 What Is Java 2D? The Java 2D Application Programming Interface (the 2D API) is a set of classes that can be used to create high quality graphics. It includes features like geometric transformation, antialiasing, alpha compositing, image processing, and bidirectional text layout, just to name a few. Don’t worry if you don’t know what some of these features are I’ll explain them all. Java 2D is part of the core classes of the Java 2 platform (formerly JDK 1.2). The 2D API introduces new classes in the following packages: java.awt java.awt.image In addition, the 2D API encompasses six entirely new packages: page 6
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Java Web Hosting services
Java 2D Graphics java.awt.color java.awt.font java.awt.geom java.awt.print java.awt.image.renderable com.sun.image.codec.jpeg All of these packages are part of the core Java 2 platform, except com.sun.image.code.jpeg. This means that, except for the JPEG package, you can rely on the 2D API in all implementations of the Java 2 platform. This book covers all of the new packages, with the exception of java.awt.image.renderable. This package serves as a bridge to the Java Advanced Imaging API (JAI), which is outside the scope of this book. 1.2 What Can Java 2D Do? Java 2D is designed to do anything you want it to do (with computer graphics, at least). Prior to Java 2D, AWT’s graphics toolkit had some serious limitations: All lines were drawn with a single-pixel thickness. Only a handful of fonts were available. AWT didn’t offer much control over drawing. For example, you couldn’t manipulate the individual shapes of characters. If you wanted to rotate or scale anything, you had to do it yourself. If you wanted special fills, like gradients or patterns, you had to make them yourself. Image support was rudimentary. Control of transparency was awkward. The 2D API remedies these shortcomings and does a lot more, too. To appreciate what the 2D API can offer, you need to see it in action. Java 2 includes a sample program that demonstrates many of the features of the API. To run it, navigate to the demo/jfc/Java2D directory in the JDK installation directory. Then run the Java2Demo class. For example: C:> cd jdk1.2demojfcJava2DC:> java Java2Demo Figure 1.1. Sun’s 2D demo page 7
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Java Web Hosting services
206 APPENDIX LICENSING, INSTALLATION, AND PARTICIPATION Figure A-6. The documentation license agreement Caution When I installed the javadoc, the install complete window would hide in the background and not come to the foreground. I had to hunt it out to click OK to end the installation process.
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Java Web Hosting services
APPENDIX LICENSING, INSTALLATION, AND PARTICIPATION 205 Once everything is done, you ll see a screen telling you that the installation is complete (shown in Figure A-5). You can then choose whether to see the README file or not. If you do, the README file is then displayed in a browser window. It has taken some time, but it now finally shows something relevant to Java 6. For the longest time, only Java 5 information was shown in the README. As is expected for prerelease software, some of the links sometimes didn t work (for example, the link to the installation instructions). I guess the links point to where things will be when Mustang is released the joys of prerelease software. Figure A-5. Installation complete In addition to getting the JDK, it is best to also get the javadocs. These come down in an installable JAR file. On the Binary Snapshot Releases web page (http://download.java. net/jdk6/binaries), just follow the first link on the second line, which reads Java Docs (XX MB JAR / HTML) (XX stands for the size of the JAR file). Downloading and unzipping/unjarring the documentation is surprisingly not what you do. Instead, after downloading the file, you run it with the java -jar jarfilename command, replacing jarfilename with the name of the downloaded JAR file. This requires you to accept another license agreement (shown in Figure A-6) before choosing an installation directory. Personally, I tend to enter the same directory as the JDK installation. The documentation will then go into a docs subdirectory. Once installed, you should then bookmark the top-level javadoc page. Installation takes some time.
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Java Web Hosting services
204 APPENDIX LICENSING, INSTALLATION, AND PARTICIPATION Figure A-3. The Custom Setup screen for Mustang installation Figure A-4. Installation progress status Note Installation of the public JRE will display even more screenshots and an additional license that requires acceptance.
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services
202 APPENDIX LICENSING, INSTALLATION, AND PARTICIPATION Sun has been reluctant to release the core Java release as open source. While Apache Harmony (http://incubator.apache.org/harmony) incubates along as an open source J2SE 5.0 implementation, you can t get the source for the core system of Mustang unless you re in an unrestricted country and you agree to the Java Research License (JRL). Iran, North Korea, and Cuba: no. United States, Canada, France, and England: yes. (That is not a complete list in either case.) It appears that Sun doesn t require you to follow their Sun Community Source License (SCSL) for research related to java.net projects. The SCSL is Sun s attempt to open up source somewhat, but not totally. It is geared toward the commercial community and allows that community to offer proprietary modifications and extensions to a particular area, while maintaining compatibility through technology compatibility kits (TCKs). You can get a more complete overview of the license at www.sun.com/software/communitysource/overview.xml. On the other hand, the JRL is geared more toward internal non-production research and development uses. If or when the project turns into something that is distributed, either internally or externally, you then must sign something called the Java Distribution License, which requires its own level of compatibility requirements. While the SCSL does offer a research section, the JRL is geared more toward the research community and universities. For more information on it, see www.java.net/jrl.csp. Getting the Software While JSR 270 describes Mustang (see http://jcp.org/en/jsr/detail?id=270), access to the software comes from the previously mentioned snapshot area. Starting at https://mustang.dev.java.net and following the Latest Mustang binary snapshots link takes you to the weekly binary snapshot drops. You ll find versions for the Microsoft Windows platform, Windows AMD64, Solaris SPARC, Solaris x86, Solaris AMD64, Linux, and Linux AMD64. Macintosh users will need to wait for Apple to release a version. It is best to get the complete self-extracting JDK file for your platform; though if you re only interested in the Java Runtime Environment (JRE), it s available as a JAR file (a self-extracting DEBUG JAR file is also available). Downloading and running the file displays a splash screen (see Figure A-1). Then you get to agree to the prerelease software evaluation agreement (shown in Figure A-2).
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services