• 智能AI问答!更聪明、更完整、原创作!
  • 一款基于GPT的超级AI机器人,可以回答问题、聊天、创作、写文案!

用java绘制一个可以转动的转盘,需要颜色不同,转盘里面包含了各种吃的,转动转盘可以选择不同吃的,

发布于 2024-12-07 23:24:28

使用Java绘制可以转动的转盘是一个图形用户界面(GUI)编程任务,需要使用AWT(Abstract Window Toolkit)和Swing库来创建。转盘需要一些重要的组件:一个大转盘、颜色分区、食物图案和旋转控制。

以下是一个简单的Java Swing程序来实现这个需求:

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

public class SpinWheel extends JPanel {
    // 总区域数
    private static final int SECTION_COUNT = 6;
    private static final int[][] COLORS = {{255, 0, 0}, {0, 255, 0}, {0, 0, 255}, {255, 255, 0}, {255, 0, 255}, {0, 255, 255}};
    private static final String[] FOODS = {"炸鸡", "汉堡", "披萨", "寿司", "雪糕", "沙拉"};
    private final Timer timer;

    public SpinWheel() {
        this.setPreferredSize(new Dimension(300, 300));
        timer = new Timer(10, new ActionListener() {
            int nextStep = 1;

            public void actionPerformed(ActionEvent e) {
                int currentAngle = getAngle();
                int newAngle = (currentAngle + nextStep) % 360;
                setAngle(newAngle);
            }
        });
    }

    public int getAngle() {
        return (int) (Math.random() * 360) + 90; // Start at 90 degrees offset.
    }

    public void setAngle(int nextStep) {
        double angle = Math.toRadians(nextStep);
        double x = getWidth() / 2;
        double y = getHeight() / 2;
        for (int i = 0; i < SECTION_COUNT; i++) {
            double radian = Math.toRadians(angle + (i * 360 / SECTION_COUNT));
            double sectionX = x + (Math.cos(radian) * (getWidth() / 2 - 20));
            double sectionY = y + (Math.sin(radian) * (getHeight() / 2 - 20));
            repaint();
            timer休息50毫秒。
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Dimension size = getSize();
        int centerX = size.width / 2;
        int centerY = size.height / 2;

        // Draw the backdrop circle
        g.setColor(Color.LIGHT_GRAY);
        g.fillOval(0, 0, size.width, size.height);

        // Draw the sections
        for (int i = 0; i < SECTION_COUNT; i++) {
            Color color = new Color(COLORS[i][0], COLORS[i][1], COLORS[i][2]);
            int startAngle = i * 360 / SECTION_COUNT;
            int arcAngle = 360 / SECTION_COUNT;

            int[] xs = {
                centerX,
                (int) (centerX + Math.cos(Math.toRadians(startAngle)) * (size.width / 2 - 25)),
                (int) (centerX + Math.cos(Math.toRadians(startAngle + arcAngle)) * (size.width / 2 - 25))
            };

            int[] ys = {
                centerY,
                (int) (centerY + Math.sin(Math.toRadians(startAngle)) * (size.height / 2 - 25)),
                (int) (centerY + Math.sin(Math.toRadians(startAngle + arcAngle)) * (size.height / 2 - 25))
            };
            g.setColor(color);
            g.fillArc(centerX - (size.width / 2 - 25), centerY - (size.height / 2 - 25),
                      size.width - 50, size.height - 50, startAngle, arcAngle);

            g.setColor(Color.BLACK);
            FontMetrics fm = g.getFontMetrics();
            String food = FOODS[i];
            int foodX = xs[1] - (fm.stringWidth(food) / 2);
            int foodY = ys[1] - (fm.getHeight() / 4);
            g.drawString(food, foodX, foodY);
        }

        // Draw the spinner pointer
        g.setColor(Color.BLACK);
        g.drawLine(centerX, centerY, centerX, size.height / 2);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("SpinWheel");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new SpinWheel());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        SpinWheel wheel = (SpinWheel) frame.getContentPane().getComponent(0);
        wheel.initTimer();
    }

    private void initTimer() {
        timer.start();
    }
}

以上代码为简化表示,并未包含真实的旋转逻辑和事件处理机制,而是通过

0 条评论

发布
问题

在线
客服