-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPanelShadow.java
More file actions
182 lines (158 loc) · 5.03 KB
/
Copy pathPanelShadow.java
File metadata and controls
182 lines (158 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package Charts;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
public class PanelShadow extends JPanel {
public ShadowType getShadowType() {
return shadowType;
}
public void setShadowType(ShadowType shadowType) {
this.shadowType = shadowType;
}
public int getShadowSize() {
return shadowSize;
}
public void setShadowSize(int shadowSize) {
this.shadowSize = shadowSize;
}
public float getShadowOpacity() {
return shadowOpacity;
}
public void setShadowOpacity(float shadowOpacity) {
this.shadowOpacity = shadowOpacity;
}
public Color getShadowColor() {
return shadowColor;
}
public void setShadowColor(Color shadowColor) {
this.shadowColor = shadowColor;
}
private BufferedImage renderImage;
private ShadowType shadowType = ShadowType.CENTER;
private int shadowSize = 6;
private float shadowOpacity = 0.5f;
private Color shadowColor = Color.BLACK;
// Gradient Option
private GradientType gradientType = GradientType.HORIZONTAL;
private Color colorGradient = new Color(255, 255, 255);
private int radius;
public PanelShadow() {
setOpaque(false);
setBackground(Color.WHITE);
}
@Override
protected void paintComponent(Graphics grphcs) {
if (renderImage == null) {
createRenderImage();
}
grphcs.drawImage(renderImage, 0, 0, null);
super.paintComponent(grphcs);
}
@Override
public void setBounds(int x, int y, int width, int height) {
super.setBounds(x, y, width, height);
createRenderImage();
}
private void createRenderImage() {
renderImage = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = renderImage.createGraphics();
int size = shadowSize * 2;
int x;
int y;
int width = getWidth() - size;
int height = getHeight() - size;
if (shadowType == ShadowType.TOP) {
x = shadowSize;
y = size;
} else if (shadowType == ShadowType.BOT) {
x = shadowSize;
y = 0;
} else if (shadowType == ShadowType.TOP_LEFT) {
x = size;
y = size;
} else if (shadowType == ShadowType.TOP_RIGHT) {
x = 0;
y = size;
} else if (shadowType == ShadowType.BOT_LEFT) {
x = size;
y = 0;
} else if (shadowType == ShadowType.BOT_RIGHT) {
x = 0;
y = 0;
} else {
x = shadowSize;
y = shadowSize;
}
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = img.createGraphics();
createBackground(g, width, height);
ShadowRenderer render = new ShadowRenderer(shadowSize, shadowOpacity, shadowColor);
g2.drawImage(render.createShadow(img), 0, 0, null);
g2.drawImage(img, x, y, null);
g2.dispose();
}
private void createBackground(Graphics2D g2, int width, int height) {
g2.setColor(getBackground());
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int x1, x2, y1, y2;
if (gradientType == GradientType.HORIZONTAL || gradientType == null) {
x1 = 0;
y1 = 0;
x2 = width;
y2 = 0;
} else if (gradientType == GradientType.VERTICAL) {
x1 = 0;
y1 = 0;
x2 = 0;
y2 = height;
} else if (gradientType == GradientType.DIAGONAL_1) {
x1 = 0;
y1 = height;
x2 = width;
y2 = 0;
} else {
x1 = 0;
y1 = 0;
x2 = width;
y2 = height;
}
Point p1 = new Point(x1, y1);
Point p2 = new Point(x2, y2);
g2.setPaint(new GradientPaint(p1, getBackground(), p2, colorGradient));
g2.fill(new RoundRectangle2D.Double(0, 0, width, height, radius, radius));
g2.dispose();
}
public GradientType getGradientType() {
return gradientType;
}
public void setGradientType(GradientType gradientType) {
this.gradientType = gradientType;
repaint();
}
public Color getColorGradient() {
return colorGradient;
}
public void setColorGradient(Color colorGradient) {
this.colorGradient = colorGradient;
repaint();
}
public int getRadius() {
return radius;
}
public void setRadius(int radius) {
this.radius = radius;
repaint();
}
public static enum GradientType {
VERTICAL, HORIZONTAL, DIAGONAL_1, DIAGONAL_2
}
public static enum ShadowType {
CENTER, TOP_RIGHT, TOP_LEFT, BOT_RIGHT, BOT_LEFT, BOT, TOP
}
}