对于UI的开发,那么最简单的自然是Button的开发,并且从Button开始开发将积累很多可重用组件以及方法,为以后的开发提供许多依仗
Button动画自然至少有两个个动画,按之前、按下,并且可以添加鼠标移入动画本人的美工确实有限的很,所以开发的东西不会非常好看,还请指教。
这是一个为按下的动画,将这个绘制分解之后自然就可以看出,其实就是两段渐变效果
代码如下
1public class OutsetPainter implements Painter {
2
3 private Rectangle backRect;
4
5 private LinearGradientPaint lGPaint1;
6
7 private Point startP = new Point();
8 private Point endP = new Point();
9
10 private Color darkColor;
11
12 private Color lightColor;
13
14 private Color backColor;
15
16 private Color whiteColor = new Color(250, 250, 250);
17
18 private final static int CHANGE_STEP = 12;
19
20 public OutsetPainter(Rectangle rect) {
21 init(rect, StyleColor.backColor);
22 }
23
24 public OutsetPainter(Rectangle rect, Color color) {
25 init(rect, color);
26 }
27
28 public void init(Rectangle rect, Color color) {
29 this.backRect = (Rectangle) rect.clone();
30 backColor = color;
31 initLinearGradientPaint(rect);
32 }
33
34 public void painted(Graphics2D g2d) {
35 g2d.setPaint(lGPaint1);
36 g2d.fill(backRect);
37 }
38
39 private void initLinearGradientPaint(Rectangle backRect) {
40 darkColor = createDarkColor(backColor);
41 lightColor = createLightColor(backColor);
42 startP.y = backRect.y;
43 endP.y = backRect.y + backRect.height;
44 Color[] colors = { whiteColor, backColor, darkColor, lightColor };
45 float[] dish = { 0.0f, 0.49f, 0.50f, 1.0f };
46 lGPaint1 = new LinearGradientPaint(startP, endP, dish, colors);
47 }
48
49 private Color createDarkColor(Color color) {
50 int red = getSmallerInt(color.getRed());
51 int green = getSmallerInt(color.getGreen());
52 int blue = getSmallerInt(color.getBlue());
53 return new Color(red, green, blue);
54 }
55
56 private int getSmallerInt(int i) {
57 i = (i - CHANGE_STEP < 0) ? 0 : i - CHANGE_STEP;
58 return i;
59 }
60
61 private Color createLightColor(Color color) {
62 int red = getBiggerInt(color.getRed());
63 int green = getBiggerInt(color.getGreen());
64 int blue = getBiggerInt(color.getBlue());
65 return new Color(red, green, blue);
66 }
67
68 private int getBiggerInt(int i) {
69 i = (i + CHANGE_STEP > 255) ? 255 : i + CHANGE_STEP;
70 return i;
71 }
LinearGradientPaint 是这里唯一要讲述一下的类,这个类实现了Paint接口,并实现了渐变的绘制
这也是Java2D的强大之处,为做游戏提供了很多依仗,但是要做更高强度的绘制渲染还是得自己实现Paint接口进行绘制
请注意一个细节
1public OutsetPainter(Rectangle rect, Color color) {
2 init(rect, color);
3}
4
这段代码为鼠标移入按钮提供了依仗,使得更换颜色达到动画效果