Chapter 12. Graphics and Sound


The Graphics class and the Component method paint( ) have survived virtually unchanged since the early days of Java. Together they provide a basic but quite functional graphics capability. The first printing API was put forward in 1.1, and it was promptly replaced in Java 2 (things change quickly in the online world). Both printing APIs, fortunately, are based on use of Graphics objects, so your drawing code does not have to change: only the details of getting the right kind of Graphics object change in moving from 1.1 to Java 2. The 2D (two-dimensional graphics) package is also based on Graphics: Graphics2D is a subclass of Graphics. To put the 2D graphics in perspective, think about the tremendous boost that the Adobe PostScript language gave to desktop publishing and printing. PostScript is both a scripting language and a marking engine : it has the ability to make a terrific variety of marks on paper. Since Java is already a comprehensive programming language, the 2D API needed only to add the marking engine. This it did very well, using several ideas imported from PostScript via Adobe’s participation in the early design.

Also present from the beginning was the AudioClip class, which represents a playable sound file. In Java 2 this was extended to support additional formats (including MIDI) and to be usable from within an application as well. Meanwhile, the Java Media Framework -- standard extension -- provides for ...

Get Java Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.