0

I have to make a rotating rectangle on my applet, how is it done? The rectangle should rotate around one of its conner on the plane. This is what I have so far:

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JApplet;

public class MainApplet extends JApplet {
    Font bigFont;
     Color redColor; 
     Color weirdColor; 
     Color bgColor;

    @Override
     public void init()  
     { 
          bigFont = new Font("Arial",Font.BOLD,16);
          redColor = Color.red;
          weirdColor = new Color(60,60,122);
      setBackground(bgColor);
     }

    @Override
     public void stop() { }

    @Override
     public void paint(Graphics g)  
     { 
      g.setFont(bigFont); 
      g.drawString("Shapes and Colors",80,20);     
      g.setColor(redColor);
      g.drawRect(100,100,100,100);
      g.fillRect(100,100,100,100);
     }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Vladimir Stazhilov
  • 1,956
  • 4
  • 31
  • 63
  • 1
    Duplicate: http://stackoverflow.com/questions/7517688/rotate-a-java-graphics2d-rectangle – zeller May 16 '12 at 16:57
  • Look into using an AffineTransform to do this. Check out its [API](http://docs.oracle.com/javase/7/docs/api/java/awt/geom/AffineTransform.html) and you'll see that it has a static method that returns a transform specifically for rotation. Just be sure to convert any degrees into radians before doing the transform. You will also need to use a [Swing Timer](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) to drive the animation. – Hovercraft Full Of Eels May 16 '12 at 16:57

1 Answers1

2

I am not going to write your applet for you, but I'll give you some steps to get you started:

In your init:

  • Set a timer, that calls the refresh method every time.
  • Set a global counter to 0

In your refresh method:

  • Increase the counter by 1 (possibly mod 360 to keep it in the 0-359 range)
  • Call the repaint method

In your paint method:

  • Turn the Canvas the number of degrees the counter is on (possibly using an AffineTransform object)
  • Paint your image/square/shape/anything

Good luck :)

Hidde
  • 11,493
  • 8
  • 43
  • 68