在 java 中在同一轴上旋转形状

Rotate a shape on the same axis in java

我下面有这个程序。我希望程序在同一点上旋转形状,并多次旋转矩形。然而,程序只旋转矩形一次并将旋转后的形状放在框架的另一个位置上。我真的需要帮助。谢谢!!!!*

import javax.swing.JPanel;
import java.awt.*;
import java.util.Random;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.AffineTransform;

public class BoxesPanel extends JPanel
{
   private boolean drawRect = false, drawCircle = false, Repaint = false;
   private JButton enter; 
   int count = 0;

   //------------------------------------------------------------------
   //   Sets up the drawing panel
   //------------------------------------------------------------------
   public BoxesPanel(int num)
   {
      if(num == 1)
         drawRect = true;
      else if(num == 2)
         drawCircle = true; 

      enter = new JButton("click");
      enter.addActionListener(new ButtonListener());

      add(enter);

      setBackground(Color.gray);
      setPreferredSize(new Dimension(400, 400));
   }

   private class ButtonListener implements ActionListener
   {
      public void actionPerformed (ActionEvent event)
      {
         System.out.println("Rotate has been clicked: " + Repaint+ " " + count);
         Repaint = true;
         repaint();  
      }
   }   

   public void paintComponent(Graphics page)
   {
      super.paintComponent(page);
      Graphics2D g2d = (Graphics2D)page;

      int x, y, width, height;

      x = 100;
      y = 100;

      width = 100;
      height = 100;

      if(Repaint)
         g2d.rotate(Math.toRadians(45), (x+width)/2, (y+height)/2);

      g2d.setColor(Color.yellow);
      AffineTransform old = g2d.getTransform();

      if(drawRect)
         g2d.fillRect(x,y,width,height);
      else if(drawCircle)
         g2d.fillOval(x,y,width,height);

      g2d.setTransform(old);

      System.out.println("Painted: " + Repaint+ " " + count);
      Repaint = false;
   }
}   


import javax.swing.JFrame;

public class Boxes
{
   //---------------------------------------------------------
   //    Creates the main frame of the program.
   //---------------------------------------------------------
   public static void main(String[] args)
   {
      JFrame frame = new JFrame ("Boxes");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      BoxesPanel panel = new BoxesPanel(1);

      frame.getContentPane().add(panel);
      frame.pack();
      frame.setVisible(true);
   }
}

我做了一些修改。它现在似乎可以工作了。

public class BoxesPanel extends JPanel
{
   private boolean drawRect = false, drawCircle = false;
   private int angle = 0;
   private JButton enter; 
   int count = 0;

   //------------------------------------------------------------------
   //   Sets up the drawing panel
   //------------------------------------------------------------------
   public BoxesPanel(int num)
   {
      if(num == 1)
         drawRect = true;
      else if(num == 2)
         drawCircle = true; 

      enter = new JButton("click");
      enter.addActionListener(new ButtonListener());

      add(enter);

      setBackground(Color.gray);
      setPreferredSize(new Dimension(400, 400));
   }

   private class ButtonListener implements ActionListener
   {
      public void actionPerformed (ActionEvent event)
      {
         System.out.println("Rotate has been clicked: " + count);
         count++;
         angle = (angle + 45)%360;
         repaint();  
      }
   }   

   public void paintComponent(Graphics page)
   {
      super.paintComponent(page);
      Graphics2D g2d = (Graphics2D)page;

      int x, y, width, height;

      x = 100;
      y = 100;

      width = 100;
      height = 100;

      AffineTransform old = g2d.getTransform();

      g2d.rotate(Math.toRadians(angle), x+width/2, y+height/2);

      g2d.setColor(Color.yellow);

      if(drawRect)        
         g2d.fillRect(x,y,width,height);
      else if(drawCircle)
         g2d.fillOval(x,y,width,height);

      g2d.setTransform(old);

      System.out.println("Painted: " + count);
   }
}