1
これは本当に奇妙なバグで、私はそれを理解できないようです。これを簡単にするために、私は3つのクラスを持っています:Canvasクラス(オブジェクトをGraphics2D.draw()
で描画します)、そしてたくさんのobの家具クラス。最後に、CustomShapeクラスを使用して、既存の他のシェイプに基づいて新しいシェイプを作成できます。しかし、奇妙な場所で形が描かれています。 xの任意のy座標は、図形が描画されている場所と一致しません。あいまいな位置に描画された図形
Closet.java:
public class Closet {
double x, y, width, height, rotation;
Color color;
Closet() {
this.x = X;
this.y = Y;
this.width = 40;
this.height = 40;
this.rotation = 0;
this.color = Color.blue;
}
public Shape getShape() {
GeneralPath closetShape = new GeneralPath();
closetShape.append(new Rectangle2D.Double(0, 0, width, height), false);
closetShape.moveTo(0 , 0);
closetShape.lineTo(width, height);
closetShape.moveTo(0, height);
closetShape.lineTo(width, 0);
// transform:
AffineTransform t = new AffineTransform();
t.translate(x, y);
Rectangle2D umriss = closetShape.getBounds2D();
t.rotate(Math.toRadians(rotation),umriss.getX()+umriss.getWidth()/2,umriss.getY()+umriss.getHeight()/2);
return t.createTransformedShape(closetShape);
}
}
CustomShape.java
public class CustomShape {
double x, y, width, height, rotation;
Color color;
private Closet[] c;
CustomShape(Closet... elements) {
this.m = elements;
GeneralPath path = new GeneralPath();
Closet[] newC = elements.clone();
Arrays.stream(newC).forEach(e -> path.append(e.getShape(), false));
this.x = path.getBounds2D().getX();
this.y = path.getBounds2D().getY();
this.width = path.getBounds2D().getWidth();
this.height = path.getBounds2D().getHeight();
this.rotation = 0;
this.color = Color.blue;
}
public Shape getShape() {
GeneralPath path = new GeneralPath();
Arrays.stream(c).forEach(e -> path.append(e.getShape(), false));
AffineTransform t = new AffineTransform();
t.translate(x, y);
Rectangle2D umriss = path.getBounds2D();
t.rotate(Math.toRadians(rotation),umriss.getX()+umriss.getWidth()/2,umriss.getY()+umriss.getHeight()/2);
return t.createTransformedShape(path);
}
}
シェイプ内の平行移動や回転を行う代わりに、Graphics2Dのtranslate/rotationを変更する方が良いでしょうか?そうすることで、シェイプを一定に保つことができ、毎回新しいシェイプを作成する必要はありません。 – john16384