aboutsummaryrefslogtreecommitdiff
path: root/java/applets/src/aurelienribon/utils/swing/GroupBorder.java
blob: a4db3a978f0087f30523ae8d3206fb061ba981ad (plain)
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
package aurelienribon.utils.swing;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import javax.swing.border.Border;

public class GroupBorder implements Border {
	private final int titleHeight = 20;
	private final int borderPadding = 0;
	private String title = "";

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@Override
	public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
		Graphics2D gg = (Graphics2D) g.create();
		
		int titleW = gg.getFontMetrics().stringWidth(title) + 20;
		int titleDescent = gg.getFontMetrics().getDescent();
		
		gg.setColor(c.getBackground());

		if (!title.equals("")) {
			int[] xs = {0, titleW, titleW + titleHeight, 0};
			int[] ys = {0, 0, titleHeight, titleHeight};
			gg.fillPolygon(xs, ys, 4);
			gg.fillRect(0, titleHeight, width, height);
			gg.setColor(c.getForeground());
			gg.drawString(title, 10, titleHeight - titleDescent);
		} else {
			gg.fillRect(0, 0, width, height);
		}
		
		gg.dispose();
	}

	@Override
	public Insets getBorderInsets(Component c) {
		return new Insets(!title.equals("") ? borderPadding + titleHeight : borderPadding, borderPadding, borderPadding, borderPadding);
	}

	@Override
	public boolean isBorderOpaque() {
		return false;
	}
}