Snippet112.java Source code

Java tutorial

Introduction

Here is the source code for Snippet112.java

Source

/*
 * Image example snippet: display an image in a group
 *
 * For a list of all SWT example snippets see
 * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;

public class Snippet112 {

    public static void main(String[] args) {
        Display display = new Display();
        final Image image = new Image(display, 20, 20);
        Color color = display.getSystemColor(SWT.COLOR_RED);
        GC gc = new GC(image);
        gc.setBackground(color);
        gc.fillRectangle(image.getBounds());
        gc.dispose();
        color.dispose();

        Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());
        Group group = new Group(shell, SWT.NONE);
        group.setLayout(new FillLayout());
        group.setText("a square");
        Canvas canvas = new Canvas(group, SWT.NONE);
        canvas.addPaintListener(new PaintListener() {
            public void paintControl(PaintEvent e) {
                e.gc.drawImage(image, 0, 0);
            }
        });

        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        image.dispose();
        display.dispose();
    }
}