> Java > java지도 시간 > 본문

Java에서 x축을 중심으로 도형을 수직으로 회전하려면 어떻게 해야 합니까?

DDD
풀어 주다: 2024-10-27 21:35:30
원래의
279명이 탐색했습니다.

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

x축을 중심으로 도형을 수직으로 회전

제공된 코드는 다각형을 회전하는 방법을 보여주지만 회전하지는 않습니다. x축. x축을 중심으로 수직 회전을 달성하려면 다각형을 90도 회전한 다음 원하는 회전을 라디안 단위로 적용하면 됩니다. 수정된 코드는 다음과 같습니다.

<code class="java">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();
    }
}</code>
로그인 후 복사

이 코드에서는 g2d.rotate(Math.PI / 2, w / 2, h / 2)를 사용하여 PaintComponent 메서드에서 다각형을 90도 회전합니다. 이렇게 하면 다각형이 x축에 정렬되어 g2d.rotate(theta, w/2, h/2)를 사용하여 원하는 회전을 라디안 단위로 적용할 수 있습니다.

위 내용은 Java에서 x축을 중심으로 도형을 수직으로 회전하려면 어떻게 해야 합니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!