FileDocCategorySizeDatePackage
FilePageRenderer.javaAPI DocExample3426Mon Apr 05 12:03:02 BST 1999None

FilePageRenderer

public class FilePageRenderer extends JComponent implements Printable

Fields Summary
private int
mCurrentPage
private Vector
mLines
private Vector
mPages
private Font
mFont
private int
mFontSize
private Dimension
mPreferredSize
Constructors Summary
public FilePageRenderer(File file, PageFormat pageFormat)

    mFontSize = 12;
    mFont = new Font("Serif", Font.PLAIN, mFontSize);
    // Open the file.
    BufferedReader in = new BufferedReader(
        new FileReader(file));
    // Read all the lines.
    String line;
    mLines = new Vector();
    while ((line = in.readLine()) != null)
      mLines.addElement(line);
    // Clean up.
    in.close();
    // Now paginate, based on the PageFormat.
    paginate(pageFormat);
  
Methods Summary
public intgetCurrentPage()

 return mCurrentPage; 
public intgetNumPages()

 return mPages.size(); 
public java.awt.DimensiongetPreferredSize()

 return mPreferredSize; 
public voidnextPage()

    if (mCurrentPage < mPages.size() - 1)
      mCurrentPage++;
    repaint();
  
public voidpaginate(java.awt.print.PageFormat pageFormat)

    mCurrentPage = 0;
    mPages = new Vector();
    float y = mFontSize;
    Vector page = new Vector();
    for (int i = 0; i < mLines.size(); i++) {
      String line = (String)mLines.elementAt(i);
      page.addElement(line);
      y += mFontSize;
      if (y + mFontSize * 2 > pageFormat.getImageableHeight()) {
        y = 0;
        mPages.addElement(page);
        page = new Vector();
      }
    }
    // Add the last page.
    if (page.size() > 0) mPages.addElement(page);
    // Set our preferred size based on the PageFormat.
    mPreferredSize = new Dimension((int)pageFormat.getImageableWidth(),
        (int)pageFormat.getImageableHeight());
    repaint();
  
public voidpaintComponent(java.awt.Graphics g)

    Graphics2D g2 = (Graphics2D)g;
    // Make the background white.
    java.awt.geom.Rectangle2D r = new java.awt.geom.Rectangle2D.Float(0, 0,
        mPreferredSize.width, mPreferredSize.height);
    g2.setPaint(Color.white);
    g2.fill(r);
    // Get the current page.
    Vector page = (Vector)mPages.elementAt(mCurrentPage);
    // Draw all the lines for this page.
    g2.setFont(mFont);
    g2.setPaint(Color.black);
    float x = 0;
    float y = mFontSize;
    for (int i = 0; i < page.size(); i++) {
      String line = (String)page.elementAt(i);
      if (line.length() > 0) g2.drawString(line, (int)x, (int)y);
      y += mFontSize;
    }
  
public voidpreviousPage()

    if (mCurrentPage > 0)
      mCurrentPage--;
    repaint();
  
public intprint(java.awt.Graphics g, java.awt.print.PageFormat pageFormat, int pageIndex)

    if (pageIndex >= mPages.size()) return NO_SUCH_PAGE;
    int savedPage = mCurrentPage;
    mCurrentPage = pageIndex;
    Graphics2D g2 = (Graphics2D)g;
    g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
    paint(g2);
    mCurrentPage = savedPage;
    return PAGE_EXISTS;