用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);
        });
    }
}
相关推荐
UserNamezhangxi27 分钟前
kotlin 协程笔记
java·笔记·kotlin·协程
咖啡里的茶i36 分钟前
数字化图书管理系统设计实践(java)
java·课程设计
疯狂的代M夫1 小时前
C++对象的内存布局
开发语言·c++
九转苍翎1 小时前
Java内功修炼(2)——线程安全三剑客:synchronized、volatile与wait/notify
java·thread
曲莫终1 小时前
正则表达式删除注释和多余换航
java·kotlin
mit6.8241 小时前
Linux下C#项目构建
开发语言·c#
群联云防护小杜1 小时前
从一次 DDoS 的“死亡回放”看现代攻击链的进化
开发语言·python·linq
霸敛1 小时前
好家园房产中介网后台管理完整(python+flask+mysql)
开发语言·python·flask
whitepure1 小时前
万字详解JavaObject类方法
java·后端
Momentary_SixthSense2 小时前
RESP协议
java·开发语言·javascript·redis·后端·python·mysql