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);
}
}