简体   繁体   中英

How to use a simple way to rotate a rectangle or triangle in java?

I'm currently working on a project with the theme of earth hour, and we are only allowed to use rectangles, circles and triangles. Here's the image i'm tring to create (not exactly, mine will be much more simplified!):

https://www.google.com/search?q=earth+hour&biw=1366&bih=586&source=lnms&tbm=isch&sa=X&ved=0ahUKEwj__5H0vtvQAhXLrlQKHTi8BagQ_AUIBygC#imgrc=fQkBxn0a8LnwbM%3A (not sure if you could see the link)

But when i'm coding it, i'm running into trouble to rotate those rectangles to stand on the tangent line of the circle. I'm a student just learnt some basics of java, like loops and arrays. So my quesiton is that if there's some understandable way that doesn't involve some complex and exotic methods that could rotate those rectangles? I know it will probably involve some complicated solutions that is beyond my knowledge. But any help is much appreciated.

this is part of the code that i build the building standing perpendicularly to the circle(earth):

 // create mid buildings
Color blc = new Color(0, 0, 0);
Rectangle midBld = new Rectangle(240, 220, 20, 40, blc);
midBld.draw(g);
Rectangle midBld1 = new Rectangle(242, 190, 16, 30, blc);
midBld1.draw(g);
Triangle midBld2 = new Triangle(250, 160, 8, 30, blc);
midBld2.draw(g);
Triangle midBld3 = new Triangle(250, 160, -8, 30, blc);
midBld3.draw(g);

A Rectangle cannot be rotated, its edges are always in parallel to the axis. But you can rotate and translate the coordinate system in witch you draw the shapes. From Graphics2D API doc.

All coordinates passed to a Graphics2D object are specified in a device-independent coordinate system called User Space, which is used by applications. The Graphics2D object contains an AffineTransform object as part of its rendering state that defines how to convert coordinates from user space to device-dependent coordinates in Device Space.

Graphics2D also provide two methods that are useful in this task: translate that moves the origin of the coordinates and rotate that, well, rotates the system.

package graphics;

import javax.swing.*;
import java.awt.*;

/**
 * Earth Hour
 */
public class RotateRect extends JFrame {

    private static final int WIDTH = 400;
    private static final int HEIGHT = 400;

    public RotateRect() {
        this.setSize(WIDTH, HEIGHT);
        this.setTitle("Rotate Rectangles");
        this.setContentPane(new JPanel() {
            @Override
            public void paint(Graphics g) {
                Graphics2D g2 = (Graphics2D) g;

                // Background: White
                g2.setColor(Color.WHITE);
                g2.fillRect(0, 0, this.getWidth(), this.getHeight());

                // Draw "Earth": Center(200, 400), Radius=200
                g2.setColor(Color.BLACK);
                g2.fillOval(0, 200, 400, 400);

                // Move origin to center of the canvas (surface of earth)
                g2.translate(200, 200);
                // Rotate the coordinate system, relative to the center of earth.
                // note x, y are in the translated system
                // Transforms are accumulative
                g2.rotate(-Math.PI/6, 0, 200);
                // Fill a rectangle with top-left corner at (-20, 80) in the rotated system
                // It's important to make the rectangle symmetrical to the y-axis, otherwise the building looks
                // funny.
                // Also, make the building "sunk" a little, so that it's fully on the ground.
                g2.fillRect(-20, -80, 40, 100);

                g2.rotate(Math.PI/3, 0, 200);
                g2.fillRect(-20, -80, 40, 100);

                g2.rotate(-Math.PI/6, 0, 200);
                g2.fill(new Rectangle(-20, -80, 40, 100));
            }
        });
    }

    public static void main(String [] args) {
        RotateRect rr = new RotateRect();
        rr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        EventQueue.invokeLater(()->rr.setVisible(true));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM