Tying up the Loose Ends

At this point, we have dealt with events, components, and containers both in summary and in depth. There are just a few other topics to cover to conclude the chapter.

JDK 1.4 image I/O

Java at last has an API for image I/O, allowing you to read and write files in several popular formats both locally and across the net. It's nicely-designed and extensible so that as new formats appear they can quickly be supported by Java.

Image I/O (introduced in JDK1.4) supports reading GIF, JPEG, and PNG formats. GIF is not supported for writing because it uses LZW compression, the algorithm for which is encumbered by a patent held by Unisys. One patent expired in June 2003, but Unisys has indicated that it thinks it holds some other patents that continue its ownership of GIF.

PNG is “Portable Network Graphics,” a newer standard intended to replace GIF, and not encumbered by patents. You can read about the formats (any format, in fact) at www.wotsit.org.

There are five packages in Java Image I/O, but most of them are concerned with the implementation and plugability of new support. You will probably find that the class javax.imageio.ImageIO contains static methods to do all the simple things you need. You can read a JPEG file into an Image with the following two lines:

import javax.imageio.*;


File f = new File("c:imagesmyimage.jpg");
BufferedImage bi = ImageIO.read(f);

It is equally easy to write out an image in PNG or JPEG format. If you refer back to the previous chapter, we showed code to do screen capture into an object of type java.awt.Image. When you have one of these, check if it is the BufferedImage subclass. You can write BufferedImages to a file with the following lines:

BufferedImage bi = (BufferedImage) myImage;
File f = new File("c:imagesmyimage.jpg");
ImageIO.write(bi, "jpg", f);

When you have something as an image, you can easily get its graphics context and draw on it, as shown in the following code:

Graphics2D big = bi.createGraphics();
big.setPaint(Color.red);
big.drawString ("Date: Oct 2004", 20, 20);

Those lines of code let you put a date or other caption in the top left of your digital photos.

The javax.imageio package contains the basic classes and interfaces for reading and writing thumbnails and for controlling the image reading process (ImageReader, ImageReadParam, and ImageTypeSpecifier) and the image writing process (ImageWriter and ImageWriteParam). It also supports conversion between formats (ImageTranscoder).

More information about Java 1.4 Image I/O is online at java.sun.com/j2se/1.4/docs/guide/imageio/spec/apps.fm1.html

Games programmers will love the “full screen exclusive mode” of JDK 1.4. It lets the programmer suspend the windowing system and write directly into video memory, and onto the screen. Because you are writing directly to memory rather than through many layers of GUI software, it is very fast, although it lacks the features of a window system.

A similar mode is available using Microsoft's DirectX library, which is widely used by games programmers. Now games programmers can get the performance they need, and the future-proofing/portability of Java code too. Full-screen exclusive mode is handled through a java.awt.GraphicsDevice object . For a list of all available screen graphics devices (in single or multi-monitor systems), you can call the method getScreenDevices on the local java.awt.GraphicsEnvironment().

The toolkit

A Component method called getToolkit() returns a reference to the toolkit. The name toolkit just means “a collection of generally useful window-related things” and is the “T” in AWT. Once you have a Toolkit, you can call any of its methods, which can do the following:

  • Set things up for printing the screen.

  • Get information about the screen size and resolution.

  • Beep the keyboard.

  • Get information about the color model in use.

  • Get information about font size.

  • Transfer data to and from the system clipboard.

  • Set the icon image of the Frame.

For example, java.awt.Toolkit.getDefaultToolkit().getScreenSize() returns a java.awt.Dimension object, which has ints representing height and width. As usual, you can view all the methods by typing javap java.awt.Toolkit.

JFC introduced the ability to transfer data to and from the system clipboard and to drag-and-drop components. The topics are a bit outside the scope of this book, but you can find the source files in $JAVAHOME/src/java/awt/datatransfer and dnd, (dnd is drag'n'drop). There's a tutorial online at java.sun.com/docs/books/tutorial/uiswing/misc/dnd.html

Printing the screen

Java's world view of printing is a little strange. It regards the purpose of printing as being to transfer GUI content onto paper. Most people think that printing is about transferring the content of files onto paper with neat margins and fonts. Once you understand that Java printing means rendering frames and panels onto paper instead of the screen, some of the features become clearer. If you really want to print files, bring them up in a GUI or use the OS-specific commands.

Java support for printing has evolved over the years, and JDK 1.4 introduced the third variation on a printing API. JDK 1.0 had no support for printing. JDK 1.1 offered some basic low-resolution printing to a printer based on the class PrintJob. JDK 1.2 introduced the java.awt.print package, and the ability to print anything that can be rendered on the screen. It uses callbacks; the application provides information on the components that it wants printed. The printing system will call back to the paintComponent() method when it is read to print. Instead of passing it a Graphics context that relates to the screen, it passes one that will eventually get sent to the printer. There's a certain convenience for everyone in this approach. It makes it easy for the printing subsystem to print pages repeatedly or in a different order. Just as the window system can call on any component to repaint itself at any time, the printing system has the same ability, and callbacks provide it.

JDK 1.3 added two new classes giving finer control over properties of a print job (like destination or number of copies) and attributes of a printed page (like paper size, orientation, and quality).

JDK 1.4 implements JSR006, the unified printing API. It is unified in the sense that it allows printing on all platforms, including the Java 2 Micro Edition. It builds on the JDK 1.2 print API, and has a new package, javax.print. That package lets you discover and select print servers. You also get improved formatting options, and there is an interface for plug-ins, so third parties can provide their own plug-in printing services.

Changing cursor appearance

In JDK 1.0.2, the cursor could be set only for a Frame. In jdk 1.1, this restriction was lifted, and the cursor can now be set for each individual Component. The cursor is the little icon that moves about the screen tracking the mouse movements. There are fourteen different cursor icons listed in Table 22-1.

Table 22-1. Fourteen cursor icons

Appearance

Name

Eight different directions for resizing

Cursor.SW_RESIZE_CURSOR, etc.

One default cursor

Cursor.DEFAULT_CURSOR

One crosshair cursor

Cursor.CROSSHAIR_CURSOR

One text cursor

Cursor.TEXT_CURSOR

One busy waiting cursor

Cursor.WAIT_CURSOR

One hand cursor

Cursor.HAND_CURSOR

One move cursor

Cursor.MOVE_CURSOR

Figure 22-11 shows some of these icons.

Figure 22-11. Some of the cursor icons

image

The cursor appearance can be set for any component with the following method:

public synchronized void setCursor(Cursor cursor)

For example, to set the hand cursor, use the following:

this.setCursor(new Cursor(Cursor.HAND_CURSOR) );

There is a getCursor() method, too. There is no way in JDK 1.1 to supply your own bitmap for a custom cursor, though obviously this is a reasonable thing to do. Custom cursors arrived in JDK 1.2 with the following toolkit method:

Cursor createCustomCursor(Image cursor, Point hotSpot, String id)
        throws IndexOutOfBoundsException

You must override that method, and then call setCursor().

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.210.205