Java's SWT:GC doesn't draw simple rectangle on canvas, but draws it on image


very tired

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

Consider the following simple code snippet:

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 doesn't draw anything:
enter image description here

And this doesn'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 notice what happens when I set the 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. It does draw rectangles.
So drawing on the image is no problem, but drawing on the shell/canvas produces nothing.

What am I missing here? Shouldn't I be able to draw on Drawable?

Hierarchy

It's a timing issue ( painting on Canvasor Shellduring initial construction gets overdrawn on subsequent paint passes, while Imagepaint operations on persistence keep drawing ImageData). For Canvas(one or more Shell), you should paint during its paint event, as shown in this tutorial . Bring yourself what is right to manage with your drawing operations to:PaintEventGC

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

enter image description here

Related


Java SWT GC can't draw text on Canvas

James Allison I have the following code: final Canvas canvas = new Canvas(mainshell, SWT.NO_REDRAW_RESIZE); canvas.setBounds(0, 0, mainshell.getSize().x, mainshell.getSize().y); canvas.setBackgroundImage( new Image(display, "BlueBack.jpg" ));

Java SWT GC can't draw text on Canvas

James Allison I have the following code: final Canvas canvas = new Canvas(mainshell, SWT.NO_REDRAW_RESIZE); canvas.setBounds(0, 0, mainshell.getSize().x, mainshell.getSize().y); canvas.setBackgroundImage( new Image(display, "BlueBack.jpg" ));

canvas.drawRect doesn't draw a rectangle on the image

Krishna Here canvas.drawRect will be different when using different bitmaps. I want to draw a rectangle over the top image and want the part of the image outside the rectangle to be darkened or blurred. please help me. draw.setOnClickListener(new View.OnClickL

canvas.drawRect doesn't draw a rectangle on the image

Krishna Here canvas.drawRect will be different when using different bitmaps. I want to draw a rectangle over the top image and want the part of the image outside the rectangle to be darkened or blurred. Please help me. draw.setOnClickListener(new View.OnClickL

Canvas doesn't draw rectangle after for loop

Digital Brent I'm trying to create a news ticker that renders text in small square "pixels". I say "pixels" because they look like pixels, but the actual displayed square is larger than 1px. So far I can get all the rendered letters from the object I build (co

Canvas doesn't draw rectangle after for loop

Digital Brent I'm trying to create a news ticker that renders text in small square "pixels". I say "pixels" because they look like pixels, but the actual displayed square is larger than 1px. So far I can get all the rendered letters from the object I build (co

Canvas doesn't draw rectangle after for loop

Digital Brent I'm trying to create a news ticker that renders text in small square "pixels". I say "pixels" because they look like pixels, but the actual displayed square is larger than 1px. So far I can get all the rendered letters from the object I build (co

Canvas doesn't draw rectangle after for loop

Digital Brent I'm trying to create a news ticker that renders text in small square "pixels". I say "pixels" because they look like pixels, but the actual displayed square is larger than 1px. So far I can get all the rendered letters from the object I build (co

React-Native-Canvas doesn't draw whole rectangle

Brian Ton While making another app, I ran into a problem react-native-canvas, so I created this minimal project. The problem is that the WebView is much smaller than the Canvas and doesn't fill the entire canvas when appropriate. Here is the code (very similar

React-Native-Canvas doesn't draw whole rectangle

Brian Ton While making another app, I ran into a problem react-native-canvas, so I created this minimal project. The problem is that the WebView is much smaller than the Canvas and doesn't fill the entire canvas when appropriate. Here is the code (very similar

Scrolling not working with Canvas and GC in Java SWT

Joe I'm trying to draw some shapes and lines on a SWT Canvas object using GC [Graphics Context]. Canvas objects are initialized with a fixed size & V_SCROLL|H_SCROLL. I want the Canvas to be scrollable once the GC goes beyond the Canvas bounds. Although the sc

Scrolling not working with Canvas and GC in Java SWT

Joe I'm trying to draw some shapes and lines on a SWT Canvas object using GC [Graphics Context]. Canvas objects are initialized with a fixed size & V_SCROLL|H_SCROLL. I want the Canvas to be scrollable once the GC goes beyond the Canvas bounds. Although the sc

Scrolling not working with Canvas and GC in Java SWT

Joe I'm trying to draw some shapes and lines on a SWT Canvas object using GC [Graphics Context]. Canvas objects are initialized with a fixed size & V_SCROLL|H_SCROLL. I want the Canvas to be scrollable once the GC goes beyond the Canvas bounds. Although the sc

SWT draws "transparent" rectangle on shell

Zero: I want to create a "Selection Area" tool. The tool should allow drawing rectangular areas on the screen using the mouse. I used a fullscreen, semi-transparent, dimmed swt Shellas the background and drew a white rectangle on it to represent the selected a

SWT draws "transparent" rectangle on shell

Zero: I want to create a "Selection Area" tool. The tool should allow drawing rectangular areas on the screen using the mouse. I used a fullscreen, semi-transparent, dimmed swt Shellas the background and drew a white rectangle on it to represent the selected a

SWT draws "transparent" rectangle on shell

Zero: I want to create a "Selection Area" tool. The tool should allow drawing rectangular areas on the screen using the mouse. I used a fullscreen, semi-transparent, dimmed swt Shellas the background and drew a white rectangle on it to represent the selected a

Draw simple rectangle on Jframe in Java

wfbarksdale: I am extending JFrame like this: public GameFrame() { this.setBounds(30, 30, 500, 500); this.setDefaultCloseOperation(EXIT_ON_CLOSE); initializeSquares(); } private void initializeSquares(){ for(int i = 0; i < 5; i++){ thi

Draw simple rectangle on Jframe in Java

wfbarksdale: I am extending JFrame like this: public GameFrame() { this.setBounds(30, 30, 500, 500); this.setDefaultCloseOperation(EXIT_ON_CLOSE); initializeSquares(); } private void initializeSquares(){ for(int i = 0; i < 5; i++){ thi

Draw simple rectangle on Jframe in Java

wfbarksdale: I am extending JFrame like this: public GameFrame() { this.setBounds(30, 30, 500, 500); this.setDefaultCloseOperation(EXIT_ON_CLOSE); initializeSquares(); } private void initializeSquares(){ for(int i = 0; i < 5; i++){ thi

Draw simple rectangle on Jframe in Java

wfbarksdale: I am extending JFrame like this: public GameFrame() { this.setBounds(30, 30, 500, 500); this.setDefaultCloseOperation(EXIT_ON_CLOSE); initializeSquares(); } private void initializeSquares(){ for(int i = 0; i < 5; i++){ thi

My Java program doesn't draw the rectangle over the entire window

user 2066392 I'm trying to draw an 80*80 rectangle on a 800*800 window, but for some reason I can only draw two columns! ! I feel like I'm missing something about Java. I have a class called: DrawBoard which has a method that overrides the paint() method. An o

Tkinter won't draw rectangle on Canvas

Cadel Watson I'm trying to initialize a grid of blue rectangles with a user-specified size. However, the rectangle is not drawn on the initialized canvas. I am trying to store them in a matrix for later use. My code is as follows: import Tkinter import sys fro

Tkinter won't draw rectangle on Canvas

Cadel Watson I'm trying to initialize a grid of blue rectangles with a user-specified size. However, the rectangle is not drawn on the initialized canvas. I am trying to store them in a matrix for later use. My code is as follows: import Tkinter import sys fro

Why doesn't HTML5 drawImage draw image on canvas?

humble 7 I tried adding an eventListener to draw the image only after the image is loaded. I've also tried putting event listeners before and after setting the image src, but still nothing seems to work. I can't seem to figure out what's causing it. Can someon

Why doesn't HTML5 drawImage draw image on canvas?

humble 7 I tried adding an eventListener to draw the image only after the image is loaded. I've also tried putting event listeners before and after setting the image src, but still nothing seems to work. I can't seem to figure out what's causing it. Can someon

Why doesn't HTML5 drawImage draw image on canvas?

humble 7 I tried adding an eventListener to draw the image only after the image is loaded. I've also tried putting event listeners before and after setting the image src, but still nothing seems to work. I can't seem to figure out what's causing it. Can someon