我对Java的经验很少,而且我是一个业余程序员。所以请注意我的词汇。
我希望能够将静态矩形粘贴在旋转矩形的顶部。 到目前为止,当我尝试添加另一个对象时,它会与另一个图像一起旋转。我尝试将旋转设置为零,但这似乎不起作用。我还尝试创建另一个类来单独绘制组件并使用frame.add将它们添加到框架中。我还尝试为 Draw 类创建另一个对 GUI 没有影响的部分。这是我当前的绘图类(class)。如有任何帮助,我们将不胜感激。
class DrawRectangle extends JPanel {
@Override
public void paintComponent(Graphics g) {
int h = this.getHeight();
int w = this.getWidth();
Graphics2D g2 = (Graphics2D) g;
//draw background
g2.setColor(Color.WHITE);
g2.fillRect(0, 0, w, h);
//draw roatiing rectangle
g2.setColor(Color.CYAN);
Rectangle rRec = new Rectangle(w / 4, h / 4, 2 * w / 4, 2 * h / 4);
double wr = rRec.getX() + rRec.getWidth() / 2;
double hr = rRec.getY() + rRec.getHeight() / 2;
g2.rotate(Math.toRadians(count), wr, hr);
g2.fill(rRec);
g2.fillRect(w / 3, h / 3, 2 * w / 3, 2 * h / 3);
}
public void paintComponent2(Graphics g) {
int h = this.getHeight();
int w = this.getWidth();
Graphics2D g2 = (Graphics2D) g;
}
}
最佳答案
So far when I try to add another object it spins with the other image.
创建一个单独的 Graphics 对象来执行旋转,这样就不会影响传递到绘画方法中的 Graphics 对象的属性:
//Graphics2D g2 = (Graphics2D) g;
Graphics2D g2 = (Graphics2D)g.create();
// painting code
g2.dispose();
关于Java 动画旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32660354/