java swt 图像(imagedata)旋转

发布于 2024-11-05 10:21:35 字数 935 浏览 1 评论 0原文

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

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

我将不胜感激的帮助 - 我想通过图像中心旋转。 对于drawig,我使用GC和Canvas - new GC(canvas);


编辑: 我仍然遇到图像中心旋转的问题:

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();

I'm trying to do a simple program, where I can add some png's to canvas.
One of the option is to rotate selected png. I have problem with rotation.
When I'm using Transformation it rotates everything on canvas.
I founded only examples of rotation by 90' or flip but I want to rotate by any angle.

I was also trying to convert swt image to bufferedimage, rotate then convert bufferedimage to swt image and draw it but it's very slow and having some problems with transparency.

I'm will be gratefull for help - I want to rotate via center of image.
For drawig I'm using GC and Canvas - new GC(canvas);


EDIT:
I stil have problem with rotation by center of image:

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();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红衣飘飘貌似仙 2024-11-12 10:21:35

您必须使用矩阵变换。看一下 这个 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();
    }
}

>>输出

在此处输入图像描述

You have to use matrix transformations. Have a look at this SWT snippet. For reference I have shown the 15 degree as example (towards the end).

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

>>Output

enter image description here

作妖 2024-11-12 10:21:35

好的。我解决了。

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(x+width/2, y+height/2);
        transform.rotate(rotation);
        transform.translate(-x-width/2, -y-height/2);

        gc.setTransform(transform);
        gc.drawImage(image, x, y);          
        gc.setTransform(oldTransform);

        transform.dispose();

问题在于翻译不好。因此,首先我必须通过 x 和 y 位置加上宽度和长度的一半来平移形状,然后旋转回之前的位置:

第一次平移:

transform.translate(x+width/2, y+height/2);

然后旋转和第二次平移:

transform.translate(-x-width/2, -y-height/2);

Ok. I solved it.

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(x+width/2, y+height/2);
        transform.rotate(rotation);
        transform.translate(-x-width/2, -y-height/2);

        gc.setTransform(transform);
        gc.drawImage(image, x, y);          
        gc.setTransform(oldTransform);

        transform.dispose();

The problem was in a bad translation. So first I had to translate my shape by it's x and y position plus half of width and length and after rotation back to previous position:

first translation:

transform.translate(x+width/2, y+height/2);

then rotation and second translation:

transform.translate(-x-width/2, -y-height/2);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文