java swt 图像(imagedata)旋转
我正在尝试做一个简单的程序,我可以在其中添加一些 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须使用矩阵变换。看一下 这个 SWT 片段。作为参考,我以 15 度为例(接近尾声)。
>>输出
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).
>>Output
好的。我解决了。
问题在于翻译不好。因此,首先我必须通过 x 和 y 位置加上宽度和长度的一半来平移形状,然后旋转回之前的位置:
第一次平移:
然后旋转和第二次平移:
Ok. I solved it.
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:
then rotation and second translation: