GC: drawImage(Image image, int x, int y) : GC « org.eclipse.swt.graphics « Java by API






GC: drawImage(Image image, int x, int y)

import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class MainClass {
  public static void main(String[] args) {

  Display display = new Display();
    final Shell shell = new Shell(display);

    final Image image = new Image(display, new MainClass().getClass().getResourceAsStream(
        "swt.png"));
    System.out.println(image.getImageData().scanlinePad);
    image.getImageData().scanlinePad = 40;
    System.out.println(image.getImageData().scanlinePad);

    shell.addPaintListener(new PaintListener() {
      public void paintControl(PaintEvent event) {
        event.gc.drawImage(image, 0, 0);
        Rectangle rect = shell.getClientArea();
        ImageData data = image.getImageData();

        int srcX = data.width / 4;
        int srcY = data.height / 4;
        int srcWidth = data.width / 2;
        int srcHeight = data.height / 2;
        int destWidth = 2 * srcWidth;
        int destHeight = 2 * srcHeight;

        event.gc.drawImage(image, srcX, srcY, srcWidth, srcHeight, rect.width
            - destWidth, rect.height - destHeight, destWidth, destHeight);
      }
    });
    shell.setText("Draw Images");
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    image.dispose();
    display.dispose();
  }
}

           
       








Related examples in the same category

1.GC: drawFocus(int x, int y, int width, int height)
2.GC: drawLine(int x1, int y1, int x2, int y2)
3.GC: drawOval(int x, int y, int width, int height)
4.GC: drawPoint(int x, int y)
5.GC: drawPolygon(int[] pointArray)
6.GC: drawRoundRectangle(int x, int y, int width, int height, int arcWidth, int arcHeight)
7.GC: drawText(String string, int x, int y)
8. drawText(String string, int x, int y, int flags)
9.GC: fillArc(int x, int y, int width, int height, int startAngle, int endAngle)
10.GC: setBackground(Color c)
11.GC: setFont(Font f)
12.GC: setLineWidth(int w)