用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);
        });
    }
}
相关推荐
皮皮林5515 小时前
Java性能调优黑科技!1行代码实现毫秒级耗时追踪,效率飙升300%!
java
冰_河5 小时前
QPS从300到3100:我靠一行代码让接口性能暴涨10倍,系统性能原地起飞!!
java·后端·性能优化
桦说编程8 小时前
从 ForkJoinPool 的 Compensate 看并发框架的线程补偿思想
java·后端·源码阅读
躺平大鹅10 小时前
Java面向对象入门(类与对象,新手秒懂)
java
初次攀爬者11 小时前
RocketMQ在Spring Boot上的基础使用
java·spring boot·rocketmq
花花无缺11 小时前
搞懂@Autowired 与@Resuorce
java·spring boot·后端
Derek_Smart12 小时前
从一次 OOM 事故说起:打造生产级的 JVM 健康检查组件
java·jvm·spring boot
NE_STOP13 小时前
MyBatis-mybatis入门与增删改查
java
孟陬16 小时前
国外技术周刊 #1:Paul Graham 重新分享最受欢迎的文章《创作者的品味》、本周被划线最多 YouTube《如何在 19 分钟内学会 AI》、为何我不
java·前端·后端