Home » Eclipse Projects » GEF » no LayoutManager when printing draw2D figures
no LayoutManager when printing draw2D figures [message #245201] |
Wed, 27 August 2008 10:25  |
Eclipse User |
|
|
|
Originally posted by: Gerhard.Hinterndorfer.pedrics.at
Hello,
I need to print some draw2D figures and I do it the in the following way:
---- code snippet start -----------
public void paintForPrint(GC gc, int x, int y){
if(printGraphics == null){
printGraphics = new SWTGraphics(gc);
}
Rectangle evtBounds = eventFigures.getBounds();
//move all the figures to the passed offset.
eventFigures.setLocation(Point.SINGLETON.setLocation(x, y));
eventFigures.paint(printGraphics);
//Reset the location
eventFigures.setBounds(evtBounds);
}
---- code snippet end -----------
the result is, that the figures are printed, but the layout manager is
not relocating the figures at the right place as it is done on the display.
Does anybody have an idea how I can force that the layout manager is
running and locating my figures at the right place also at the printer?
Thanks a lot for your help in advance
Gerhard
|
|
| |
Re: no LayoutManager when printing draw2D figures [message #245231 is a reply to message #245228] |
Thu, 28 August 2008 05:23   |
Eclipse User |
|
|
|
Originally posted by: Gerhard.Hinterndorfer.pedrics.at
David,
thanks for your idea, but I think constraints are again only used by
Layoutmanager as e.g. XYLayout ... and my Problem is, that the
Layoutmanager is not active when printing.
Just to have a better idea what I am talking about, I prepared a demo:
-------- demo start ---------
package at.testDraw2D;
import org.eclipse.draw2d.*;
import org.eclipse.draw2d.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.printing.PrintDialog;
import org.eclipse.swt.printing.Printer;
import org.eclipse.swt.printing.PrinterData;
import org.eclipse.swt.widgets.*;
public class CanvasDraw2DTest {
private static int canvasWidth = 460;
private static int canvasHeight = 300;
private Canvas plotArea;
private Font displayFont;
private boolean alreadyPrinted = false;
public static void main(String[] args) {
new CanvasDraw2DTest().startApplication();
}
private void startApplication() {
Shell shell = new Shell();
shell.setText("CanvasDraw2DTest");
shell.setSize(500, 500);
shell.open();
plotArea = new Canvas(shell, SWT.NO_REDRAW_RESIZE |
SWT.DOUBLE_BUFFERED);
plotArea.setBounds(10, 10, canvasWidth, canvasHeight);
Color backgroundColor = new
Color(org.eclipse.swt.widgets.Display.getDefault(), 180, 228, 187);
plotArea.setBackground(backgroundColor);
plotArea.addPaintListener(new org.eclipse.swt.events.PaintListener() {
public void paintControl(org.eclipse.swt.events.PaintEvent e) {
plotOnCanvas(canvasWidth, canvasHeight, displayFont, e.gc);
}
});
displayFont = new Font(Display.getDefault(), "Arial", 8, SWT.NORMAL);
LightweightSystem lws = new LightweightSystem(plotArea);
lws.setContents(createFigures(canvasWidth, canvasHeight,
displayFont));
// set Draw2D Layer into transparent mode
lws.getRootFigure().setOpaque(false);
Display display = Display.getDefault();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
if(alreadyPrinted){
display.sleep();
}else{
printSwtAndDraw2DObjects(shell);
alreadyPrinted = true;
}
}
}
displayFont.dispose();
backgroundColor.dispose();
}
private void printSwtAndDraw2DObjects(Shell shell){
PrintDialog printDialog = new PrintDialog(shell);
PrinterData printerData = printDialog.open();
if (printerData != null) {
Printer printer = new Printer(printerData);
if (!printer.startJob("CanvasDraw2DTest"))
System.out.println("Printer start job failed");
else {
if (!printer.startPage())
System.out.println("Printer start failed");
else {
GC gc = new GC(printer);
Font printerFont = new Font(printer, "Arial", 8, SWT.NORMAL);
int plotWidth = 2000;
int plotHeight = 1000;
// plot SWT part
plotOnCanvas(plotWidth, plotHeight, printerFont, gc);
// plot Draw2D part
SWTGraphics printGraphics = new SWTGraphics(gc);
IFigure plotFigures = createFigures(plotWidth,
plotHeight, printerFont);
plotFigures.paint(printGraphics);
printGraphics.dispose();
printer.endPage();
gc.dispose();
printerFont.dispose();
}
printer.endJob();
}
printer.dispose();
}else{
System.out.println("Sorry, no Printer support!");
}
}
private void plotOnCanvas(int width, int height, Font font, GC gc) {
gc.setForeground(Display.getDefault().getSystemColor(SWT.COL OR_BLACK));
gc.drawRectangle(10, 10, width - 20, height - 20);
gc.setFont(font);
gc.drawText("Canvas Text.", 20, height - 40, SWT.DRAW_TRANSPARENT);
gc.drawLine(10, 10, width - 20, height - 20);
gc.drawLine(10, height - 20, width - 20, 10);
}
private IFigure createFigures(int width, int height, Font font){
IFigure topLevelFigure = new Figure();
topLevelFigure.setForegroundColor(ColorConstants.red);
topLevelFigure.setFont(font);
IFigure labelFigures = new Figure();
labelFigures.setLayoutManager(new FlowLayout());
topLevelFigure.add(labelFigures);
labelFigures.setBounds(
new org.eclipse.draw2d.geometry.Rectangle(0, 0, width, height));
labelFigures.add(new extLabel(width / 2, height / 2, "Draw2D
Label1"));
labelFigures.add(new extLabel(width / 10, height / 10, "Draw2D
Label2"));
return topLevelFigure;
}
}
class extLabel extends Label {
Label toolTip;
public extLabel(int xPos, int yPos, String text) {
super();
this.setText(text);
toolTip = new Label("Tool Tip: " + text);
setToolTip(toolTip);
//just that the labels can be seen if no Layoutmanager is added
// or running
this.setLocation(new org.eclipse.draw2d.geometry.Point(xPos, yPos));
this.setSize(500,80);
}
}
-------- demo end -----------
At the display, the LayoutManager is relocating the 2 Draw2D Labels via
FlowLayout, starting from the top left corner. But on the printer, the
flowLayout is not getting active.
This behaviour can also be simulated on Display, if the
"labelFigures.setLayoutManager(new FlowLayout());"
line is commented out in the method "createFigures".
Any ideas how I can solve this issue?
cheers Gerhard
|
|
|
Re: no LayoutManager when printing draw2D figures [message #245246 is a reply to message #245231] |
Thu, 28 August 2008 08:45   |
Eclipse User |
|
|
|
Originally posted by: lifesting.gmail.com
> David,
>
> thanks for your idea, but I think constraints are again only used by
> Layoutmanager as e.g. XYLayout ... and my Problem is, that the
> Layoutmanager is not active when printing.
>
> Just to have a better idea what I am talking about, I prepared a demo:
> -------- demo start ---------
> package at.testDraw2D;
>
> import org.eclipse.draw2d.*;
> import org.eclipse.draw2d.Label;
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.graphics.*;
> import org.eclipse.swt.printing.PrintDialog;
> import org.eclipse.swt.printing.Printer;
> import org.eclipse.swt.printing.PrinterData;
> import org.eclipse.swt.widgets.*;
>
> public class CanvasDraw2DTest {
> private static int canvasWidth = 460;
> private static int canvasHeight = 300;
> private Canvas plotArea;
> private Font displayFont;
> private boolean alreadyPrinted = false;
>
> public static void main(String[] args) {
> new CanvasDraw2DTest().startApplication();
> }
>
> private void startApplication() {
> Shell shell = new Shell();
> shell.setText("CanvasDraw2DTest");
> shell.setSize(500, 500);
> shell.open();
>
> plotArea = new Canvas(shell, SWT.NO_REDRAW_RESIZE |
> SWT.DOUBLE_BUFFERED);
> plotArea.setBounds(10, 10, canvasWidth, canvasHeight);
> Color backgroundColor = new
> Color(org.eclipse.swt.widgets.Display.getDefault(), 180, 228, 187);
> plotArea.setBackground(backgroundColor);
> plotArea.addPaintListener(new org.eclipse.swt.events.PaintListener() {
> public void paintControl(org.eclipse.swt.events.PaintEvent e) {
> plotOnCanvas(canvasWidth, canvasHeight, displayFont, e.gc);
> }
> });
> displayFont = new Font(Display.getDefault(), "Arial", 8, SWT.NORMAL);
>
> LightweightSystem lws = new LightweightSystem(plotArea);
> lws.setContents(createFigures(canvasWidth, canvasHeight,
> displayFont));
> // set Draw2D Layer into transparent mode
> lws.getRootFigure().setOpaque(false);
>
> Display display = Display.getDefault();
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch()) {
> if(alreadyPrinted){
> display.sleep();
> }else{
> printSwtAndDraw2DObjects(shell);
> alreadyPrinted = true;
> }
> }
> }
> displayFont.dispose();
> backgroundColor.dispose();
> }
>
> private void printSwtAndDraw2DObjects(Shell shell){
> PrintDialog printDialog = new PrintDialog(shell);
> PrinterData printerData = printDialog.open();
> if (printerData != null) {
> Printer printer = new Printer(printerData);
> if (!printer.startJob("CanvasDraw2DTest"))
> System.out.println("Printer start job failed");
> else {
> if (!printer.startPage())
> System.out.println("Printer start failed");
> else {
> GC gc = new GC(printer);
> Font printerFont = new Font(printer, "Arial", 8, SWT.NORMAL);
> int plotWidth = 2000;
> int plotHeight = 1000;
> // plot SWT part
> plotOnCanvas(plotWidth, plotHeight, printerFont, gc);
>
> // plot Draw2D part
> SWTGraphics printGraphics = new SWTGraphics(gc);
> IFigure plotFigures = createFigures(plotWidth,
> plotHeight, printerFont);
> plotFigures.paint(printGraphics);
> printGraphics.dispose();
>
> printer.endPage();
> gc.dispose();
> printerFont.dispose();
> }
> printer.endJob();
> }
> printer.dispose();
> }else{
> System.out.println("Sorry, no Printer support!");
> }
> }
>
> private void plotOnCanvas(int width, int height, Font font, GC gc) {
>
> gc.setForeground(Display.getDefault().getSystemColor(SWT.COL OR_BLACK));
> gc.drawRectangle(10, 10, width - 20, height - 20);
> gc.setFont(font);
> gc.drawText("Canvas Text.", 20, height - 40, SWT.DRAW_TRANSPARENT);
> gc.drawLine(10, 10, width - 20, height - 20);
> gc.drawLine(10, height - 20, width - 20, 10);
> }
>
> private IFigure createFigures(int width, int height, Font font){
> IFigure topLevelFigure = new Figure();
> topLevelFigure.setForegroundColor(ColorConstants.red);
> topLevelFigure.setFont(font);
> IFigure labelFigures = new Figure();
> labelFigures.setLayoutManager(new FlowLayout());
> topLevelFigure.add(labelFigures);
> labelFigures.setBounds(
> new org.eclipse.draw2d.geometry.Rectangle(0, 0, width, height));
> labelFigures.add(new extLabel(width / 2, height / 2, "Draw2D
> Label1"));
> labelFigures.add(new extLabel(width / 10, height / 10, "Draw2D
> Label2"));
> return topLevelFigure;
> }
> }
>
> class extLabel extends Label {
> Label toolTip;
>
> public extLabel(int xPos, int yPos, String text) {
> super();
> this.setText(text);
> toolTip = new Label("Tool Tip: " + text);
> setToolTip(toolTip);
>
> //just that the labels can be seen if no Layoutmanager is added
> // or running
> this.setLocation(new org.eclipse.draw2d.geometry.Point(xPos, yPos));
> this.setSize(500,80);
> }
> }
> -------- demo end -----------
>
> At the display, the LayoutManager is relocating the 2 Draw2D Labels via
> FlowLayout, starting from the top left corner. But on the printer, the
> flowLayout is not getting active.
> This behaviour can also be simulated on Display, if the
> "labelFigures.setLayoutManager(new FlowLayout());"
> line is commented out in the method "createFigures".
>
> Any ideas how I can solve this issue?
>
> cheers Gerhard
You need validate the Background Figure before paint it, here's a example:
.....
// plot Draw2D part
SWTGraphics printGraphics = new SWTGraphics(gc);
IFigure plotFigures = createFigures(plotWidth,
plotHeight, printerFont);
plotFigures.validate(); //Let LayoutManager to
calculate bounds.
plotFigures.paint(printGraphics);
printGraphics.dispose();
Commonly, using *FigureCanvas* instead of Canvas is a good idea because
FigureCanvas would maintain the boring state.
|
|
|
Re: no LayoutManager when printing draw2D figures [message #245287 is a reply to message #245246] |
Fri, 29 August 2008 14:47  |
Eclipse User |
|
|
|
Originally posted by: Gerhard.Hinterndorfer.pedrics.at
thanks a lot David for your help, now it is working ...
cheers Gerhard
David BY Chan schrieb:
> ....
> // plot Draw2D part
> SWTGraphics printGraphics = new SWTGraphics(gc);
> IFigure plotFigures = createFigures(plotWidth,
> plotHeight, printerFont);
> plotFigures.validate(); //Let LayoutManager to calculate
> bounds.
> plotFigures.paint(printGraphics);
> printGraphics.dispose();
>
> Commonly, using *FigureCanvas* instead of Canvas is a good idea because
> FigureCanvas would maintain the boring state.
|
|
|
Goto Forum:
Current Time: Sun May 11 13:15:50 EDT 2025
Powered by FUDForum. Page generated in 0.03347 seconds
|