Display an image in a group : Image « SWT JFace Eclipse « Java






Display an image in a group

Display an image in a group

/*
 * 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();
  }
}


           
       








Related examples in the same category

1.Image Analyzer in SWTImage Analyzer in SWT
2.Set icons with different resolutionsSet icons with different resolutions
3.Capture a widget image with a GCCapture a widget image with a GC
4.Create an icon (in memory)Create an icon (in memory)
5.Display an animated GIFDisplay an animated GIF
6.Rotate and flip an imageRotate and flip an image
7.SWT and Image
8.Draw Images ExampleDraw Images Example
9.Clip ImageClip Image
10.Double BufferDouble Buffer
11.Image BasicsImage Basics
12.File Icon Util
13.Demonstrates how to draw imagesDemonstrates how to draw images
14.scroll an image (flicker free, no double buffering)scroll an image (flicker free, no double buffering)
15.Transfer type to transfer SWT ImageData objects.