Joseph Lee
Posts: 10
Nickname: at9483mph
Registered: Jan, 2004
|
|
Re: Print a image(bitmap or JPEG)
|
Posted: Feb 20, 2004 11:19 PM
|
|
Dear,
This is the java coding i used to print a component, but I cannot use it to print a image.
Actually I want to print a component(JPanel), but the printing is out of margin and I do not know how to justify it, thus I try to convert my contains in that particular component to a image(the image can capture/get all the contains boundaries without problem) and try to print it out. But I do not have any idea to do that?!
Can anyone please give me some guide on how to justify the printing margin or to print a image? THANKS!!!
import java.awt.*; import javax.swing.*; import java.awt.print.*;
public class SendToPrinter implements Printable { //Component to print private Component printComp;
//To print the component public static void printComponent(Component c) { new SendToPrinter(c).print(); } //To set the component to print public SendToPrinter(Component printComp) { this.printComp = printComp; } //To print public void print() { PrinterJob printJob = PrinterJob.getPrinterJob(); printJob.setPrintable(this); if (printJob.printDialog()) try { printJob.print(); } catch(PrinterException e) { System.out.println("Error printing: " + e); } }
//To print ( over write the Interface Printable method ) public int print(Graphics g, PageFormat pageFormat, int pageIndex) { if (pageIndex > 0) { return(NO_SUCH_PAGE); } else { Graphics2D g2d = (Graphics2D)g; g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); disableDoubleBuffering(printComp); printComp.paint(g2d); enableDoubleBuffering(printComp); return(PAGE_EXISTS); } }
//To disable the double buffering at the same time public static void disableDoubleBuffering(Component c) { RepaintManager currentManager = RepaintManager.currentManager(c); currentManager.setDoubleBufferingEnabled(false); }
//To enable the double buffering at the same time public static void enableDoubleBuffering(Component c) { RepaintManager currentManager = RepaintManager.currentManager(c); currentManager.setDoubleBufferingEnabled(true); } }
|
|