| 目次 | 前の項目 | 次の項目 | Java 2D API |
基本的な印刷処理は、次の手順で行います。
次の例では、Printable ジョブを使って 5 ページのドキュメントを印刷し、各ページに緑色でページ番号を付けます。 ジョブの制御は main メソッドで行い、このメソッドの中で PrinterJob を取得して制御します。 レンダリングは、ページペインタの print メソッドで行われます。
import java.awt.*;
import java.awt.print.*;
public class SimplePrint implements Printable {
private static Font fnt = new Font("Helvetica",Font.PLAIN,24);
public static void main(String[] args) {
// Get a PrinterJob
PrinterJob job = PrinterJob.getPrinterJob();
// Specify the Printable is an instance of SimplePrint
job.setPrintable(new SimplePrint());
// Put up the dialog box
if (job.printDialog()) {
// Print the job if the user didn't cancel printing
try { job.print(); }
catch (Exception e)
{ /* handle exception */ }
}
System.exit(0);
}
public int print(Graphics g, PageFormat pf, int pageIndex)
throws PrinterException {
// pageIndex 0 to 4 corresponds to page numbers 1 to 5.
if (pageIndex >= 5) return Printable.NO_SUCH_PAGE;
g.setFont(fnt);
g.setColor(Color.green);
g.drawString("Page " + (pageIndex+1), 100, 100);
return Printable.PAGE_EXISTS;
}
}
ページペインタの print メソッドの中で Graphics2D の機能を実行するには、最初に Graphics コンテキストを Graphics2D にキャストします。次の例では、ページ番号を赤と緑のグラデーションでレンダリングします。 そのために、GradientPaint が Graphics2D コンテキストで設定されています。
import java.awt.*;
import java.awt.print.*;
public class SimplePrint2D implements Printable {
private static Font fnt = new Font("Helvetica",Font.PLAIN,24);
private Paint pnt = new GradientPaint(100f, 100f, Color.red,
136f, 100f, Color.green, true);
public static void main(String[] args) {
// Get a PrinterJob
PrinterJob job = PrinterJob.getPrinterJob();
// Specify the Printable is an instance of SimplePrint2D
job.setPrintable(new SimplePrint2D());
// Put up the dialog box
if (job.printDialog()) {
// Print the job if the user didn't cancel printing
try { job.print(); }
catch (Exception e) { /* handle exception */ }
}
System.exit(0);
}
public int print(Graphics g, PageFormat pf, int pageIndex)
throws PrinterException {
// pageIndex 0 to 4 corresponds to page numbers 1 to 5.
if (pageIndex >= 5) return Printable.NO_SUCH_PAGE;
Graphics2D g2 = (Graphics2D) g;
// Use the font defined above
g2.setFont(fnt);
// Use the gradient color defined above
g2.setPaint(pnt);
g2.drawString("Page " + (pageIndex+1), 100f, 100f);
return Printable.PAGE_EXISTS;
}
}
ページペインタの print メソッドが同じページに対して繰り返し呼び出される場合、メソッドはそのたびに同じ出力を生成しなければなりません。同じページに対して繰り返しレンダリング要求があるたびに、常に同じ出力を生成する方法はいくつもあります。 たとえば、テキストファイルの特定のページを印刷システムが要求するたびに、同じ出力が生成されるようにするには、ページペインタで、ページごとのファイルポインタを格納して再使用するか、実際のページデータを格納するようにします。
次の例では、テキストファイルの「リスト表示」が印刷されています。 ファイルの名前は、main メソッドに引数として渡されています。 PrintListingPainter クラスは、レンダリングを要求された新しいページの開始位置で、使われているファイルポインタを保管します。 同じページをふたたびレンダリングするときは、ファイルポインタを記憶してある位置にリセットします。
import java.awt.*;
import java.awt.print.*;
import java.io.*;
public class PrintListing {
public static void main(String[] args) {
// Get a PrinterJob
PrinterJob job = PrinterJob.getPrinterJob();
// Ask user for page format (e.g., portrait/landscape)
PageFormat pf = job.pageDialog(job.defaultPage());
// Specify the Printable is an instance of
// PrintListingPainter; also provide given PageFormat
job.setPrintable(new PrintListingPainter(args[0]), pf);
// Print 1 copy
job.setCopies(1);
// Put up the dialog box
if (job.printDialog()) {
// Print the job if the user didn't cancel printing
try { job.print(); }
catch (Exception e) { /* handle exception */ }
}
System.exit(0);
}
}
class PrintListingPainter implements Printable {
private RandomAccessFile raf;
private String fileName;
private Font fnt = new Font("Helvetica", Font.PLAIN, 10);
private int rememberedPageIndex = -1;
private long rememberedFilePointer = -1;
private boolean rememberedEOF = false;
public PrintListingPainter(String file) {
fileName = file;
try {
// Open file
raf = new RandomAccessFile(file, "r");
}
catch (Exception e) { rememberedEOF = true; }
}
public int print(Graphics g, PageFormat pf, int pageIndex)
throws PrinterException {
try {
// For catching IOException
if (pageIndex != rememberedPageIndex) {
// First time we've visited this page
rememberedPageIndex = pageIndex;
// If encountered EOF on previous page, done
if (rememberedEOF) return Printable.NO_SUCH_PAGE;
// Save current position in input file
rememberedFilePointer = raf.getFilePointer();
}
else raf.seek(rememberedFilePointer);
g.setColor(Color.black);
g.setFont(fnt);
int x = (int) pf.getImageableX() + 10;
int y = (int) pf.getImageableY() + 12;
// Title line
g.drawString("File: " + fileName + ", page: " +
(pageIndex+1), x, y);
// Generate as many lines as will fit in imageable area
y += 36;
while (y + 12 < pf.getImageableY()+pf.getImageableHeight()){
String line = raf.readLine();
if (line == null){
rememberedEOF = true;
break;
}
g.drawString(line, x, y);
y += 12;
}
return Printable.PAGE_EXISTS;
}
catch (Exception e) { return Printable.NO_SUCH_PAGE; }
}
}