cy
2022-06-21 129904537f66509f97b285e7eb4f42b3dc349dd0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package cn.ksource.test;
 
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Point;
import java.awt.RadialGradientPaint;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.GeneralPath;
import java.awt.geom.Point2D;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Set;
 
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
 
@SuppressWarnings("serial")
public class Star extends JPanel {
    private Map<Shape, Point> stars = new HashMap<Shape, Point>();
    private Random rand = new Random(System.currentTimeMillis());
 
    public Star() {
        this.stars.put(createStar(5, 10, 15), new Point(40, 40));
 
        this.setFocusable(true);
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                int count = 2 + rand.nextInt(15);
                double innerRadius = 5 + rand.nextInt(20);
                double outerRadius = innerRadius + 10 + rand.nextInt(30);
                GeneralPath s = createStar(count, innerRadius, outerRadius);
                stars.put(s, new Point(e.getX(), e.getY()));
                repaint();
            }
        });
 
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_R) {
                    stars.clear();
                    repaint();
                }
            }
        });
    }
 
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
 
        // Paint a gradient for the sky
        GradientPaint background = new GradientPaint(0f, 0f, Color.GRAY.darker(), 0f, getHeight(),
            Color.GRAY.brighter());
        g2d.setPaint(background);
        g2d.fillRect(0, 0, getWidth(), 4 * getHeight() / 5);
 
        // Paint a gradient for the ground
        background = new GradientPaint(0f, 4 * getHeight() / 5, Color.BLACK, 0f, getHeight(),
            Color.GRAY.darker());
        g2d.setPaint(background);
        g2d.fillRect(0, 4 * getHeight() / 5, getWidth(), getHeight() / 5 + 4);
 
        // Using a radial gradient paint
        float[] fractions = new float[] { 0.0f, 0.40f };
        Color[] colors = new Color[] { Color.WHITE, new Color(0.0f, 0.0f, 0.0f, 0.5f) };
        Paint p = new RadialGradientPaint(0, 0, 100, fractions, colors);
        g2d.setPaint(p);
 
        // Show the stars
        Set<Map.Entry<Shape, Point>> items = stars.entrySet();
        for (Map.Entry<Shape, Point> s : items) {
            g2d.translate(s.getValue().getX(), s.getValue().getY());
            g2d.fill(s.getKey());
            g2d.translate(-s.getValue().getX(), -s.getValue().getY());
        }
    }
 
    // 创建一个星形路径
    public static GeneralPath createStar(int count, double innerRadius, double outerRadius) {
        // 因为swing中缀坐标向下,所以正上方的中点坐标是负值
        GeneralPath star = new GeneralPath();
        double extentAngle = 360.0 / count;
 
        Point2D op = new Point2D.Double(0, -outerRadius);
        Point2D ip = GeometryUtil.rotate(0, -innerRadius, extentAngle / 2);
        star.moveTo(op.getX(), op.getY());
        star.lineTo(ip.getX(), ip.getY());
 
        for (int i = 0; i < count - 1; ++i) {
            op = GeometryUtil.rotate(op.getX(), op.getY(), extentAngle);
            star.lineTo(op.getX(), op.getY());
 
            ip = GeometryUtil.rotate(ip.getX(), ip.getY(), extentAngle);
            star.lineTo(ip.getX(), ip.getY());
        }
        star.closePath();
 
        return star;
    }
 
    private static void createGuiAndShow() {
        JFrame frame = new JFrame("Star");
        frame.getContentPane().add(new Star());
 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(550, 400);
        frame.setAlwaysOnTop(true);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
 
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                createGuiAndShow();
            }
        });
    }
}