Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
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 Go to next message
Mark Freiheit is currently offline Mark FreiheitFriend
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 Go to previous message
Veronika Irvine is currently offline Veronika IrvineFriend
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
>
Previous Topic:HELP! (where is the default "org.xml.sax.driver" provided by Eclipse?)
Next Topic:Newbie - Table ScrollBar visibility
Goto Forum:
  


Current Time: Sat Apr 27 01:46:05 GMT 2024

Powered by FUDForum. Page generated in 0.02872 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top