Class HardcopyWriter

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.Appendable, java.lang.AutoCloseable

    public class HardcopyWriter
    extends java.io.Writer
    Provide graphic output to a screen/printer.

    This is from Chapter 12 of the O'Reilly Java book by David Flanagan with the alligator on the front.

    • Constructor Summary

      Constructors 
      Constructor Description
      HardcopyWriter​(java.awt.Frame frame, java.lang.String jobname, int fontsize, double leftmargin, double rightmargin, double topmargin, double bottommargin, boolean preview)  
      HardcopyWriter​(java.awt.Frame frame, java.lang.String jobname, int fontsize, double leftmargin, double rightmargin, double topmargin, double bottommargin, boolean preview, java.lang.String printerName, boolean landscape, boolean printHeader, java.awt.Dimension pagesize)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Handle close event of pane.
      protected void displayPage()
      Display a page image in the preview pane.
      void dispose()
      Free up resources .
      void flush()  
      int getCharactersPerLine()
      Return the number of columns of characters that fit on a page.
      int getCharWidth()  
      int getCurrentLineNumber()
      Get the current linenumber.
      int getFontSize()  
      int getLineAscent()  
      int getLineHeight()  
      int getLinesPerPage()
      Return the number of lines that fit on a page.
      void increaseLineSpacing​(int percent)
      Increase line spacing by a percentage
      protected void newline()
      Internal method begins a new line method modified by Dennis Miller to add preview capability
      protected void newpage()
      Internal method beings a new page and prints the header method modified by Dennis Miller to add preview capability
      void pageBreak()
      End the current page.
      void setFontName​(java.lang.String name)  
      void setFontStyle​(int style)  
      void setTextColor​(java.awt.Color c)
      sets the default text color
      protected void toolBarInit()
      Create a print preview toolbar.
      void write​(char[] buffer, int index, int len)
      Send text to Writer output.
      void write​(int rowStart, int colStart, int rowEnd, int colEnd)
      Draw a line on the printout.
      void write​(java.awt.Color c, java.lang.String s)
      Write a given String with the desired color.
      void write​(java.awt.Image c, java.awt.Component i)
      Write a graphic to the printout.
      void write​(javax.swing.JWindow jW)
      A Method to allow a JWindow to print itself at the current line position
      void writeBorders()
      Print vertical borders on the current line at the left and right sides of the page at character positions 0 and chars_per_line + 1.
      void writeNoScale​(java.awt.Image c, java.awt.Component i)
      Write a graphic to the printout.
      • Methods inherited from class java.io.Writer

        append, append, append, nullWriter, write, write, write, write
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • toolBarInit

        protected void toolBarInit()
        Create a print preview toolbar.
      • displayPage

        protected void displayPage()
        Display a page image in the preview pane.

        Not part of the original HardcopyWriter class.

      • write

        public void write​(char[] buffer,
                          int index,
                          int len)
        Send text to Writer output.
        Specified by:
        write in class java.io.Writer
        Parameters:
        buffer - block of text characters
        index - position to start printing
        len - length (number of characters) of output
      • write

        public void write​(java.awt.Color c,
                          java.lang.String s)
                   throws java.io.IOException
        Write a given String with the desired color.

        Reset the text color back to the default after the string is written.

        Parameters:
        c - the color desired for this String
        s - the String
        Throws:
        java.io.IOException - if unable to write to printer
      • flush

        public void flush()
        Specified by:
        flush in interface java.io.Flushable
        Specified by:
        flush in class java.io.Writer
      • close

        public void close()
        Handle close event of pane. Modified to clean up the added preview capability.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Specified by:
        close in class java.io.Writer
      • dispose

        public void dispose()
        Free up resources .

        Added so that a preview can be canceled.

      • setFontName

        public void setFontName​(java.lang.String name)
      • setTextColor

        public void setTextColor​(java.awt.Color c)
        sets the default text color
        Parameters:
        c - the new default text color
      • pageBreak

        public void pageBreak()
        End the current page. Subsequent output will be on a new page
      • getCharactersPerLine

        public int getCharactersPerLine()
        Return the number of columns of characters that fit on a page.
        Returns:
        the number of characters in a line
      • getLinesPerPage

        public int getLinesPerPage()
        Return the number of lines that fit on a page.
        Returns:
        the number of lines in a page
      • newline

        protected void newline()
        Internal method begins a new line method modified by Dennis Miller to add preview capability
      • newpage

        protected void newpage()
        Internal method beings a new page and prints the header method modified by Dennis Miller to add preview capability
      • write

        public void write​(java.awt.Image c,
                          java.awt.Component i)
        Write a graphic to the printout.

        This was not in the original class, but was added afterwards by Bob Jacobsen. Modified by D Miller.

        The image is positioned on the right side of the paper, at the current height.

        Parameters:
        c - image to write
        i - ignored, but maintained for API compatibility
      • writeNoScale

        public void writeNoScale​(java.awt.Image c,
                                 java.awt.Component i)
        Write a graphic to the printout.

        This was not in the original class, but was added afterwards by Kevin Dickerson. it is a copy of the write, but without the scaling.

        The image is positioned on the right side of the paper, at the current height.

        Parameters:
        c - the image to print
        i - ignored but maintained for API compatibility
      • write

        public void write​(javax.swing.JWindow jW)
        A Method to allow a JWindow to print itself at the current line position

        This was not in the original class, but was added afterwards by Dennis Miller.

        Intended to allow for a graphic printout of the speed table, but can be used to print any window. The JWindow is passed to the method and prints itself at the current line and aligned at the left margin. The calling method should check for sufficient space left on the page and move it to the top of the next page if there isn't enough space.

        Parameters:
        jW - the window to print
      • write

        public void write​(int rowStart,
                          int colStart,
                          int rowEnd,
                          int colEnd)
        Draw a line on the printout.

        This was not in the original class, but was added afterwards by Dennis Miller.

        colStart and colEnd represent the horizontal character positions. The lines actually start in the middle of the character position to make it easy to draw vertical lines and space them between printed characters.

        rowStart and rowEnd represent the vertical character positions. Horizontal lines are drawn underneath the row (line) number. They are offset so they appear evenly spaced, although they don't take into account any space needed for descenders, so they look best with all caps text

        Parameters:
        rowStart - vertical starting position
        colStart - horizontal starting position
        rowEnd - vertical ending position
        colEnd - horizontal ending position
      • getCurrentLineNumber

        public int getCurrentLineNumber()
        Get the current linenumber.

        This was not in the original class, but was added afterwards by Dennis Miller.

        Returns:
        the line number within the page
      • writeBorders

        public void writeBorders()
        Print vertical borders on the current line at the left and right sides of the page at character positions 0 and chars_per_line + 1. Border lines are one text line in height

        This was not in the original class, but was added afterwards by Dennis Miller.

      • increaseLineSpacing

        public void increaseLineSpacing​(int percent)
        Increase line spacing by a percentage

        This method should be invoked immediately after a new HardcopyWriter is created.

        This method was added to improve appearance when printing tables

        This was not in the original class, added afterwards by DaveDuchamp.

        Parameters:
        percent - percentage by which to increase line spacing