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

用java绘制一个可以转动的转盘,需要颜色不同,转盘里面包含了各种吃的,转动转盘可以选择不同吃的,任意点击转盘可以转动转盘,时隔一秒停下随机生成结果

发布于 2024-12-08 17:31:55

下面是一个Java Swing程序的示例,实现了一个可以旋转的转盘,并包含各种食物作为选项。点击转盘任意位置将开始旋转转盘,并在1秒后随机停在一个选项上。


import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

public class FoodSpinWheel extends JFrame {
    private static final int SECTION_COUNT = 8; // 转盘分隔为8个扇区
    private static final String[] COLORS = {"#FFC0CB", "#00FFFF", "#800080", "#FFD700", 
                                            "#90EE90", "#FF6347", "#FF4500", "#87CEFA"};
    private static final String[] FOODS = {"汉堡", "披萨", "炸鸡", "寿司", "奶茶", "冰淇淋", "烧烤", "沙拉"};
    private double currentAngle = 90; // 开始时指向正上方

    public FoodSpinWheel() {
        // 窗体设置
        setTitle("食品选择转盘");
        setSize(400, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setLayout(new BorderLayout());
        JPanel panel = new JPanel();
        ImageIcon icon = new ImageIcon("icon_path");  // 可以设置一个icon增加趣味性
        panel.setIcon(icon);
        add(panel); // 添加主要是图形化展示的面板
        FoodPanel foodPanel = new FoodPanel();
        add(foodPanel, BorderLayout.CENTER); // 添加自绘制的转盘面板
        setVisible(true);
    }

    class FoodPanel extends JPanel {
        public FoodPanel() {
            setPreferredSize(new Dimension(400, 400));
            setBackground(Color.GRAY);
            MouseAdapter adapter = new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    // 点击时旋转转盘
                    currentAngle = 0;
                    Timer timer = new Timer(1000, event -> {
                        stopSpinning();
                    });
                    timer.setRepeats(false);
                    timer.start();
                }
            };
            addMouseListener(adapter);
        }

        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            int width = getWidth();
            int height = getHeight();
            int diameter = Math.min(width, height);
            int radius = diameter / 2;

            // 绘制转盘的外部边框
            g2d.drawOval((width - diameter) / 2, (height - diameter) / 2, diameter, diameter);

            // 分割转盘并填充颜色
            int sectionSize = 360 / SECTION_COUNT;
            for (int i = 0; i < SECTION_COUNT; i++) {
                int startAngle = sectionSize * i;
                int endAngle = startAngle + sectionSize;
                g2d.setColor(new Color(Integer.parseInt(COLORS[i].substring(1), 16)));
                g2d.fillArc((width - diameter) / 2, (height - diameter) / 2, diameter, diameter,
                           startAngle, endAngle);
                // 绘制每个扇区的食物名称
                FontMetrics fm = g2d.getFontMetrics();
                String foodName = FOODS[i];
                int stringWidth = fm.stringWidth(foodName);
                int x = width / 2 + (int) (radius * 0.8 * Math.cos(Math.toRadians(currentAngle + sectionSize / 2 + startAngle)));
                int y = height / 2 + (int) (radius * 0.8 * Math.sin(Math.toRadians(currentAngle + sectionSize / 2 + startAngle))) + fm.getAscent() / 2;
                g2d.setColor(Color.BLACK);
                g2d.drawString(foodName, x - stringWidth / 2, y);
            }

            // 绘制指针
            g2d.rotate(Math.toRadians(currentAngle + 270), width / 2.0, height / 2.0);
            g2d.drawLine(width / 2, height / 2 - 10, width / 2, height / 2 + 80);
            g2d.rotate(Math.toRadians(-270 - currentAngle), width / 2.0, height / 2.0);
        }

        private void stopSpinning() {
            this.currentAngle += ThreadLocalRandom.current().nextInt(0, 359) + 720; // 让转盘多转几圈然后停下
            this.repaint();
            while (true) {
                int resultIndex = (int) ((this.currentAngle + 270) % 360 / (360.0 / SECTION_COUNT)); 
                if (
0 条评论

发布
问题

在线
客服