gpt4 book ai didi

java - 自定义形状旋转问题

转载 作者:行者123 更新时间:2023-12-01 14:51:12 25 4
gpt4 key购买 nike

我尝试围绕其中心旋转自定义形状,但无法获得预期的结果。

我想要的是

*形状应绕其中心旋转而不移动自身。*

我的解决方案目前正在做的是 围绕其中心旋转整个形状,每次旋转都会改变其位置。

我有多个形状,因此我创建了一个类来封装形状及其在以下类中的转换

public abstract class Shoe implements Shape, ShoeShape {

// variable declaration

/**
*
*/
public Shoe() {
position = new Point();
lastPosition = new Point();
}




public void draw(Graphics2D g2, AffineTransform transform, boolean firstTime) {

AffineTransform af = firstTime ? getInitTransform()
: getCompositeTransform();

if (af != null) {


Shape s = af.createTransformedShape(this);

if (getFillColor() != null) {
g2.setColor(getFillColor());
g2.fill(s);
} else {
g2.draw(s);
}
}

}



}




public AffineTransform getCompositeTransform() {
AffineTransform af = new AffineTransform();
af.setToIdentity();
af.translate(position.getX(), position.getY());
Point2D centerP = calculateShapeCenter();
af.rotate(orientation, centerP.getX(), centerP.getY());
return af;
}







public void onMouseDrag(MouseEvent me, Rectangle2D canvasBoundary,
int selectionOperation) {

// shape operation can be either resize , rotate , translate ,
switch (selectionOperation) {
case MmgShoeViewer.SHAPE_OPERATION_MOVE:
// MOVEMENT
break;
case MmgShoeViewer.SHAPE_OPERATION_ROTATE:

Point2D origin = calculateShapeCenter();
Point2D.Double starting = new Point2D.Double(me.getX(), me.getY());
currentAngle = RotationHelper.getAngle(origin, starting);
rotationAngle = currentAngle - startingAngle;
rotate(rotationAngle);
break;
case MmgShoeViewer.SHAPE_OPERATION_RESIZE:
break;
default:
System.out.println(" invalid select operation");
}
}



public void onMousePress(MouseEvent me, Rectangle2D canvasBoundary,
int selectionOperation) {

// shape operation can be either resize , rotate , translate ,
switch (selectionOperation) {
case MmgShoeViewer.SHAPE_OPERATION_MOVE:
break;
case MmgShoeViewer.SHAPE_OPERATION_ROTATE:
Point2D origin = calculateShapeCenter();
Point2D.Double starting = new Point2D.Double(me.getX(), me.getY());
startingAngle = RotationHelper.getAngle(origin, starting);
setShapeOperation(selectionOperation);
break;
case MmgShoeViewer.SHAPE_OPERATION_RESIZE:
break;
default:
System.out.println(" invalid select operation");
}
}

public void onMouseRelease(MouseEvent me, Rectangle2D canvasBoundary,
int selectionOperation) {

// shape operation can be either resize , rotate , translate ,
switch (selectionOperation) {
case MmgShoeViewer.SHAPE_OPERATION_MOVE:
break;
case MmgShoeViewer.SHAPE_OPERATION_ROTATE:
// FIXME rotation angle computation
setShapeOperation(-1);
break;
case MmgShoeViewer.SHAPE_OPERATION_RESIZE:
break;
default:
System.out.println(" invalid select operation");
}
}

public void rotate(double angle) {
orientation = (float) angle;
}


public void translate(double deltaX, double deltaY) {

position.setLocation(deltaX, deltaY);
lastPosition.setLocation(deltaX, deltaY);
}







// another getter and setter

我正在使用以下方法计算旋转角度

public static double getAngle(Point2D origin, Point2D other) {

double dy = other.getY() - origin.getY();
double dx = other.getX() - origin.getX();
double angle;

if (dx == 0) {// special case
angle = dy >= 0 ? Math.PI / 2 : -Math.PI / 2;
} else {
angle = Math.atan(dy / dx);
if (dx < 0) // hemisphere correction
angle += Math.PI;
}
// all between 0 and 2PI
if (angle < 0) // between -PI/2 and 0
angle += 2 * Math.PI;
return angle;
}

在 Canvas 鼠标监听器的鼠标按下事件中

selectedShape.onMousePress(me, canvasBoundary, shoeViewer
.getShapeOperation());

我只是调用所选形状的 onMousePress 方法

在 Canvas 鼠标监听器的鼠标拖动方法中,我只是调用所选形状的 onMouseDrag 方法,该方法更新旋转角度,正如您从第一类中看到的那样

selectedShape.onMouseDrag(me, canvasBoundary, shoeViewer
.getShapeOperation());

你可以看到单个形状的绘制方法,根据当前变换绘制形状,我从paintComponent调用,如

Iterator<Shoe> shoeIter = shoeShapeMap.values().iterator();

while (shoeIter.hasNext()) {

Shoe shoe = shoeIter.next();
shoe.draw(g2, firstTime);

}

其中shoeShapeMap包含当前 Canvas 上的所有自定义形状。

我在计算角度或确定 anchor 时犯了错误吗?我当前的解决方案通过检查所有条件 [90 度等] 将形状旋转 360 度,如您在上述方法中看到的。

我希望形状围绕其中心旋转而不调整其位置大小?用这个词来说,很难解释,所以请建议我有更好的方法来在这里展示我想要完成的任务吗?

我想我已经提到了与此问题相关的所有内容。如果您有任何疑问,请随时询问我。

我在这里找到了 2 个相关帖子,但我无法从中找到太多信息。

最佳答案

我认为解决方案可能是(或/或):

  • 反转 AffineTransform 上的操作顺序,将平移放在旋转之后
  • 使用 -x 和 -y 作为翻译值

关于java - 自定义形状旋转问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14829090/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com