java swt image(imagedata)旋转

nas*_*983 2 java swt image rotation

我正在尝试做一个简单的程序,我可以在画布上添加一些png.其中一个选项是旋转选定的png.我有旋转问题.当我使用Transformation时,它会在画布上旋转所有内容.我只创建了旋转90'或翻转的例子,但我想以任何角度旋转.

我也试图将swt图像转换为bufferedimage,旋转然后将bufferedimage转换为swt图像并绘制它,但它非常慢并且在透明度方面存在一些问题.

我将感激不尽的帮助 - 我想通过图像中心旋转.对于drawig我正在使用GC和Canvas - 新的GC(画布);


编辑: 我仍然有图像中心旋转问题:

gc.setAdvanced(true);
        if (!gc.getAdvanced()) {
            gc.drawText("Advanced graphics not supported", 30, 30, true);
            return;
        }

        Transform oldTransform = new Transform(gc.getDevice());  
        gc.getTransform(oldTransform);

        Transform transform = new Transform(GCController.getCanvas().getDisplay());
        transform.translate(width/2, height/2);
        transform.rotate(rotation);
        gc.setTransform(transform);
        gc.drawImage(image, x, y);

        gc.setTransform(oldTransform);

        transform.dispose();
Run Code Online (Sandbox Code Playgroud)

Fav*_*ius 6

你必须使用矩阵变换.看看这个 SWT片段.作为参考,我已经以15度为例(到最后).

import org.eclipse.swt.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class ImageTest {    
    public static void main(String[] args) {
        final Display display = new Display();

        final Image image = new Image(display,"next.png");


        final Rectangle rect = image.getBounds();
        Shell shell = new Shell(display);
        shell.setText("Matrix Tranformations");
        shell.setLayout(new FillLayout());
        final Canvas canvas = new Canvas(shell, SWT.DOUBLE_BUFFERED);
        canvas.addPaintListener(new PaintListener () {
            public void paintControl(PaintEvent e) {    
                GC gc = e.gc;
                gc.setAdvanced(true);
                if (!gc.getAdvanced()){
                    gc.drawText("Advanced graphics not supported", 30, 30, true);
                    return;
                }

                // Original image
                int x = 30, y = 30;
                gc.drawImage(image, x, y); 
                x += rect.width + 30;

                Transform transform = new Transform(display);

                // Note that the tranform is applied to the whole GC therefore
                // the coordinates need to be adjusted too.

                // Reflect around the y axis.
                transform.setElements(-1, 0, 0, 1, 0 ,0);
                gc.setTransform(transform);
                gc.drawImage(image, -1*x-rect.width, y);



                // Rotate by 45 degrees 
                float cos45 = (float)Math.cos(Math.PI/4);
                float sin45 = (float)Math.sin(Math.PI/4);
                transform.setElements(cos45, sin45, -sin45, cos45, 0, 0);
                gc.setTransform(transform);
                gc.drawImage(image, x , y);

                float cos15 = (float)Math.cos(Math.PI/12);
                float sin15 = (float)Math.sin(Math.PI/12);
                transform.setElements(cos15, sin15, -sin15, cos15, 0, 0);
                gc.setTransform(transform);
                gc.drawImage(image, x+rect.width/2 , y+rect.height/2);

                transform.dispose();
            }
        });

        shell.setSize(350, 550);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        image.dispose();
        display.dispose();
    }
}
Run Code Online (Sandbox Code Playgroud)

>>输出

在此输入图像描述