KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snow > utils > gui > SnowIcon


1 package snow.utils.gui;
2
3 import java.util.*;
4 import java.awt.*;
5 import java.awt.image.*;
6 import java.awt.geom.*;
7 import javax.swing.*;
8
9 public class SnowIcon implements Icon
10 {
11    int width, height;
12
13    double[][] kochCoord2 =
14      {{0, 0}, {0.0555556, 0.096225}, {0, 0.19245}, {0.111111, 0.19245},
15       {0.166667, 0.288675}, {0.166667, 0.288675}, {0.111111, 0.3849},
16       {0, 0.3849}, {0.0555556, 0.481125}, {0, 0.57735}, {0, 0.57735},
17       {0.111111, 0.57735}, {0.166667, 0.673575}, {0.222222, 0.57735},
18       {0.333333, 0.57735}, {0.333333, 0.57735}, {0.388889, 0.673575},
19       {0.333333, 0.7698}, {0.444444, 0.7698}, {0.5, 0.866025}, {0.5, 0.866025},
20       {0.5, 0.866025}, {0.5, 0.866025}, {0.5, 0.866025}, {0.5, 0.866025},
21       {0.5, 0.866025}, {0.555556, 0.7698}, {0.666667, 0.7698}, {0.611111, 0.673575},
22       {0.666667, 0.57735}, {0.666667, 0.57735}, {0.777778, 0.57735},
23       {0.833333, 0.673575}, {0.888889, 0.57735}, {1., 0.57735}, {1.,0.57735},
24       {0.944444, 0.481125}, {1., 0.3849}, {0.888889, 0.3849},
25       {0.833333, 0.288675}, {0.833333, 0.288675}, {0.888889, 0.19245},
26       {1., 0.19245}, {0.944444, 0.096225}, {1, 0}, {1, 0}, {1, 0}, {1., 0},
27       {1, 0}, {1, 0}, {1, 0}, {0.888889, 0}, {0.833333, -0.096225},
28       {0.777778, 0}, {0.666667, 0}, {0.666667, 0}, {0.611111, -0.096225},
29       {0.666667, -0.19245}, {0.555556, -0.19245}, {0.5, -0.288675},
30       {0.5, -0.288675}, {0.444444, -0.19245}, {0.333333, -0.19245},
31       {0.388889, -0.096225}, {0.333333, 0}, {0.333333, 0}, {0.222222, 0},
32       {0.166667, -0.096225}, {0.111111, 0}, {0, 0}};
33
34   final GeneralPath flock;
35
36   public SnowIcon(int w, int h)
37   {
38     width = w;
39     height = h;
40
41     flock = new GeneralPath();
42     flock.moveTo(0,0);
43
44     int step = (w<10 ? 5 : 1);
45     for(int i=0; i<kochCoord2.length; i+=step)
46     {
47       flock.lineTo( (float)kochCoord2[i][0], (float)kochCoord2[i][1]);
48     }
49     flock.lineTo(0f, 0f);
50
51     float sx = 0.2f;
52     float sy = 0.12f;
53     float f = 0.61f;
54
55     flock.moveTo((float)kochCoord2[0][0]*f+sx, (float)kochCoord2[0][1]*f+sy);
56     for(int i=kochCoord2.length-1; i>=0; i-=step)
57     {
58       flock.lineTo( (float)kochCoord2[i][0]*f+sx, (float)kochCoord2[i][1]*f+sy);
59     }
60     flock.lineTo((float)kochCoord2[0][0]*f+sx, (float)kochCoord2[0][1]*f+sy);
61
62
63   } // Constructor
64

65
66   public int getIconHeight()
67   {
68     return height;
69   }
70
71   public int getIconWidth()
72   {
73     return width;
74   }
75
76   public void paintSnowFlake(Graphics2D g2, double x, double y, double size)
77   {
78     Color bgColor = UIManager.getColor("Panel.background");
79     if( bgColor == null )
80     {
81        bgColor = new Color(250,170,150); // give it a green color to show
82
} // that the key was invalid.
83
Color col2 = SnowBackgroundPanel.createSmallLighterOrDarkerColor(bgColor);
84     paintSnowFlake(g2, col2, bgColor, x,y,size);
85   }
86
87   public void paintSnowFlake(Graphics2D g2, Color c1, Color c2, double x, double y, double size)
88   {
89     AffineTransform oldTransform = g2.getTransform();
90     RenderingHints oldHints = g2.getRenderingHints();
91     Paint oldPaint = g2.getPaint();
92
93
94     g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
95     AffineTransform at = new AffineTransform(size,0,0,size,x,y);
96     g2.transform(at);
97
98     g2.setColor(Color.black);
99     //g2.setStroke(new BasicStroke( (float)(Math.sqrt(size)*0.002), BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND ));
100

101     g2.setPaint(new GradientPaint(
102       0f, 0f, c1, // new Color(240,240,255,200),
103
.9f, .9f, c2 // new Color(220,220,255,200)));
104
));
105     g2.fill(flock);
106
107
108     g2.setTransform(oldTransform);
109     g2.setRenderingHints(oldHints);
110     g2.setPaint(oldPaint);
111   }
112
113
114   public void paintIcon(Component c, Graphics g, int x, int y)
115   {
116     Graphics2D g2 = (Graphics2D) g;
117     // back
118
g2.setColor(Color.white);
119     g2.fill( g2.getClip() );
120
121     // flake
122
paintSnowFlake(g2, x+width/10, y+width/3.7, width*0.8);
123     g2.draw(new Rectangle2D.Double(x,y,width,height));
124   }
125
126
127   public BufferedImage getIconAsImage()
128   {
129     BufferedImage bim = new BufferedImage(width,height,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
136   public static void main(String JavaDoc[] aa)
137   {
138     JFrame frame = new JFrame();
139     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
140     frame.getContentPane().setLayout(new BorderLayout());
141     frame.getContentPane().add(new JLabel(new SnowIcon(128, 128)), BorderLayout.CENTER);
142     frame.setSize(122,122);
143     frame.setVisible(true);
144   }
145
146
147 } // SnowIcon
Popular Tags