ImageRotate.java Source code

Java tutorial

Introduction

Here is the source code for ImageRotate.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.layout.FillLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class ImageRotate {
    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");
                ImageData sd = image.getImageData();

                ImageData dd = new ImageData(sd.height, sd.width, sd.depth, sd.palette);

                int style = SWT.UP;

                boolean up = (style & SWT.UP) == SWT.UP;

                // Run through the horizontal pixels
                for (int sx = 0; sx < sd.width; sx++) {
                    // Run through the vertical pixels
                    for (int sy = 0; sy < sd.height; sy++) {
                        // Determine where to move pixel to in destination image data
                        int dx = up ? sy : sd.height - sy - 1;
                        int dy = up ? sd.width - sx - 1 : sx;
                        // Swap the x, y source data to y, x in the destination
                        dd.setPixel(dx, dy, sd.getPixel(sx, sy));
                    }
                }

                // Create the vertical image
                Image vertical = new Image(display, dd);

                // Draw the vertical image onto the original GC
                e.gc.drawImage(vertical, 10, 10);

                vertical.dispose();
            }
        });

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