Graphics2D-在Graphics2D对象上旋转形状 [英] Graphics2D - Rotating Shapes on a Graphics2D object

查看:59
本文介绍了Graphics2D-在Graphics2D对象上旋转形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Graphics2D对象,可用于在Canvas上绘制.我在Canvas上绘制了多个形状,并且只希望变换其中的一个(或一部分).

I have a Graphics2D object which I use to draw on my Canvas. I draw multiple shapes on the Canvas and want to transform only one (or part) of them.

我会尽量保持简单:

void render(Graphics2D g) {
    ... // Draw shape 1
    ... // Draw shape 2
    ... // Draw shape 3
}

在保持形状1和3不变的情况下如何旋转形状2?旋转"是指围绕其中心点旋转,我们可以将其定义为xy例如.

How would I go about rotating shape 2 while leaving shape 1 and 3 intact? By "rotate" I mean rotating around its center point, which we can define as x and y for example.

一段时间以来,我一直在寻找一种实现此目的的方法,但是找不到任何可以按照我想要的方式工作的东西.

I've been looking for a way to do this for a while now, but couldn't find anything that works the way I want it to.

有没有简单的方法可以做到这一点?

Is there any simple way to do this?

推荐答案

而不是围绕形状的中心点旋转形状,而是旋转然后平移画布.要围绕形状的中心在(x, y)处旋转,请先将画布平移(-x, -y),然后旋转画布-d度,然后按(0,0)正常绘制形状.

Rather than rotating the shape around it's centre point, rotate and then translate the canvas. To rotate around the centre of the shape at (x, y), first translate the canvas by (-x, -y) and then rotate the canvas -d degrees and draw the shape as normal at (0,0).

完成后,向后旋转然后向后平移(请注意,通过这些几何变换,顺序很重要,先平移然后旋转将为您带来完全不同的结果).

When you're done, rotate back then translate back (note that with these geometric transformations the order is important, translating and then rotating will give you a completely different outcome).

这意味着您仍然可以旋转任何对象,而不必自己重新计算坐标.

This means that you can still draw an object at any rotation without having to recalculate the coordinates yourself.

这篇关于Graphics2D-在Graphics2D对象上旋转形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆