如何从中心旋转形状以及从屏幕中心旋转形状

How to rotate shape from its center as well as from center of the screen

我使用 drawPolygon() 创建了星形,但我想从星形中心点以及屏幕中心旋转它。

这里是我从中心旋转星星的代码:

@Override
public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    AffineTransform origX = g2d.getTransform();
    AffineTransform newX = (AffineTransform) origX.clone();
    newX.rotate(Math.toRadians(angle), x, y + 62);  // Rotate about center of the star
    g2d.setTransform(newX);
    g2d.drawPolygon(starX, starY, 5);
    g2d.dispose();
    g.dispose();
}

如果我替换:

newX.rotate(Math.toRadians(angle), x, y + 62);

newX.rotate(Math.toRadians(angle), this.getWidth() / 2, this.getHeight() / 2);

我可以从屏幕中心旋转它。

但是,我想同时实现这两种效果。 :就像地球绕着太阳自转一样。

我尝试创建另一个 AffineTransform 对象,但是当我使用 g2d.setTransform(newObj);

设置它时它覆盖了前一个对象

任何建议都会非常有帮助。 谢谢。

这里是我的完整代码,想看的话

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;

public class Star extends Applet implements Runnable{
  private int[] starX;
  private int[] starY;
  private Thread mainThread;
  private int x;
  private int y;
  private int angleFromCenterShape;

  @Override
  public void init() {
    this.setSize(800, 480);
    x = 250;
    y = 150;
    angleFromCenterShape = 0;
    starX = new int[5];
    starY = new int[5];
    mainThread = new Thread(this);
    mainThread.start();
    this.setBackground(Color.BLACK);
  }

  @Override
  public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    AffineTransform origX = g2d.getTransform();
    AffineTransform newX = (AffineTransform) origX.clone();
    newX.rotate(Math.toRadians(angleFromCenterShape), x, y + 65);   //Rotate about center of the star
    g2d.setTransform(newX);
    g2d.setColor(Color.red);
    g2d.fillPolygon(starX, starY, 5);
    g2d.dispose();
    g.dispose();
  }

  @Override
  public void run() {
    while(true){
        angleFromCenterScreen = (angleFromCenterScreen + 1) % 360; // angle loop from 0 to 36o
        initStar();
        try{
            Thread.sleep(30);
        }catch(Exception e){}
        repaint();
    }
  }

  private void initStar(){
    starX[0] = x;
    starX[1] = x - 50;
    starX[2] = x + 75;
    starX[3] = x - 75;
    starX[4] = x + 50;
    starY[0] = y;
    starY[1] = y + 130;
    starY[2] = y + 50;
    starY[3] = y + 50;
    starY[4] = y + 130;
  }
}

我找到了解决我的问题的方法,但这不是一个确切的解决方案。 然而,在同一个 AffineTransform 对象上添加另一个 rotate() 模拟 2 个旋转

这是代码

@Override
  public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    AffineTransform origX = g2d.getTransform();
    AffineTransform newX = (AffineTransform) origX.clone();
    newX.rotate(Math.toRadians(angle), this.getWidth() / 2, this.getHeight() / 2);  // Rotate from the center of screen
    newX.rotate(Math.toRadians(angle), x, y + 65);   // Rotate from the center of the shape
    g2d.setTransform(newX);
    g2d.setColor(Color.red);
    g2d.fillPolygon(starX, starY, 5);
    g2d.dispose();
    g.dispose();
  }