|
|
||
Sergey Groznyh's BlogSeptember 2007 ArchivesGenerating Print Preview of Swing Text ComponentsPosted by g_s_m on September 05, 2007 at 04:42 AM | Permalink | Comments (7)This question, “how to generate print preview of Java Swing
components,” is asked often on various discussion forums, because
currently (as of version 6) the Java SE doesn't have standard facilities for
building print previews. While working on the Swing Tutorial, I tried to address this issue under the topic “Using
There exist several resources on the Internet related to generating print previews in Java, but in most cases they are either commercial, or use an outdated API, or attempt to build some all-in-one custom dialog solution without paying much attention to details. In this issue I'll try to focus on how to create a print preview—what necessary parts are involved and how they do communicate. What to PrintThe Printable p = textComponent.getPrintable(); Page FormatNext thing we need to know is the parameters of the output media,
most important ones being the physical page dimensions. In real-life
situation the available page dimensions should be obtained by querying
some PageFormat f = new PageFormat(); int pageWidth = f.getWidth(); int pageHeight = f.getHeight(); Where to PrintThen we need a print destination that will receive the actual drawing commands. The destination should be an instance of
the
Image page = new BufferedImage(
pageWidth, pageHeight, BufferedImage.TYPE_INT_ARGB);
Graphics g = page.getGraphics();
How to PrintNow we are ready to generate page images. We need to repeatedly
invoke the
for (int n = 0; p.print(g, f, n) == Printable.PAGE_EXISTS; n++) {
// N-th page printed successfully
}
How to ScaleThe generated page image is usually too large for on-screen display, so we need to shrink the image for viewing. The following code creates an image with size reduced by two times on both dimensions.
double scaleFactor = 0.5;
int previewWidth = (int) (pageWidth * scaleFactor);
int previewHeight = (int) (pageHeight * scaleFactor);
Image preview = page.getScaledInstance(
previewWidth, previewHeight, SCALE_SMOOTH);
Back to SwingUntil now we dealt mostly with 2D and AWT parts of the Client Java API.
Now we need to place the generated preview image into some Swing
component. For this we'll create a subclass of the
JPanel previewPane = new JPanel() {
public void paintComponent(Graphics g) {
g.drawImage(preview, 0, 0, previewWidth, previewHeight, null);
}
public Dimension getPreferredSize() {
return new Dimension(previewWidth, previewHeight);
}
public Dimension getMinimumSize() {
return getPreferredSize();
}
public Dimension getMaximumSize() {
return getPreferredSize();
}
}
The resulting print preview pane can then be added to Swing container, just like any other Swing component. Below are links to the example containing the complete print preview implementation as described in this issue. Using this implementation, the print preview functionality could be added to any text component in a couple lines of code. Please note that in order to run this demo, you'll need Java runtime version 1.6 (aka 6.0) or higher. The demo displays a chapter from “Alice in Wonderland” in the
| ||
|
|