[英]How to remove original object after rotation in java graphics2D?
具体来说,我试图只显示旋转的对象。 我有一个绘制的矩形并旋转它。
如何仅显示旋转的矩形并处理旧的矩形?
编辑:
以下是旋转矩形的代码:
private void rotateRectangle(Graphics g, Rectangle rect, Color c){
Graphics2D g2d = (Graphics2D) g.create();
x = rect.x;
y = rect.y;
g2d.setColor(c);
g2d.rotate(Math.PI/6, PANEL_WIDTH/2,PANEL_HEIGHT/2);
g2d.drawRect(PANEL_WIDTH/2-x/2, PANEL_HEIGHT/2-y/2, x, y);
}
这是我调用它的paintComponent:
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setStroke(new BasicStroke(3));
//these are declared before
rect.x = x;
rect.y = y;
if(rotateClicked){
rotateRectangle(g2d,rect,squareColor);
rotateClicked = false;
}
drawRectangle(g2d, rect, squareColor);
getArea(x,y);
}
如果通过覆盖paintComponent(Graphics g)
使用 Graphics 对象自定义渲染形状,请确保使用super.paintComponent(g)
作为清除绘图区域的第一行
从那里画你的矩形/旋转矩形
不使用super.paintComponent(g)
,您之前的绘图(未旋转的矩形)将保持可见
随着源代码的更新:您正在绘制新的和旧的矩形,因为您的 if 语句没有 else 子句
尝试插入一个 else 子句,以便它绘制一个或另一个矩形,目前它可能绘制一个旋转的矩形,然后绘制未旋转的矩形
if(rotateClicked)
{
rotateRectangle(g2d,rect,squareColor);
rotateClicked = false;
}
else
drawRectangle(g2d, rect, squareColor);
您可能会也可能不希望在 else 中使用rotateClicked = true
以便它会在旋转和未旋转之间来回翻转
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.