title: How can I rotate rectangle in Java?
body: <p>My rectangle code: </p>
  
  <pre><code>class Rectangle extends JPanel {
  
   int x = 105;
   int y= 100;
   int width = 50;
   int height = 100;
  
  
    public void paint(Graphics g) {
      g.drawRect (x, y, width, height);  
      g.setColor(Color.WHITE);
    }
  
  
  Rectangle r = new Rectangle();
  </code></pre>
  
  <p>and I have a button "rotate". When the user presses the button with the mouse, the rectangle must rotate 15 degrees.</p>
  
  <p>This is my action code:</p>
  
  <pre><code>public void actionPerformed(ActionEvent e){
      Object source  = e.getSource();
  
      if( source == rotate){
          AffineTransform transform = new AffineTransform();
          transform.rotate(Math.toRadians(15), r.getX() + r.getWidth()/2, r.getY() + height/2);
          r.add(transform);
      }
  }
  </code></pre>
  
  <p>But the code doesn't work. I don't know why? What do you think?</p>
  
  <p>My edited-action-code part: </p>
  
  <pre><code>public void actionPerformed(ActionEvent e){
          Object source  = e.getSource();
  
          if( source == rotate){
  
              Paint p = new Paint();              
              panel1.add(r);
               repaint();
          }
          }
  
      class Paint extends JPanel {
      public void paintComponent(Graphics g)
      {
          super.paintComponent(g);
          Graphics2D g2d = (Graphics2D)g;
  
          g2d.setColor(Color.WHITE);
          g2d.translate(r.getX()+(r.WIDTH/2), r.getY()+(r.HEIGHT/2));
          g2d.rotate(Math.toRadians(15));
          r.equals(g2d);
          repaint();
      }
  }
  </code></pre>