Java's SWT:GC doesn't draw simple rectangle on canvas, but draws it on image
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:
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();
}
}
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();
}
}
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?
It's a timing issue ( painting on Canvas
or Shell
during initial construction gets overdrawn on subsequent paint passes, while Image
paint 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:PaintEvent
GC
final Canvas canvas=..;
canvas.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
e.gc.drawRectangle(50,50,45,45);
}
});