1 33 34 package edu.rice.cs.drjava.model.print; 35 36 import java.util.*; 37 import java.awt.print.*; 38 import java.awt.*; 39 import java.awt.font.*; 40 41 47 public class PagePrinter implements Printable { 48 49 private ArrayList<TextLayout> _textLayouts; 51 private ArrayList<TextLayout> _lineNumbers; 52 private String _fileName; 53 private DrJavaBook _parent; 54 55 59 public PagePrinter(int page, String fileName, DrJavaBook parent) { 60 _textLayouts = new ArrayList<TextLayout>(); 62 _lineNumbers = new ArrayList<TextLayout>(); 63 _fileName = fileName; 64 _parent = parent; 65 } 66 67 75 public void add(TextLayout text, TextLayout lineNumber) { 76 _textLayouts.add(text); 77 _lineNumbers.add(lineNumber); 78 } 79 80 87 public int print(Graphics graphics, PageFormat format, int pageIndex) { 88 Graphics2D g2d = (Graphics2D) graphics; 90 g2d.translate(format.getImageableX(), format.getImageableY()); 91 g2d.setPaint(Color.black); 92 93 float y = 0; 94 95 for (int i=0; i<_textLayouts.size(); i++) { 97 TextLayout layout = _textLayouts.get(i); 98 TextLayout lineNumber = _lineNumbers.get(i); 99 100 y += layout.getAscent(); 101 lineNumber.draw(g2d, 0, y); 102 layout.draw(g2d, _parent.LINE_NUM_WIDTH, y); 103 y += layout.getLeading(); 104 } 105 106 printFooter(g2d, format, pageIndex + 1); 108 109 return PAGE_EXISTS; 110 } 111 112 118 private void printFooter(Graphics2D g2d, PageFormat format, int page) { 119 TextLayout footerFile = new TextLayout(_fileName, DrJavaBook.FOOTER_FONT, g2d.getFontRenderContext()); 120 float footerPlace = (float) (format.getImageableWidth() - footerFile.getAdvance()) / 2; 121 122 footerFile.draw(g2d, footerPlace, (float) format.getImageableHeight() - footerFile.getDescent()); 123 124 TextLayout footerPageNo = new TextLayout(page + "", DrJavaBook.FOOTER_FONT, g2d.getFontRenderContext()); 125 footerPageNo.draw(g2d, 126 (float) format.getImageableWidth() - footerPageNo.getAdvance(), 127 (float) format.getImageableHeight() - footerPageNo.getDescent()); 128 } 129 130 } 131 | Popular Tags |