Java SWT: GC will not draw a simple rectangle on the canvas, but will draw it on the image

I can't get the GC (graphics context) object to draw a simple rectangle on the canvas, and I can't figure out what I'm missing ...

Consider this simple piece of code:

public class MainWindow {

    private Display display;
    private Shell shell;

    public static void main(String args[]) {
        MainWindow mw=new MainWindow();
        mw.render();
    }

    public MainWindow() {

        display=Display.getDefault();
        shell=new Shell(display, SWT.DIALOG_TRIM);

        shell.setText("Graphic Test");
        shell.setSize(400,300);
        shell.setLocation(0, 0);

        GC gc=new GC(shell);
        gc.drawRectangle(50,50,45,45);
        gc.dispose();


    }

    public void render() {

        shell.open();

        while (!shell.isDisposed()) {

            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }

        display.dispose();
    }

}

      

This will draw nothing:
enter image description here

And it won't either:

public class MainWindow {

    private Display display;
    private Shell shell;

    public static void main(String args[]) {
        MainWindow mw=new MainWindow();
        mw.render();
    }

    public MainWindow() {

        display=Display.getDefault();
        shell=new Shell(display, SWT.DIALOG_TRIM);

        shell.setText("Graphic Test");
        shell.setSize(400,300);
        shell.setLocation(0, 0);

        Canvas canvas=new Canvas(shell, SWT.NONE);
        canvas.setSize(shell.getSize().x, shell.getSize().y);

        GC gc=new GC(canvas);
        gc.drawRectangle(50,50,45,45);
        gc.dispose();


    }

    public void render() {

        shell.open();

        while (!shell.isDisposed()) {

            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }

        display.dispose();
    }

}

      

enter image description here

But watch what happens when I set a canvas with a background image and paint on the image:

public class MainWindow {

    private Display display;
    private Shell shell;

    public static void main(String args[]) {
        MainWindow mw=new MainWindow();
        mw.render();
    }

    public MainWindow() {

        display=Display.getDefault();
        shell=new Shell(display, SWT.DIALOG_TRIM);

        shell.setText("Graphic Test");
        shell.setSize(400,300);
        shell.setLocation(0, 0);

        Image background=new Image(display, "background.jpg");

        Canvas canvas=new Canvas(shell, SWT.NONE);
        canvas.setBackgroundImage(background);
        canvas.setSize(shell.getSize().x, shell.getSize().y);

        GC gc=new GC(background);
        gc.drawRectangle(50,50,45,45);
        gc.dispose();


    }

    public void render() {

        shell.open();

        while (!shell.isDisposed()) {

            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }

        display.dispose();
    }

}

      

enter image description here

Suddenly, a miracle. He draws a rectangle.
This way it has no problem drawing the image, but painting to the shell / canvas will do nothing ...

What am I missing here? Can't I lean on something drawable?

+3


source to share


1 answer


This is a temporary problem (drawing on Canvas

or Shell

during the build inital be excessively drawn during subsequent passes of paint, if drawing on Image

stored in your job with the paint in ImageData

). For Canvas

(or Shell

) you have to paint during your paint event as shown in the tutorial . PaintEvent

contains its own GC

, which is correct for administering drawing operations:

final Canvas canvas=..;
canvas.addPaintListener(new PaintListener() { 
    public void paintControl(PaintEvent e) { 
        e.gc.drawRectangle(50,50,45,45);
    }
});

      



enter image description here

+1


source







All Articles