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();
|
}
|
});
|
}
|
}
|