用java画一个抽奖时用的圆盘,感觉还挺好看的。

用java画一个抽奖时用的圆盘,感觉还挺好看的。请看封面样式,就是样例。不过是随机的。每一次都不一样。

复制代码
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Arc2D;
import java.util.Random;
public class PaintDisc extends JPanel {
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        int centerX = getWidth() / 2;
        int centerY = getHeight() / 2;
        int radius = Math.min(getWidth(), getHeight()) / 2;
        int petalCount = 12;
        double startAngle = 0;
        for (int i = 0; i < petalCount; i++) {
            g2.setColor(new Color(getRandomIntIn255(),getRandomIntIn255(),getRandomIntIn255()));
            double angle = Math.toRadians(startAngle + i * 360.0 / petalCount);
            int x = (int) (centerX + radius * Math.cos(angle));
            int y = (int) (centerY + radius * Math.sin(angle));

            Arc2D arc = new Arc2D.Double(centerX - radius, centerY - radius, radius * 2, radius * 2,
                    startAngle + i * 360.0 / petalCount, 45, Arc2D.PIE);
            g2.fill(arc);
        }
    }
    public static int getRandomIntIn255() {
        Random random = new Random();
        return random.nextInt(256);
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("用java画一个抽奖时用的圆盘");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new PaintDisc());
            frame.setSize(800, 800);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        });
    }
}
相关推荐
masa0106 分钟前
JavaScript--JavaScript基础
开发语言·javascript
拓端研究室TRL9 分钟前
Python用TOPSIS熵权法重构粮食系统及期刊指标权重多属性决策MCDM研究|附数据代码...
开发语言·python·重构
一只特立独行的猪6111 小时前
Java面试——集合篇
java·开发语言·面试
大得3692 小时前
go注册中心Eureka,注册到线上和线下,都可以访问
开发语言·eureka·golang
讓丄帝愛伱2 小时前
spring boot启动报错:so that it conforms to the canonical names requirements
java·spring boot·后端
weixin_586062022 小时前
Spring Boot 入门指南
java·spring boot·后端
小珑也要变强3 小时前
队列基础概念
c语言·开发语言·数据结构·物联网
Dola_Pan5 小时前
Linux文件IO(二)-文件操作使用详解
java·linux·服务器
wang_book5 小时前
Gitlab学习(007 gitlab项目操作)
java·运维·git·学习·spring·gitlab
AI原吾6 小时前
掌握Python-uinput:打造你的输入设备控制大师
开发语言·python·apython-uinput