0

I have this program below. I want the program to rotate the shape on the same spot and also rotate the rectangle more than once. However, the program only rotates the rectangle once and puts the rotated shape on another spot on the frame. I really need help. Thanks!!!!*

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);
   }
}
M_Row
  • 109
  • 3
  • 12
  • Something like [this](http://stackoverflow.com/questions/27260445/rotating-a-triangle-around-a-point-java/27260788#27260788) and/or [this](http://stackoverflow.com/questions/14884480/rotate-an-image-in-java-by-the-specified-angle/14885645#14885645)? – MadProgrammer Mar 11 '15 at 04:03
  • Separate the logic from the implementation. The shape and it's translation should be separate from the physical painting of those shapes – MadProgrammer Mar 11 '15 at 04:06
  • Painting is destructive, anything painted by the `paintComponent` will be removed the next time `paintComponent` is called, so you need to restore the current state the component. The `Shape`s you want to render and there transformations should be maintained separately from the `paintComponent` method in order to allow you to repaint them repeatedly. Painting may occur for many different reasons, most of which you don't control – MadProgrammer Mar 11 '15 at 04:11

1 Answers1

0

I made a few changes. It seems to work now.

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);
   }
}
Tesseract
  • 8,049
  • 2
  • 20
  • 37
  • I tried this for a regular and irregular polygons and it did not work like it did for circle, ellipse and rectangle. Is there something I need to do to make it work for those? – M_Row Mar 15 '15 at 01:32