Article View: borland.public.jbuilder.java.language
Article #14911SV: Can I send some objetcs to the printer???
From: Johan Forsberg
Date: Wed, 28 Jul 1999 00:00
Date: Wed, 28 Jul 1999 00:00
126 lines
5121 bytes
5121 bytes
Vinicius Assuncao <vlassuncao@starmedia.com> skrev i
diskussionsgruppsmeddelandet:7ncpab$rjb4@forums.borland.com...
> Hi,
>
> Can I send some objects (like a panel) to the printer???
Yes. If you are using Java2 and Swing, I have made the following
ComponentPrinter.
Call with ComponentPrinter.print(myJComponent).
Note: The component must not contain any images or gradient fills!
(Well, it will still print, but it will be very low rez, and extremely
slow).
class HardPrintable implements Printable {
Component comp;
int i=0;
AffineTransform trans;
public HardPrintable(Component c) {
Component frame;
comp=c;
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(this);
if (printJob.printDialog()) {
for (frame=c; frame.getParent() != null;
frame=frame.getParent());
try {
debuffer(comp);
printJob.print();
rebuffer(comp);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
// Most of the complication in the print() merthod is because I
scale/rotate the
// component to fill the page.
public int print(Graphics g0, PageFormat pf, int pi) throws
PrinterException {
if (pi >= 1) {
return Printable.NO_SUCH_PAGE;
}
if (!(g0 instanceof Graphics2D)) {
g0.setColor(Color.black);
g0.drawString("Jag beh๏ฟฝver Graphics2D!",
(int)(pf.getImageableX())+10,(int)(pf.getImageableY()+20));
return Printable.PAGE_EXISTS;
}
Graphics2D g=(Graphics2D)g0;
Rectangle b=comp.getBounds();
int w=(int)pf.getImageableWidth(), h=(int)pf.getImageableHeight();
// Buffer the transform, as print() is called several times per page
if (trans==null) {
trans=new AffineTransform();
trans.translate(pf.getImageableX(), pf.getImageableY());
GregorianCalendar c=new GregorianCalendar();
if (c.get(c.DAY_OF_MONTH)==13 && c.get(c.DAY_OF_WEEK)==c.FRIDAY
&& Math.random()<0.1) {
trans.rotate(Math.random()*Math.PI*2.0, w/2.0, h/2.0);
} else if (b.width > b.height) {
// A bug forces us to do the following manually
trans.translate(w/2.0, h/2.0);
trans.rotate(-Math.PI/2);
trans.translate(-h/2.0, -w/2.0);
pf.setOrientation(PageFormat.LANDSCAPE);
w=(int)pf.getImageableWidth();
h=(int)pf.getImageableHeight();
} else
pf.setOrientation(PageFormat.PORTRAIT);
double scale=((double)w)/b.width;
if (scale*b.height > h)
scale=((double)h)/b.height;
trans.scale(scale, scale);
}
g.transform(trans);
comp.paint(g);
return Printable.PAGE_EXISTS;
}
Vector wasBuffered=new Vector();
// As I mentioned, the component must not contain images. However,
double buffered
// components are esentially one big image. Therefore we must turn of
all double buffering.
private void debuffer(Component c) {
if (c instanceof JComponent) {
JComponent jc=(JComponent) c;
if (jc.isDoubleBuffered()) {
wasBuffered.add(jc);
jc.setDoubleBuffered(false);
}
}
if (c instanceof Container) {
Container cont=(Container) c;
for (int i=0; i<cont.getComponentCount(); i++)
debuffer(cont.getComponent(i));
}
}
private void rebuffer(Component c) {
for (int i=0; i<wasBuffered.size(); i++)
((JComponent) wasBuffered.elementAt(i)).setDoubleBuffered(true);
}
}
// I didn't include the other printables...
public class ComponentPrinter {
public static void print(Component c) { hardcopy(c); }
public static void hardcopy(Component c) { new HardPrintable(c); }
public static void softcopy(Component c) { new SoftPrintable(c); }
public static void rastercopy(Component c) { new PreviewPrintable(c); }
public static void preview(Component c) {
JFrame w=new JFrame("Test");
JLabel jl=new JLabel(new ImageIcon(PreviewPrintable.makeImage(c,
468, 648)));
w.getContentPane().add(jl, BorderLayout.CENTER);
w.pack();
w.setVisible(true);
}
}
Technical Details
Message-ID:
<7nmbaj$73r2@forums.borland.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<7ncpab$rjb4@forums.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII X-Google-Thread: f74b3,e2c951bf85eeb386 X-Google-Attributes: gidf74b3,public From: "Johan Forsberg" <jfo@stmf.se> Subject: SV: Can I send some objetcs to the printer??? Date: 1999/07/28 Message-ID: <7nmbaj$73r2@forums.borland.com> X-Deja-AN: 506088073 References: <7ncpab$rjb4@forums.borland.com> X-Priority: 3 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2014.211 Organization: Another Netscape Collabra Server User X-MSMail-Priority: Normal Newsgroups: borland.public.jbuilder.java.language