0

I want to rotate the rectangle around one of corner, but I don't now how determine the new coordinates of corners. Rotation may be around any corner. May be exists another method of rotation?

Can someone help me?

screenshot of my applet

import java.applet.*;
import java.awt.*;

public class MainApplet extends Applet implements Runnable {

int width, height;
int i = 0;
Thread t = null;
boolean threadSuspended;

public void init() {
    width = getSize().width;
    height = getSize().height;
    setBackground(Color.black);
}

public void destroy() {}


public void start() {
    if (t == null) {
        t = new Thread(this);
        threadSuspended = false;
        t.start();
    } else {
        if (threadSuspended) {
            threadSuspended = false;
            synchronized (this) {
                notify();
            }
        }
    }
}

public void stop() {
    threadSuspended = true;
}

public void run() {
    try {
        while (true) {
            ++i;
            if (i == 359) {
                i = 0;
            }
            showStatus("i is " + i);

            if (threadSuspended) {
                synchronized (this) {
                    while (threadSuspended) {
                        wait();
                    }
                }
            }
            repaint();
            t.sleep(100);  // interval given in milliseconds
        }
    } catch (InterruptedException e) {
    }
}

public void paint(Graphics g) {
    g.setColor(Color.green);

    g.drawRect(200,150, (int) (50*Math.cos(i)-100*Math.sin(i)+200-200*Math.cos(i)+150*Math.sin(i)),
                        (int) (50*Math.sin(i)+100*Math.cos(i)+150-200*Math.sin(i)-150*Math.cos(i)));
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Sergo
  • 15
  • 1
  • 4
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson May 23 '16 at 04:16
  • It was task from teacher. – Sergo May 24 '16 at 07:09
  • *"It was task from teacher."* OK. Give them that link.. – Andrew Thompson May 24 '16 at 07:51

1 Answers1

1

You can use Graphics2D's .rotate(theta, double x, double y) method.

public void paint(Graphics g){
  //Create Graphics2D object:
  Graphics2D g2d = (Graphics2D) g.create();

  //Create rectangle of origin (0,0), w=30, h=50
  Rectangle rectangle = new Rectangle();
  rectangle.setBounds(0,0,30,50);

  //Rotate rectangle by 1 radian(Math.PI) from the bottom corner
  g2d.rotate(Math.PI, rectangle.x + rectangle.width/2, rectangle.y + rectangle.height/2);

  //Draw rectangle
  g2d.draw(rectangle);
}
Arman
  • 655
  • 2
  • 7
  • 23
  • @Sergo I added comments to explain how this works. If you replace your "paint" method with the one I wrote, then you will see that the rectangle can rotate. Keep in mind ".rotate(theta, x, y)" theta is in radians, not degrees. – Arman May 22 '16 at 22:52
  • I replaced, but he don't move. Do you made another changes? – Sergo May 23 '16 at 07:38
  • public void paint(Graphics g){ Graphics2D g2d = (Graphics2D) g.create(); g2d.setColor(Color.green); Rectangle rectangle = new Rectangle(); rectangle.setBounds(100,100,50,70); g2d.rotate(Math.PI*0.00555*i, rectangle.x, rectangle.y); g2d.draw(rectangle); } //and change sleep(10) – Sergo May 23 '16 at 08:03
  • Thankyou for help :) – Sergo May 23 '16 at 08:05