Class HardcopyWriter

java.lang.Object
java.io.Writer
jmri.util.davidflanagan.HardcopyWriter
All Implemented Interfaces:
Printable, Closeable, Flushable, Appendable, AutoCloseable
Direct Known Subclasses:
CompatibleHardcopyWriter

public class HardcopyWriter extends Writer implements Printable
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.

This has been extensively modified by Philip Gladstone to improve the print preview functionality and to switch to using the Java 1.8 (maybe 1.2) printing classes. The original code used the Java 1.1 printing classes.

  • Field Details

  • Constructor Details

    • HardcopyWriter

      public HardcopyWriter(Frame frame, String jobname, String fontName, Integer fontStyle, Integer fontsize, double leftmargin, double rightmargin, double topmargin, double bottommargin, boolean isPreview, String printerName, Boolean isLandscape, Boolean isPrintHeader, Attribute sides, Dimension pagesize) throws HardcopyWriter.PrintCanceledException
      Constructor for HardcopyWriter
      Parameters:
      frame - The AWT Frame
      jobname - The name to print in the title of the page
      fontName - The name of the font to use (if null, default is used)
      fontStyle - The style of the font to use (if null, default is used)
      fontsize - The size of the font to use (if null, default is used)
      leftmargin - The left margin in points
      rightmargin - The right margin in points
      topmargin - The top margin in points
      bottommargin - The bottom margin in points
      isPreview - Whether to preview the print job
      printerName - The name of the printer to use (if null, default is used)
      isLandscape - Whether to print in landscape mode (if null, default is used)
      isPrintHeader - Whether to print the header (if null, default is used)
      sides - The type of duplexing to use (if null, default is used)
      pagesize - The size of the page to use (if null, default is used)
      Throws:
      HardcopyWriter.PrintCanceledException - If the print job gets cancelled.
    • HardcopyWriter

      public HardcopyWriter(String fontName, Integer fontStyle, Integer fontsize, double leftmargin, double rightmargin, double topmargin, double bottommargin, Boolean isLandscape, Dimension pagesize) throws HardcopyWriter.PrintCanceledException
      Throws:
      HardcopyWriter.PrintCanceledException
  • Method Details

    • 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.

    • measure

      This function measures the size of the text in the current font. It returns a Rectangle2D.
      Parameters:
      s - The string to be measured (no tabs allowed)
      Returns:
      The Rectangle2D object in points
    • measure

      public Rectangle2D measure(Collection<String> stringList)
      This function returns the bounding box that includes all of the strings passed (when printed on top of each other)
      Parameters:
      stringList - A collection of Strings
      Returns:
      The Rectangle2D object in points
    • getPrintablePagesizePoints

      Get the current page size in points (logical units).
      Returns:
      The printable page area in points
    • setColumns

      public void setColumns(HardcopyWriter.Column[] columns)
      Function to set the columns for future text output. Output starts in first first column, and advances to the next column on a tab character. This either causes truncation of the text or wrapping to the next line depending on the column alignment type.

      If no columns are set, then the default is one column, left aligned with the full width of the page

      Parameters:
      columns - Array of Column objects
    • setColumns

      Function to set Columns based on a Collection<Column> object

      If no columns are set, then the default is one column, left aligned with the full width of the page

      Parameters:
      columns - Collection of Column objects
    • write

      public void write(char[] buffer, int index, int len)
      Send text to Writer output. Note that the text will be aligned to the current column (by default, this is one column, left aligned with the full width of the page)
      Specified by:
      write in class Writer
      Parameters:
      buffer - block of text characters
      index - position to start printing
      len - length (number of characters) of output
    • write

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

      Reset the text color back to the default after the string is written. This method is really only good for changing the color of a complete line (or column).

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

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

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

      public void dispose()
      Free up resources .

      Added so that a preview can be canceled.

    • setFont

      public void setFont(String name, Integer style, Integer size)
      Set the font to be used for the next write operation. This really only is good for the next line (or column) of output. Use with caution.

      If any of the parameters are null, the current value will be used.

      Parameters:
      name - the name of the font
      style - the style of the font
      size - the size of the font
    • getLineHeight

      public int getLineHeight()
      Get the height of a line of text. This is the amount that the vertical position will advance for each line.
      Returns:
      the height of a line of text
    • getFontSize

      public int getFontSize()
      Get the size of the font.
      Returns:
      the size of the font
    • getCharWidth

      public Float getCharWidth()
      Get the width of a character. This is only valid for monospaced fonts.
      Returns:
      the width of a character, or null if the font is not monospaced
    • getLineAscent

      public int getLineAscent()
      Get the ascent of the font. This is the distance from the baseline to the top of the font.
      Returns:
      the ascent of the font
    • setTextColor

      public void setTextColor(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

      Return the number of columns of characters that fit on a page.
      Returns:
      the number of characters in a line or null if the font is not monospaced
    • ensureVerticalSpace

      public void ensureVerticalSpace(int points)
      This ensures that the required amount of vertical space is available. If not, a page break is inserted.
      Parameters:
      points - The amount of vertical space to ensure in points.
    • leaveVerticalSpace

      public void leaveVerticalSpace(int points)
      This leaves the required amount of vertical space. If not enough space is available, a page break is inserted.
      Parameters:
      points - The amount of vertical space to leave in points.
    • newline

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

      protected void ensureOnPage()
      Ensure that we have a page object. The page is null when we are before the first page, or between pages.
    • getPageImages

      Gets all the pages as Images.
      Returns:
      the current page as a BufferedImage
    • getPageNum

      public int getPageNum()
      Gets the current page num -- this can be used to determine when a page break has happened. The page number may be increased whenever a newline is printed.
      Returns:
      the current page number
    • write

      public void write(Image c, 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. Modified by P Gladstone. The image well be rendered at 1.5 pixels per point.

      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
    • writeDecoderProIcon

      public Dimension writeDecoderProIcon(boolean no_advance)
      Write the decoder pro icon to the output. Method added by P Gladstone. This actually uses the high resolution image. It also advances the v_pos appropriately (unless no_advance is True)

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

      Parameters:
      no_advance - if true, do not advance the v_pos
      Returns:
      The actual size in points of the icon that was rendered.
    • writeDecoderProIcon

      Write the decoder pro icon to the output. Method added by P Gladstone. This actually uses the high resolution image. It also advances the v_pos appropriately.

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

      Returns:
      The actual size in points of the icon that was rendered.
    • writeSpecificSize

      Write an image to the output at the current position, scaled to the required size. More importantly, it does not save the image object in memory, but will re-read it from the disk when it needs to be used. This is needed if there are a lot of large images in a printout.
      Parameters:
      icon - The ImageIconWrapper object to display
      requiredSize - The size in points to display the image
      Returns:
      The actual size rendered
    • writeSpecificSize

      public Dimension writeSpecificSize(Image c, Dimension requiredSize)
      Write a graphic to the printout at a specific size (in points)

      This was not in the original class, but was added afterwards by Kevin Dickerson. Heavily modified by P Gladstone. If the image is large and there are many images in the printout, then it probably makes sense to pass in a HardcopyWriter.ImageIconWrapper object instead. This will save memory as it just retains the filename until time comes to actually render the image.

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

      Parameters:
      c - the image to print
      requiredSize - the dimensions (in points) to scale the image to. The image will fit inside the bounding box.
      Returns:
      the dimensions of the image in points
    • write

      public void write(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.

      I'm not convinced that this is actually used as the code that invokes it is under a test for Java version before 1.5.

      Parameters:
      jW - the window to print
    • writeLine

      public void writeLine(int vStart, int hStart, int vEnd, int hEnd)
      Draw a line on the printout. Calls to this should be replaced by calls to `writeExactLine` which doesn't offset the line by strange amounts.

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

      hStart and hEnd represent the horizontal point positions. The lines actually start in the middle of the character position (to the left) to make it easy to draw vertical lines and space them between printed characters. This is (unfortunately) bad for getting something on the right margin.

      vStart and vEnd represent the vertical point positions. Horizontal lines are drawn underneath below the vStart position. 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. If vStart is set to the current vPos, then the line is under the current row of text.

      Parameters:
      vStart - vertical starting position
      hStart - horizontal starting position
      vEnd - vertical ending position
      hEnd - horizontal ending position
    • writeExactLine

      public void writeExactLine(int vStart, int hStart, int vEnd, int hEnd)
      Draw a line on the printout.

      This was not in the original class, but was added afterwards by Philip Gladstone.

      hStart and hEnd represent the horizontal point positions.

      vStart and vEnd represent the vertical point positions.

      Parameters:
      vStart - vertical starting position
      hStart - horizontal starting position
      vEnd - vertical ending position
      hEnd - horizontal ending position
    • getCurrentVPos

      public int getCurrentVPos()
      Get the current vertical position on the page
      Returns:
      the current vertical position of the base of the current line on the page (in points)
    • writeBorders

      public void writeBorders()
      Print vertical borders on the current line at the left and right sides of the page at pixel positions 0 and width. Border lines are one text line in height. ISSUE: Where should these lines be drawn?

      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
    • isMonospaced

      public boolean isMonospaced()
      Returns true if the current font is monospaced
      Returns:
      true if the current font is monospaced.
    • getPageCommands

      Get the list of commands for the whole document.
      Returns:
      the list of commands for the document
    • print

      public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException
      Replay the recorded commands to the graphics context. This is called by the PrinterJob.
      Specified by:
      print in interface Printable
      Throws:
      PrinterException