> Java > java지도 시간 > 본문

Java에서 X축을 중심으로 2D 모양을 회전하고 크기를 조정하는 방법은 무엇입니까?

Patricia Arquette
풀어 주다: 2024-10-28 00:03:29
원래의
951명이 탐색했습니다.

How to Rotate and Scale a 2D Shape Around the X-Axis in Java?

크기 조정을 사용하여 X축을 중심으로 도형을 수직으로 회전

문제:

귀하 x 및 y축이 있는 2D 그래프가 있고 크기 조정 기능을 통합하면서 축을 중심으로 모양을 회전하려고 합니다.

해결책:

이를 달성하려면 회전 및 크기 조정과 같은 변환을 사용하여 모양의 점을 조작할 수 있습니다. 다음은 먼저 고정된 각도로 모양을 회전한 다음 다양한 배율로 크기를 조정하는 예입니다.

<code class="java">import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import javax.swing.*;

public class ShapeRotationScaling extends JPanel implements ActionListener {

    private static final int ANGLE_INCREMENT = 1; // in degrees
    private static final double SCALE_INCREMENT = 0.1;
    private int[] shapeXPoints = {200, 200, 240, 240, 220, 220, 200};
    private int[] shapeYPoints = {200, 260, 260, 240, 240, 200, 200};
    private AffineTransform transform = new AffineTransform();
    private double rotationAngle = 0;
    private double scaleFactor = 1;
    private Timer timer = new Timer(100, this);

    public ShapeRotationScaling() {
        this.setPreferredSize(new Dimension(700, 700));
        this.setBackground(Color.white);
        timer.start();
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        // Rotate the shape around the x-axis
        transform.rotate(Math.toRadians(rotationAngle), this.getWidth() / 2, 0);

        // Scale the shape
        transform.scale(scaleFactor, scaleFactor);

        // Increment the rotation angle and scale factor
        rotationAngle += ANGLE_INCREMENT;
        scaleFactor += SCALE_INCREMENT;

        repaint();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        // Draw the x and y axes
        g2d.drawLine(this.getWidth() / 2, 0, this.getWidth() / 2, this.getHeight());
        g2d.drawLine(0, this.getHeight() / 2, this.getWidth(), this.getHeight() / 2);

        // Apply the transformation to the shape and draw it
        Shape rotatedAndScaledShape = transform.createTransformedShape(new Polygon(shapeXPoints, shapeYPoints, shapeXPoints.length));
        g2d.draw(rotatedAndScaledShape);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Shape Rotation and Scaling");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ShapeRotationScaling sr = new ShapeRotationScaling();
        frame.add(sr);
        frame.pack();
        frame.setVisible(true);
    }
}</code>
로그인 후 복사

위 내용은 Java에서 X축을 중심으로 2D 모양을 회전하고 크기를 조정하는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

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