2017-02-24 3 views
1

이것은 정말 이상한 버그이며 그것을 이해하지 못하는 것 같습니다. 이를 단순화하기 위해 캔버스 클래스 (객체를 Graphics2D.draw()으로 그리는 클래스)와 많은 가구 클래스 (가구 클래스를 하나씩 볼 수 있기 때문에 모두가 다른 모양을 반환합니다)가 있습니다. 마지막으로 커스텀 셰이프 클래스가있어서 기존의 다른 셰이프를 기반으로 새로운 셰이프를 만들 수 있습니다. 그러나 모양은 별난 곳에서 그려지고 있습니다. 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); 
} 
} 
+0

셰이프 내부에서 변환 및 회전을 수행하는 대신 Graphics2D translate/rotation을 변경하는 것이 더 좋지 않습니까? 그렇게하면 모양이 일정하게 유지되고 매번 새로운 모양을 만들 필요가 없습니다. – john16384

답변

1

나는 해결책을 발견 : 나는 CustomShape에 추가하기 전에 이러한 모양을 변형했다. 그래서 x와 y 좌표가 잘못되었습니다.