ImagePartDraw.java Source code

Java tutorial

Introduction

Here is the source code for ImagePartDraw.java

Source

import org.eclipse.swt.SWT;
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.layout.FillLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class ImagePartDraw {
    public static void main(String[] args) {
        final Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setText("Canvas Example");
        shell.setLayout(new FillLayout());

        Canvas canvas = new Canvas(shell, SWT.NONE);

        canvas.addPaintListener(new PaintListener() {
            public void paintControl(PaintEvent e) {
                Image image = new Image(display, "yourFile.gif");
                System.out.println(image.getImageData().scanlinePad);
                image.getImageData().scanlinePad = 40;
                System.out.println(image.getImageData().scanlinePad);

                e.gc.drawImage(image, 0, 0);
                // Determine how big the drawing area is
                Rectangle rect = shell.getClientArea();

                // Get information about the image
                ImageData data = image.getImageData();

                // Calculate drawing values
                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;

                // Draw the image
                e.gc.drawImage(image, srcX, srcY, srcWidth, srcHeight, rect.width - destWidth,
                        rect.height - destHeight, destWidth, destHeight);
            }
        });

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