"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How can I rotate a shape vertically around the x-axis in Java?

How can I rotate a shape vertically around the x-axis in Java?

Published on 2024-11-17
Browse:506

How can I rotate a shape vertically around the x-axis in Java?

Rotating a shape vertically around the x-axis

The provided code demonstrates how to rotate a polygon, but it does not rotate it around the x-axis. To achieve vertical rotation around the x-axis, we can rotate the polygon by 90 degrees and then apply the desired rotation in radians. Here's the modified code:

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

public class RotatingShape extends JPanel implements ActionListener {

    private int[] p1x = {200, 200, 240, 240, 220, 220, 200};
    private int[] p1y = {200, 260, 260, 240, 240, 200, 200};
    private Polygon p1 = new Polygon(p1x, p1y, p1x.length);
    private double theta = 0;
    private double dt = Math.PI / 36; // Rotation speed
    private Timer timer = new Timer(100, this);

    public RotatingShape() {
        this.setPreferredSize(new Dimension(700, 700));
        this.setBackground(Color.white);
        p1.translate(-50,  100);
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        theta  = dt;
        repaint();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        int w = this.getWidth();
        int h = this.getHeight();
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.drawLine(w / 2, 0, w / 2, h);
        g2d.drawLine(0, h / 2, w, h / 2);
        g2d.rotate(Math.PI / 2, w / 2, h / 2); // Rotate 90 degrees to align with x-axis
        g2d.rotate(theta, w / 2, h / 2); // Apply rotation around x-axis
        g2d.drawPolygon(p1);
    }

    public void start() {
        timer.start();
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Rotating Shape");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        RotatingShape sl = new RotatingShape();
        frame.add(sl);
        frame.pack();
        frame.setVisible(true);
        sl.start();
    }
}

In this code, we rotate the polygon by 90 degrees in the paintComponent method using g2d.rotate(Math.PI / 2, w / 2, h / 2). This aligns the polygon with the x-axis, allowing us to apply the desired rotation in radians using g2d.rotate(theta, w / 2, h / 2).

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3