Java InstantCode. Developing Applications Using Java NIO

The Java New Input/Output (NIO) API provides the java.nio, java.nio.channels, and java.awt.print packages, which you can use to perform various printer management functions. The java.nio package provides two classes, ByteBuffer and CharBuffer. You can use these classes to buffer a file. To read and write text and image files, you can use two classes provided by java.nio.channels, File and FileChannels. The java.awt.print package provides the PrinterJob and PageFormat classes to specify the properties of a file and print it. You can also use this package to manage the printers connected on the network.

This chapter describes how to develop a Printer Management application using the java.nio, java.nio.channels, and java.awt.print packages. It also explains how to read, write, buffer, format, and print text and image files. The application manages the printers that are connected to the computers that have Windows operating system.

Architecture of the Printer Management Application

The Printer Management application provides an interface that allows end users to create a book, add a document to the book, specify the document page setup, and simultaneously print the required number of copies of that book. The book interface also allows end users to customize the print settings and use the application to manage the network printers. The application provides a Print dialog box that displays the status of the printer.

The Printer Management application uses the following files:

Figure 5-1 shows the architecture of the Printer Management application:

Figure 5-1: Architecture of the Printer Management Application

The PrintFile.java file is the main class of the Printer Management application. The File menu of the PrintFile.java file allows end users to open a document, specify its page setup, and print it. If they select the File-> Print->Print Default or File->Page Setup & Print menu item, the PrintFile.java file calls the PrintComp.java file, which prints the currently open text or image file.

If end users select the File-> Print Custom menu item, the PrintFile.java file calls the CreateBookInterface.java file, which creates a book that can be printed. End users can specify the page format and the number of copies of each page they want to print. All the pages added to the book can be printed using one print command.

Категории