-
Notifications
You must be signed in to change notification settings - Fork 156
/
MainPanel.java
190 lines (167 loc) · 5.76 KB
/
MainPanel.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
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
183
184
185
186
187
188
189
190
// -*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
// @homepage@
package example;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Path2D;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import javax.swing.*;
public final class MainPanel extends JPanel {
private static final String TXT = "***********************";
private static final int LINE_WIDTH = 1;
private static final int BI_GAP = 2;
private MainPanel() {
super(new GridLayout(0, 1));
setBorder(BorderFactory.createEmptyBorder(20, 10, 20, 0));
add(makeBreadcrumbList(0, Color.PINK, Arrays.asList("overlap1:", "0px", TXT)));
add(makeBreadcrumbList(5, Color.CYAN, Arrays.asList("overlap2:", "5px", TXT)));
add(makeBreadcrumbList(9, Color.ORANGE, Arrays.asList("overlap3:", "9px", TXT)));
setPreferredSize(new Dimension(320, 240));
}
private static AbstractButton makeButton(String title, Color color, boolean first) {
// https://java-swing-tips.blogspot.com/2008/11/rounded-corner-jbutton.html
return new JToggleButton(title, new GrayIcon()) {
private final transient ArrowToggleButtonCellIcon icon = new ArrowToggleButtonCellIcon();
@Override public void updateUI() {
super.updateUI();
setContentAreaFilled(false);
int th = ArrowToggleButtonCellIcon.TH;
int left = LINE_WIDTH + BI_GAP + (first ? 0 : th);
setBorder(BorderFactory.createEmptyBorder(0, left, 0, th));
setFocusPainted(false);
setOpaque(false);
setBackground(color);
setHorizontalAlignment(LEFT);
// setIcon(new TestIcon());
}
@Override public boolean contains(int x, int y) {
// Shape shape = icon.getShape();
// return shape != null && shape.contains(x, y);
return Optional.ofNullable(icon.getShape())
.map(s -> s.contains(x, y))
.orElseGet(() -> super.contains(x, y));
}
@Override public Dimension getPreferredSize() {
return new Dimension(icon.getIconWidth(), icon.getIconHeight());
}
@Override protected void paintComponent(Graphics g) {
icon.paintIcon(this, g, 0, 0);
super.paintComponent(g);
}
};
}
private static Container makeContainer(int overlap) {
// https://java-swing-tips.blogspot.com/2013/12/breadcrumb-navigation-with-jradiobutton.html
JPanel p = new JPanel(new FlowLayout(FlowLayout.LEADING, -overlap, 0)) {
@Override public boolean isOptimizedDrawingEnabled() {
return false;
}
};
p.setBorder(BorderFactory.createEmptyBorder(0, overlap, 0, 0));
p.setOpaque(false);
return p;
}
private static Component makeBreadcrumbList(int overlap, Color color, List<String> list) {
Container p = makeContainer(overlap + LINE_WIDTH);
ButtonGroup bg = new ButtonGroup();
boolean f = true;
for (String title : list) {
AbstractButton b = makeButton(title, color, f);
p.add(b);
bg.add(b);
f = false;
}
return p;
}
public static void main(String[] args) {
EventQueue.invokeLater(MainPanel::createAndShowGui);
}
private static void createAndShowGui() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (UnsupportedLookAndFeelException ignored) {
Toolkit.getDefaultToolkit().beep();
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
ex.printStackTrace();
return;
}
JFrame frame = new JFrame("@title@");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(new MainPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
// https://ateraimemo.com/Swing/ToggleButtonBar.html
// https://java-swing-tips.blogspot.com/2012/11/make-togglebuttonbar-with-jradiobuttons.html
class ArrowToggleButtonCellIcon implements Icon {
public static final int TH = 10; // The height of a triangle
private static final int HEIGHT = TH * 2 + 1;
private static final int WIDTH = 100;
private Shape shape;
public Shape getShape() {
return shape;
}
protected Shape makeShape(Container parent, Component c, int x, int y) {
int w = c.getWidth() - 1;
int h = c.getHeight() - 1;
double h2 = Math.round(h * .5);
double w2 = TH;
Path2D p = new Path2D.Double();
p.moveTo(0d, 0d);
p.lineTo(w - w2, 0d);
p.lineTo(w, h2);
p.lineTo(w - w2, h);
p.lineTo(0d, h);
if (!Objects.equals(c, parent.getComponent(0))) {
p.lineTo(w2, h2);
}
p.closePath();
return AffineTransform.getTranslateInstance(x, y).createTransformedShape(p);
}
@Override public void paintIcon(Component c, Graphics g, int x, int y) {
Container parent = c.getParent();
if (Objects.isNull(parent)) {
return;
}
shape = makeShape(parent, c, x, y);
Color bgc = parent.getBackground();
Color borderColor = Color.GRAY.brighter();
if (c instanceof AbstractButton) {
ButtonModel m = ((AbstractButton) c).getModel();
if (m.isSelected() || m.isRollover()) {
bgc = c.getBackground();
borderColor = Color.GRAY;
}
}
Graphics2D g2 = (Graphics2D) g.create();
g2.setPaint(bgc);
g2.fill(shape);
g2.setPaint(borderColor);
g2.draw(shape);
g2.dispose();
}
@Override public int getIconWidth() {
return WIDTH;
}
@Override public int getIconHeight() {
return HEIGHT;
}
}
class GrayIcon implements Icon {
@Override public void paintIcon(Component c, Graphics g, int x, int y) {
g.setColor(Color.GRAY);
g.drawOval(x, y, getIconWidth(), getIconHeight());
}
@Override public int getIconWidth() {
return 12;
}
@Override public int getIconHeight() {
return 12;
}
}