C
Cindy Huyser
I'm pretty much a newbie to Java printing. An application I'm writing
has a number of JFrame-descended objects, which I pass to the
constructor of ComponentPrintable, shown below. Each JFrame contains at
least one JPanel descendant, and inside the JPanels a number of JPanels,
JButtons and JLabels.
My problem is that although all the components I've built show up
without trouble in the GUI, some of the JLabels print as truncated
strings (followed by ...). This is happens with the labels on some
JButtons as well as those embedded in a JPanel.
I've tried setting using FontMetrics to get a size for the labels, then
giving the labels some extra padding, but when 2 labels are in an
adjacent JPanel (governed by FlowLayout), only the first label comes out
intact. No preferred size is set for these JPanels. The assemblage of
JPanels is governed by GridBagLayout. I've also tried various
combinations of KEY_ANTIALIAS, KEY_TEXT_ANTIALIAS, and
KEY_FRACTIONALMETRICS settings, as well as replacing FlowLayouts with
GridBagLayouts, to no avail.
Any ideas? My gut feeling is there's some sort of translation problem
between the screen resolution and the printer resolution, though I've no
idea how to address it if that's the case. I'm using JDK 1.4.1.
Thanks in advance,
Cindy Huyser
import java.awt.*;
import java.awt.print.*;
import javax.swing.JComponent;
import java.awt.image.*;
import java.awt.color.*;
import java.util.HashMap;
public class ComponentPrintable implements Printable
{
private Component component;
/** Creates a new instance of ComponentPrintable */
public ComponentPrintable(Component c)
{
component = c;
}
public int print(Graphics g, PageFormat pageFormat, int pageIndex)
{
if (pageIndex > 0) return NO_SUCH_PAGE;
Graphics2D g2 = (Graphics2D)g;
GraphicsConfiguration gc = g2.getDeviceConfiguration();
Dimension compSize = component.getSize();
g2.translate(pageFormat.getImageableX(),
pageFormat.getImageableY());
// Scale to width
double xScale =
pageFormat.getImageableWidth()/compSize.getWidth();
if(xScale < 1)
{
g2.scale(xScale, xScale);
}
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_OFF);
component.print(g2);
return PAGE_EXISTS;
}
}
has a number of JFrame-descended objects, which I pass to the
constructor of ComponentPrintable, shown below. Each JFrame contains at
least one JPanel descendant, and inside the JPanels a number of JPanels,
JButtons and JLabels.
My problem is that although all the components I've built show up
without trouble in the GUI, some of the JLabels print as truncated
strings (followed by ...). This is happens with the labels on some
JButtons as well as those embedded in a JPanel.
I've tried setting using FontMetrics to get a size for the labels, then
giving the labels some extra padding, but when 2 labels are in an
adjacent JPanel (governed by FlowLayout), only the first label comes out
intact. No preferred size is set for these JPanels. The assemblage of
JPanels is governed by GridBagLayout. I've also tried various
combinations of KEY_ANTIALIAS, KEY_TEXT_ANTIALIAS, and
KEY_FRACTIONALMETRICS settings, as well as replacing FlowLayouts with
GridBagLayouts, to no avail.
Any ideas? My gut feeling is there's some sort of translation problem
between the screen resolution and the printer resolution, though I've no
idea how to address it if that's the case. I'm using JDK 1.4.1.
Thanks in advance,
Cindy Huyser
import java.awt.*;
import java.awt.print.*;
import javax.swing.JComponent;
import java.awt.image.*;
import java.awt.color.*;
import java.util.HashMap;
public class ComponentPrintable implements Printable
{
private Component component;
/** Creates a new instance of ComponentPrintable */
public ComponentPrintable(Component c)
{
component = c;
}
public int print(Graphics g, PageFormat pageFormat, int pageIndex)
{
if (pageIndex > 0) return NO_SUCH_PAGE;
Graphics2D g2 = (Graphics2D)g;
GraphicsConfiguration gc = g2.getDeviceConfiguration();
Dimension compSize = component.getSize();
g2.translate(pageFormat.getImageableX(),
pageFormat.getImageableY());
// Scale to width
double xScale =
pageFormat.getImageableWidth()/compSize.getWidth();
if(xScale < 1)
{
g2.scale(xScale, xScale);
}
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_OFF);
component.print(g2);
return PAGE_EXISTS;
}
}