/
DiamondIcon.java
49 lines (49 loc) · 1.3 KB
/
DiamondIcon.java
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
import javax.swing.*;
import java.awt.*;
public class DiamondIcon implements Icon {
private Color color;
private boolean selected;
private int width;
private int height;
private Polygon poly;
private static final int DEFAULT_WIDTH = 10;
private static final int DEFAULT_HEIGHT = 10;
public DiamondIcon(Color color) {
this (color, true, DEFAULT_WIDTH, DEFAULT_HEIGHT);
}
public DiamondIcon(Color color, boolean selected) {
this (color, selected, DEFAULT_WIDTH, DEFAULT_HEIGHT);
}
public DiamondIcon (Color color, boolean selected, int width, int height) {
this.color = color;
this.selected = selected;
this.width = width;
this.height = height;
initPolygon();
}
private void initPolygon() {
poly = new Polygon();
int halfWidth = width/2;
int halfHeight = height/2;
poly.addPoint (0, halfHeight);
poly.addPoint (halfWidth, 0);
poly.addPoint (width, halfHeight);
poly.addPoint (halfWidth, height);
}
public int getIconHeight() {
return height;
}
public int getIconWidth() {
return width;
}
public void paintIcon(Component c, Graphics g, int x, int y) {
g.setColor (color);
g.translate (x, y);
if (selected) {
g.fillPolygon (poly);
} else {
g.drawPolygon (poly);
}
g.translate (-x, -y);
}
}