001: package snow.utils.gui;
002:
003: import java.util.*;
004: import java.awt.*;
005: import java.awt.image.*;
006: import java.awt.geom.*;
007: import javax.swing.*;
008:
009: public class SnowIcon implements Icon {
010: int width, height;
011:
012: double[][] kochCoord2 = { { 0, 0 }, { 0.0555556, 0.096225 },
013: { 0, 0.19245 }, { 0.111111, 0.19245 },
014: { 0.166667, 0.288675 }, { 0.166667, 0.288675 },
015: { 0.111111, 0.3849 }, { 0, 0.3849 },
016: { 0.0555556, 0.481125 }, { 0, 0.57735 }, { 0, 0.57735 },
017: { 0.111111, 0.57735 }, { 0.166667, 0.673575 },
018: { 0.222222, 0.57735 }, { 0.333333, 0.57735 },
019: { 0.333333, 0.57735 }, { 0.388889, 0.673575 },
020: { 0.333333, 0.7698 }, { 0.444444, 0.7698 },
021: { 0.5, 0.866025 }, { 0.5, 0.866025 }, { 0.5, 0.866025 },
022: { 0.5, 0.866025 }, { 0.5, 0.866025 }, { 0.5, 0.866025 },
023: { 0.5, 0.866025 }, { 0.555556, 0.7698 },
024: { 0.666667, 0.7698 }, { 0.611111, 0.673575 },
025: { 0.666667, 0.57735 }, { 0.666667, 0.57735 },
026: { 0.777778, 0.57735 }, { 0.833333, 0.673575 },
027: { 0.888889, 0.57735 }, { 1., 0.57735 }, { 1., 0.57735 },
028: { 0.944444, 0.481125 }, { 1., 0.3849 },
029: { 0.888889, 0.3849 }, { 0.833333, 0.288675 },
030: { 0.833333, 0.288675 }, { 0.888889, 0.19245 },
031: { 1., 0.19245 }, { 0.944444, 0.096225 }, { 1, 0 },
032: { 1, 0 }, { 1, 0 }, { 1., 0 }, { 1, 0 }, { 1, 0 },
033: { 1, 0 }, { 0.888889, 0 }, { 0.833333, -0.096225 },
034: { 0.777778, 0 }, { 0.666667, 0 }, { 0.666667, 0 },
035: { 0.611111, -0.096225 }, { 0.666667, -0.19245 },
036: { 0.555556, -0.19245 }, { 0.5, -0.288675 },
037: { 0.5, -0.288675 }, { 0.444444, -0.19245 },
038: { 0.333333, -0.19245 }, { 0.388889, -0.096225 },
039: { 0.333333, 0 }, { 0.333333, 0 }, { 0.222222, 0 },
040: { 0.166667, -0.096225 }, { 0.111111, 0 }, { 0, 0 } };
041:
042: final GeneralPath flock;
043:
044: public SnowIcon(int w, int h) {
045: width = w;
046: height = h;
047:
048: flock = new GeneralPath();
049: flock.moveTo(0, 0);
050:
051: int step = (w < 10 ? 5 : 1);
052: for (int i = 0; i < kochCoord2.length; i += step) {
053: flock.lineTo((float) kochCoord2[i][0],
054: (float) kochCoord2[i][1]);
055: }
056: flock.lineTo(0f, 0f);
057:
058: float sx = 0.2f;
059: float sy = 0.12f;
060: float f = 0.61f;
061:
062: flock.moveTo((float) kochCoord2[0][0] * f + sx,
063: (float) kochCoord2[0][1] * f + sy);
064: for (int i = kochCoord2.length - 1; i >= 0; i -= step) {
065: flock.lineTo((float) kochCoord2[i][0] * f + sx,
066: (float) kochCoord2[i][1] * f + sy);
067: }
068: flock.lineTo((float) kochCoord2[0][0] * f + sx,
069: (float) kochCoord2[0][1] * f + sy);
070:
071: }
072:
073: public int getIconHeight() {
074: return height;
075: }
076:
077: public int getIconWidth() {
078: return width;
079: }
080:
081: public void paintSnowFlake(Graphics2D g2, double x, double y,
082: double size) {
083: Color bgColor = UIManager.getColor("Panel.background");
084: if (bgColor == null) {
085: bgColor = new Color(250, 170, 150); // give it a green color to show
086: } // that the key was invalid.
087: Color col2 = SnowBackgroundPanel
088: .createSmallLighterOrDarkerColor(bgColor);
089: paintSnowFlake(g2, col2, bgColor, x, y, size);
090: }
091:
092: public void paintSnowFlake(Graphics2D g2, Color c1, Color c2,
093: double x, double y, double size) {
094: AffineTransform oldTransform = g2.getTransform();
095: RenderingHints oldHints = g2.getRenderingHints();
096: Paint oldPaint = g2.getPaint();
097:
098: g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
099: RenderingHints.VALUE_ANTIALIAS_ON);
100: AffineTransform at = new AffineTransform(size, 0, 0, size, x, y);
101: g2.transform(at);
102:
103: g2.setColor(Color.black);
104: //g2.setStroke(new BasicStroke( (float)(Math.sqrt(size)*0.002), BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND ));
105:
106: g2.setPaint(new GradientPaint(0f, 0f, c1, // new Color(240,240,255,200),
107: .9f, .9f, c2 // new Color(220,220,255,200)));
108: ));
109: g2.fill(flock);
110:
111: g2.setTransform(oldTransform);
112: g2.setRenderingHints(oldHints);
113: g2.setPaint(oldPaint);
114: }
115:
116: public void paintIcon(Component c, Graphics g, int x, int y) {
117: Graphics2D g2 = (Graphics2D) g;
118: // back
119: g2.setColor(Color.white);
120: g2.fill(g2.getClip());
121:
122: // flake
123: paintSnowFlake(g2, x + width / 10, y + width / 3.7, width * 0.8);
124: g2.draw(new Rectangle2D.Double(x, y, width, height));
125: }
126:
127: public BufferedImage getIconAsImage() {
128: BufferedImage bim = new BufferedImage(width, height,
129: BufferedImage.TYPE_INT_ARGB);
130: Graphics2D g2 = bim.createGraphics();
131: this .paintSnowFlake(g2, Color.black, Color.white, 0, 5, width);
132: return bim;
133: }
134:
135: /*test
136: public static void main(String[] aa)
137: {
138: JFrame frame = new JFrame();
139: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
140: frame.setLayout(new BorderLayout());
141: frame.add(new JLabel(new SnowIcon(128, 128)), BorderLayout.CENTER);
142: frame.setSize(122,122);
143: frame.setVisible(true);
144: }*/
145:
146: }
|