Vladimir Stazhilov
Vladimir Stazhilov

Reputation: 1954

Draw a rotating rectangle on applet

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

Upvotes: 0

Views: 1769

Answers (1)

Hidde
Hidde

Reputation: 11931

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 :)

Upvotes: 2

Related Questions