【问题标题】:java swt image (imagedata) rotationjava swt 图像(图像数据)旋转
【发布时间】:2011-05-05 15:19:53
【问题描述】:

我正在尝试做一个简单的程序,我可以在其中向画布添加一些 png。 选项之一是旋转选定的 png。我有旋转的问题。 当我使用 Transformation 时,它会旋转画布上的所有内容。 我只创建了旋转 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();

【问题讨论】:

    标签: java image swt rotation


    【解决方案1】:

    你必须使用矩阵变换。看看this SWT sn-p。作为参考,我以 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();
        }
    }
    

    >>输出

    【讨论】:

      【解决方案2】:

      好的。我解决了。

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

      【讨论】:

        猜你喜欢
        • 2022-01-09
        • 2012-08-23
        • 1970-01-01
        • 2013-09-05
        • 2015-09-16
        • 1970-01-01
        • 2018-08-24
        相关资源
        最近更新 更多