1
2
3
4
5
6
7
8
9
10
11
12
13 package org.abstracthorizon.aequo.gui;
14
15 import java.awt.Color;
16 import java.awt.GradientPaint;
17 import java.awt.Graphics;
18 import java.awt.Graphics2D;
19 import java.awt.LayoutManager;
20 import java.awt.Paint;
21 import java.awt.RenderingHints;
22
23 import javax.swing.JPanel;
24
25
26
27
28
29
30 public class GradientPanel extends JPanel {
31
32
33 protected Color color1 = Color.WHITE;
34
35
36 protected Color color2 = Color.LIGHT_GRAY;
37
38
39
40
41 public GradientPanel() {
42 setOpaque(false);
43 }
44
45
46
47
48
49 public GradientPanel(LayoutManager layout) {
50 super(layout);
51 setOpaque(false);
52 }
53
54
55
56
57
58 public GradientPanel(boolean isDoubleBuffered) {
59 super(isDoubleBuffered);
60 setOpaque(false);
61 }
62
63
64
65
66
67
68 public GradientPanel(LayoutManager layout, boolean isDoubleBuffered) {
69 super(layout, isDoubleBuffered);
70 setOpaque(false);
71 }
72
73
74
75
76
77
78 public void setGradientColors(Color color1, Color color2) {
79 this.color1 = color1;
80 this.color2 = color2;
81 }
82
83
84
85
86
87 public void paint(Graphics g) {
88 Graphics2D g2 = (Graphics2D)g;
89
90 RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
91 hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
92
93 g2.setRenderingHints(hints);
94
95 int w = getWidth();
96 int h = getHeight();
97
98 Paint oldPaint = g2.getPaint();
99 GradientPaint newPaint = new GradientPaint(w / 2.2f, 0.0f, color1, w - w / 2.2f, h, color2);
100
101 g2.setPaint(newPaint);
102 g2.fillRect(0, 0, w, h);
103 g2.setPaint(oldPaint);
104 super.paint(g);
105 }
106 }