Class HardcopyWriter
- All Implemented Interfaces:
Printable,Closeable,Flushable,Appendable,AutoCloseable
- Direct Known Subclasses:
CompatibleHardcopyWriter
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumEnum to represent the alignment of text in a column.static classClass to represent a column in the output.static classprotected static classprotected static classprotected static classprotected static classstatic classstatic classprotected static interfaceprotected static classprotected static classprotected static class -
Field Summary
FieldsModifier and TypeFieldDescription(package private) PrintRequestAttributeSetprotected floatprotected JButtonprotected Colorprotected intprotected HardcopyWriter.Column[]protected List<HardcopyWriter.PrintCommand>protected Fontprotected Frameprotected Fontprotected FontMetricsprotected intprotected intprotected booleanprotected booleanprotected Stringprotected doubleprotected Stringprotected intprotected intprotected intprotected FontMetricsprotected JButtonprotected Graphicsprotected List<List<HardcopyWriter.PrintCommand>>protected JLabelprotected PageFormatprotected intprotected Dimensionprotected Dimensionprotected doubleprotected JmriJFrameprotected ImageIconprotected Imageprotected JLabelprotected JPanelprotected JToolBarprotected JButtonprotected PrinterJobprotected booleanprotected Graphicsprotected Integerprotected booleanprotected Stringprotected doubleprotected Stringprotected intprotected intprotected intprotected intprotected intprotected intprotected intFields inherited from interface java.awt.print.Printable
NO_SUCH_PAGE, PAGE_EXISTS -
Constructor Summary
ConstructorsConstructorDescriptionHardcopyWriter(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) Constructor for HardcopyWriterHardcopyWriter(String fontName, Integer fontStyle, Integer fontsize, double leftmargin, double rightmargin, double topmargin, double bottommargin, Boolean isLandscape, Dimension pagesize) -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Handle close event of pane.protected voidDisplay a page image in the preview pane.voiddispose()Free up resources .protected voidEnsure that we have a page object.voidensureVerticalSpace(int points) This ensures that the required amount of vertical space is available.voidflush()Return the number of columns of characters that fit on a page.Get the width of a character.intGet the current vertical position on the pageintGet the size of the font.intGet the ascent of the font.intGet the height of a line of text.Get the list of commands for the whole document.Gets all the pages as Images.intGets the current page num -- this can be used to determine when a page break has happened.Get the current page size in points (logical units).voidincreaseLineSpacing(int percent) Increase line spacing by a percentagebooleanReturns true if the current font is monospacedvoidleaveVerticalSpace(int points) This leaves the required amount of vertical space.This function measures the size of the text in the current font.measure(Collection<String> stringList) This function returns the bounding box that includes all of the strings passed (when printed on top of each other)protected voidnewline()Internal method begins a new line method modified by Dennis Miller to add preview capabilityvoidEnd the current page.intprint(Graphics g, PageFormat pf, int pageIndex) Replay the recorded commands to the graphics context.voidsetColumns(Collection<HardcopyWriter.Column> columns) Function to set Columns based on aCollection<Column>objectvoidsetColumns(HardcopyWriter.Column[] columns) Function to set the columns for future text output.voidSet the font to be used for the next write operation.voidsets the default text colorprotected voidCreate a print preview toolbar.voidwrite(char[] buffer, int index, int len) Send text to Writer output.voidWrite a given String with the desired color.voidWrite a graphic to the printout.voidA Method to allow a JWindow to print itself at the current line positionvoidPrint vertical borders on the current line at the left and right sides of the page at pixel positions 0 and width.Write the decoder pro icon to the output.writeDecoderProIcon(boolean no_advance) Write the decoder pro icon to the output.voidwriteExactLine(int vStart, int hStart, int vEnd, int hEnd) Draw a line on the printout.voidwriteLine(int vStart, int hStart, int vEnd, int hEnd) Draw a line on the printout.writeSpecificSize(Image c, Dimension requiredSize) Write a graphic to the printout at a specific size (in points)writeSpecificSize(HardcopyWriter.ImageIconWrapper icon, Dimension requiredSize) Write an image to the output at the current position, scaled to the required size.
-
Field Details
-
printerJob
-
pageFormat
-
printJobGraphics
-
page
-
jobname
-
line
-
useFontSize
-
time
-
pagesizePixels
-
pagesizePoints
-
font
-
headerfont
-
useFontName
-
isMonospacedFont
-
useFontStyle
-
metrics
-
headermetrics
-
x0
-
y0
-
height
-
width
-
width_including_right_margin
-
headery
-
titleTop
-
leftMargin
-
charwidth
-
lineheight
-
lineascent
-
v_pos
-
max_v_pos
-
pagenum
-
color
-
printHeader
-
isPreview
-
previewImage
-
pageImages
-
previewFrame
-
previewPanel
-
previewIcon
-
previewLabel
-
previewToolBar
-
frame
-
nextButton
-
previousButton
-
closeButton
-
pageCount
-
showPreview
-
columns
-
columnIndex
-
pixelScale
-
screenResolution
-
pageCommands
-
currentPageCommands
-
attributes
-
-
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 Framejobname- The name to print in the title of the pagefontName- 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 pointsrightmargin- The right margin in pointstopmargin- The top margin in pointsbottommargin- The bottom margin in pointsisPreview- Whether to preview the print jobprinterName- 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
-
-
Method Details
-
toolBarInit
Create a print preview toolbar. -
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
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
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 aCollection<Column>objectIf 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
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) -
write
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 Strings- the String- Throws:
IOException- if unable to write to printer
-
flush
-
close
Handle close event of pane. Modified to clean up the added preview capability. -
dispose
Free up resources .Added so that a preview can be canceled.
-
setFont
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 fontstyle- the style of the fontsize- the size of the font
-
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
Get the size of the font.- Returns:
- the size of the font
-
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
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
sets the default text color- Parameters:
c- the new default text color
-
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
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
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
Internal method begins a new line method modified by Dennis Miller to add preview capability -
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
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
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 writei- ignored, but maintained for API compatibility
-
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 (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 displayrequiredSize- The size in points to display the image- Returns:
- The actual size rendered
-
writeSpecificSize
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 printrequiredSize- 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
A Method to allow a JWindow to print itself at the current line positionThis 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
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 positionhStart- horizontal starting positionvEnd- vertical ending positionhEnd- horizontal ending position
-
writeExactLine
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 positionhStart- horizontal starting positionvEnd- vertical ending positionhEnd- horizontal ending position
-
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
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
Increase line spacing by a percentageThis 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
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
Replay the recorded commands to the graphics context. This is called by the PrinterJob.- Specified by:
printin interfacePrintable- Throws:
PrinterException
-