ContainerPrintable
Aus Byte-Welt Wiki
Version vom 30. März 2009, 17:53 Uhr von AndreUhres (Diskussion | Beiträge) (Die Seite wurde neu angelegt: „<code=java> /* * ContainerPrintable.java * We can pass the container to be printed in the constructor. * The printing is done in segments of height 10. * We c...“)
<code=java> /*
* ContainerPrintable.java * We can pass the container to be printed in the constructor. * The printing is done in segments of height 10. * We can adapt the segment height using method setSegmentHeight(..) */
import java.awt.*; import java.awt.print.*; import java.awt.geom.*;
public class ContainerPrintable implements Printable {
private Container container;//The container to print private int segmentHeight = 10;//The container's segment height private int totalWidth;//To save total width. private int last = -1;//The most recent page index asked to print private int segment = 0;//The next segment to print private final Rectangle clip = new Rectangle(0, 0, 0, 0);//To store an area to be printed private final Rectangle tempRect = new Rectangle(0, 0, 0, 0);
public ContainerPrintable(final Container container) { this.container = container; container.setBackground(Color.WHITE); totalWidth = container.getWidth(); }
public int print(final Graphics graphics, final PageFormat pageFormat, final int pageIndex) throws PrinterException { final int imgWidth = (int) pageFormat.getImageableWidth(); final int imgHeight = (int) pageFormat.getImageableHeight(); if (imgWidth <= 0) { throw new PrinterException("Width of printable area is too small."); } // the amount of vertical space available for printing the container int availableSpace = imgHeight; if (availableSpace <= 0) { throw new PrinterException("Height of printable area is too small."); } // we need a scale factor to fit the container's entire width on the page double scaleFactor = 1.0D; if (totalWidth > imgWidth) { scaleFactor = (double) imgWidth / (double) totalWidth; } while (last < pageIndex) { if (segment >= getSegmentCount()) {// if we are finished with all segments return NO_SUCH_PAGE; } int scaledHeight = (int) (availableSpace / scaleFactor); // calculate the area of the container to be printed for this page nextClip(scaledHeight); last++; } // translate into the coordinate system of the pageFormat Graphics2D g2d = (Graphics2D) graphics; g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); // constrain the container output to the available space tempRect.x = 0; tempRect.y = 0; tempRect.width = imgWidth; tempRect.height = availableSpace; g2d.clip(tempRect); // if we have a scale factor, scale the graphics object to fit the entire width if (scaleFactor != 1.0D) { g2d.scale(scaleFactor, scaleFactor); // otherwise, ensure that the current portion of the container is centered horizontally } else { int diff = (imgWidth - clip.width) / 2; g2d.translate(diff, 0); } // store the old transform and clip for later restoration AffineTransform oldTrans = g2d.getTransform(); Shape oldClip = g2d.getClip(); // print the current section of the container g2d.translate(-clip.x, -clip.y); g2d.clip(clip); container.print(g2d); // restore the original transform and clip g2d.setTransform(oldTrans); g2d.setClip(oldClip); return PAGE_EXISTS; }
private int getSegmentCount() { return container.getHeight() / segmentHeight; }
public void setSegmentHeight(final int segmentHeight) { this.segmentHeight = segmentHeight; }
public int getSegmentHeight() { return segmentHeight; }
private void nextClip(final int pageHeight) { clip.x = 0; clip.y += clip.height;// adjust clip to the top of the next set of segments clip.height = 0;// adjust clip height to be zero // fit as many segments as possible, and at least one int segmentCount = getSegmentCount(); do { clip.height += segmentHeight; if (++segment >= segmentCount) { break; } } while (clip.height + segmentHeight <= pageHeight); clip.width = totalWidth; }
} </code=java>