。窗口中放置“顺转”和“逆转”两个按钮,当单击按钮时,将椭圆每次旋转30。。

窗口中放置“顺转”和“逆转”两个按钮,当单击按钮时,将椭圆每次旋转30 javafx

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

public class MyPanel extends JPanel {
   private int angle = 0;
   public void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2d = (Graphics2D) g;
      g2d.rotate(Math.toRadians(angle), getWidth()/2, getHeight()/2);
      g2d.drawOval(50, 50, 100, 150);
   }
   public void setAngle(int angle) {
      this.angle = angle;
   }
}

public class RotationFrame extends JFrame {
   public RotationFrame() {
      super("椭圆旋转");
      MyPanel panel = new MyPanel();
      JButton clockwiseButton = new JButton("顺转");
      clockwiseButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            panel.setAngle(panel.angle + 30);
            panel.repaint();
         }
      });
      JButton anticlockwiseButton = new JButton("逆转");
      anticlockwiseButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            panel.setAngle(panel.angle - 30);
            panel.repaint();
         }
      });
      JPanel buttonPanel = new JPanel();
      buttonPanel.add(clockwiseButton);
      buttonPanel.add(anticlockwiseButton);
      Container contentPane = getContentPane();
      contentPane.add(panel, BorderLayout.CENTER);
      contentPane.add(buttonPanel, BorderLayout.SOUTH);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setSize(300, 300);
   }
   public static void main(String[] args) {
      JFrame frame = new RotationFrame();
      frame.setVisible(true);
   }
}

在上面的代码中,MyPanel类扩展自JPanel并在其内部绘制旋转的椭圆。setAngle方法用于设置椭圆的旋转角度。在RotationFrame类的构造函数中,创建了两个按钮,并分别注册了它们的ActionListener。该方法接收按钮单击事件,更改椭圆的旋转角度,并使用repaint方法在MyPanel上重新绘制椭圆。最后,将MyPanel和按钮面板添加到窗口的ContentPane中,并设置了窗口的大小和关闭操作。


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

public class EllipseRotationExample extends JFrame implements ActionListener {
    private JPanel panel;
    private JButton clockwiseButton;
    private JButton anticlockwiseButton;
    private int rotationAngle;

    public EllipseRotationExample() {
        setTitle("Ellipse Rotation Example");
        setSize(400, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        panel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g;
                int width = getWidth();
                int height = getHeight();
                int centerX = width / 2;
                int centerY = height / 2;

                // Create a new ellipse
                Ellipse2D ellipse = new Ellipse2D.Double(centerX - 100, centerY - 50, 200, 100);

                // Rotate the ellipse
                g2d.rotate(Math.toRadians(rotationAngle), centerX, centerY);

                // Draw the rotated ellipse
                g2d.setColor(Color.BLUE);
                g2d.fill(ellipse);
            }
        };

        clockwiseButton = new JButton("顺转");
        anticlockwiseButton = new JButton("逆转");

        clockwiseButton.addActionListener(this);
        anticlockwiseButton.addActionListener(this);

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(clockwiseButton);
        buttonPanel.add(anticlockwiseButton);

        setLayout(new BorderLayout());
        add(panel, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == clockwiseButton) {
            rotationAngle += 30;
        } else if (e.getSource() == anticlockwiseButton) {
            rotationAngle -= 30;
        }

        // Redraw the panel to reflect the new rotation
        panel.repaint();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new EllipseRotationExample().setVisible(true);
            }
        });
    }
}

此代码创建了一个继承自JFrame的类EllipseRotationExample,它包含一个绘制面板panel、两个按钮clockwiseButton和anticlockwiseButton以及一个旋转角度变量rotationAngle。在面板的paintComponent方法中,通过Graphics2D类的rotate方法来旋转椭圆。单击按钮时,旋转角度增加或减少30度,并调用repaint方法重新绘制面板来更新椭圆的旋转。
要运行这个示例,你需要确保你的开发环境中有Java Swing库。