Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Rendering a Control to a Printer GC
Rendering a Control to a Printer GC [message #442817] |
Tue, 14 September 2004 01:43 |
Mark Freiheit Messages: 30 Registered: July 2009 |
Member |
|
|
Please forgive the newbie question.
I am trying to print a Control, but am having some difficulty.
If I could get the Control (ie a Browser) to render correctly onto
a GC or an Image, I think I can get there. The general algorithm I
have tried is
GC printerGC = new GC(myPrinter);
Canvas canvas = new Canvas(new Shell(), SWT.NONE);
Browser browser = new Browser(canvas, SWT.NONE);
GC browserGC = new GC(browser);
browser.setText(myHTMLString);
browser.redraw();
Image image = new Image(Display.getCurrent(), clientArea); //
clientArea is the Printer's
clientArea
browserGC.copyArea(image, 0, 0);
printerGC.drawImage(image, 0, 0);
Not sure why this does not work, but the Image (image) Does not appear
to render correctly. Could someone please explain how this should work,
or point me to an example?
Thanks in advance -- Mark
|
|
|
Re: Rendering a Control to a Printer GC [message #442927 is a reply to message #442817] |
Wed, 15 September 2004 14:48 |
Veronika Irvine Messages: 1272 Registered: July 2009 |
Senior Member |
|
|
You need two images because the image must be created on the correct device:
public static void main (String [] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new GridLayout(3, false));
for (int i = 0; i < 10; i++) {
Button b = new Button(shell, SWT.PUSH);
b.setText("Button "+i);
}
Button b = new Button(shell, SWT.PUSH);
b.setText("Print");
b.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
PrinterData data = Printer.getDefaultPrinterData();
if (data == null) {
System.out.println("Warning: No default printer.");
return;
}
Printer printer = new Printer(data);
if (printer.startJob("SWT Printing Snippet")) {
// capture shell as it currently appears into an ImageData object
Rectangle bounds = shell.getBounds();
Image srcImage = new Image(display, bounds.width, bounds.height);
GC srcGC = new GC(display);
srcGC.copyArea(srcImage, bounds.x, bounds.y);
srcGC.dispose();
ImageData srcData = srcImage.getImageData();
srcImage.dispose();
// draw onto printer scaled appropriately
Point srcDPI = display.getDPI();
Point destDPI = printer.getDPI();
Rectangle trim = printer.computeTrim(0, 0, 0, 0);
int leftMargin = destDPI.x + trim.x; // one inch from left side of paper
int topMargin = destDPI.y / 2 + trim.y; // one-half inch from top edge
of paper
Image destImage = new Image(printer, srcData);
GC destGC = new GC(printer);
if (printer.startPage()) {
destGC.drawImage(destImage, 0, 0, bounds.width, bounds.height,
leftMargin, topMargin, bounds.width*destDPI.x/ srcDPI.x,
bounds.height*destDPI.y/ srcDPI.y);
printer.endPage();
}
destGC.dispose();
printer.endJob();
destImage.dispose();
}
printer.dispose();
}
});
shell.pack();
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose();
}
}
"Mark Freiheit" <freiheit@speakeasy.net> wrote in message
news:ci5ic5$9op$1@eclipse.org...
> Please forgive the newbie question.
>
> I am trying to print a Control, but am having some difficulty.
> If I could get the Control (ie a Browser) to render correctly onto
> a GC or an Image, I think I can get there. The general algorithm I
> have tried is
>
> GC printerGC = new GC(myPrinter);
> Canvas canvas = new Canvas(new Shell(), SWT.NONE);
> Browser browser = new Browser(canvas, SWT.NONE);
> GC browserGC = new GC(browser);
> browser.setText(myHTMLString);
> browser.redraw();
> Image image = new Image(Display.getCurrent(), clientArea); //
> clientArea is the Printer's
> clientArea
> browserGC.copyArea(image, 0, 0);
> printerGC.drawImage(image, 0, 0);
>
> Not sure why this does not work, but the Image (image) Does not appear
> to render correctly. Could someone please explain how this should work,
> or point me to an example?
>
> Thanks in advance -- Mark
>
|
|
|
Goto Forum:
Current Time: Sat Apr 27 01:46:05 GMT 2024
Powered by FUDForum. Page generated in 0.02872 seconds
|