用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);
        });
    }
}
相关推荐
lly2024063 小时前
Bootstrap 警告框
开发语言
2601_949146534 小时前
C语言语音通知接口接入教程:如何使用C语言直接调用语音预警API
c语言·开发语言
曹牧4 小时前
Spring Boot:如何测试Java Controller中的POST请求?
java·开发语言
KYGALYX4 小时前
服务异步通信
开发语言·后端·微服务·ruby
zmzb01034 小时前
C++课后习题训练记录Day98
开发语言·c++
爬山算法5 小时前
Hibernate(90)如何在故障注入测试中使用Hibernate?
java·后端·hibernate
kfyty7255 小时前
集成 spring-ai 2.x 实践中遇到的一些问题及解决方案
java·人工智能·spring-ai
猫头虎5 小时前
如何排查并解决项目启动时报错Error encountered while processing: java.io.IOException: closed 的问题
java·开发语言·jvm·spring boot·python·开源·maven
李少兄5 小时前
在 IntelliJ IDEA 中修改 Git 远程仓库地址
java·git·intellij-idea
YUJIANYUE5 小时前
PHP纹路验证码
开发语言·php