Search code examples
javaeclipseswtrcpdraw2d

Eclipse/SWT: Rectangle taking up entire canvas


I am writing an Eclipse RCP-based application and am trying to draw a rectangle on top of a ViewPart. However, the rectangle seems to take up the whole screen even when specifiying the bounds. The following is my code.

public void createPartControl(Composite parent) {
        Shell shell = parent.getShell();

        Canvas canvas = new Canvas(parent, SWT.NONE);
        LightweightSystem lws = new LightweightSystem(canvas);
        RectangleFigure rectangle = new RectangleFigure();
        rectangle.setBounds(new Rectangle(0, 0, 10, 10));
        rectangle.setBackgroundColor(ColorConstants.green);
        lws.setContents(rectangle);
}

Solution

  • I haven't used Draw2D, but I tried modifying your example by creating another rectangle figure and adding it to the first one, and that one shows up. I.e.

    // from your code
    rectangle.setBackgroundColor(ColorConstants.green);
    
    // new code
    RectangleFigure r2 = new RectangleFigure();
    r2.setBounds(new Rectangle(0,0,10,10));
    r2.setBackgroundColor(ColorConstants.blue);
    rectangle.add(r2);
    
    // back to your code
    lws.setContents(rectangle);
    

    It looks fine to me - there's a little blue rectangle in the top left corner of the totally green canvas. I guess that the figure you use as the contents of the canvas, by default (and probably by necessity), takes up the whole canvas.