이 작은 직사각형을 원의 둘레로 이동하여 정경처럼 보이고 움직입니다.
암호
private void doDrawing(Graphics g){
g.setColor(Color.BLUE);
g.fillArc(-CANON_RADIUS/2, this.getHeight()-CANON_RADIUS/2, CANON_RADIUS, CANON_RADIUS, 0, 90);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.BLUE);
Rectangle rect = new Rectangle(CANON_RADIUS/2, this.getHeight()-CANON_RADIUS/2, CANON_WIDTH, CANON_HEIGHT);
AffineTransform transform = new AffineTransform();
transform.rotate(Math.toRadians(-60), rect.getX() + rect.width/2, rect.getY() + rect.height/2);
Shape transformed = transform.createTransformedShape(rect);
g2d.fill(transformed);
}
이 코드는 사각형을 중심으로 회전합니다. 원주를 중심으로 사각형을 회전하려면 어떻게해야합니까?
먼저, 이미 사용하고있는 것과 같은 변환 매트릭스를 사용할 수 있습니다. http://en.wikipedia.org/wiki/Transformation_matrix
편집 : 코드를보고 앵커를 중심으로 캐논을 회전하려고합니다. javadocs를 참조하십시오 : http://docs.oracle.com/javase/7/docs/api/java/awt/geom/AffineTransform.html
public void rotate (double theta, double anchorx, double anchory)
첫 번째 인수는 로테이션이고 마지막 두 인수는 대포 기지의 중앙이어야합니다! 예를 들어 screen.height 및 0과 같습니다.
AffineTransform transform = new AffineTransform();
transform.rotate(Math.toRadians(-60), 0, Screen.height);
Shape transformed = transform.createTransformedShape(rect);
g2d.fill(transformed);
두 번째 방법은 회전 된 직사각형의 중앙을베이스 반경 주위로 이동하는 것입니다.
(의사 코드) :
Point p = circle.getPoint();
shape.moveto(p.x-(shape.width/2),p.y-(shape.height/2));
g2d.fill(shape);
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다