-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoundButton.java
More file actions
executable file
·127 lines (110 loc) · 3.94 KB
/
RoundButton.java
File metadata and controls
executable file
·127 lines (110 loc) · 3.94 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
/*
* Projet : Trustopics
* Version : 0.2.1
* Fichier : RoundButton.java
*/
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.font.FontRenderContext;
import java.awt.font.TextAttribute;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import java.text.AttributedCharacterIterator;
import java.text.AttributedString;
import javax.swing.BorderFactory;
import javax.swing.JButton;
public class RoundButton extends JButton implements MouseListener {
private String glb_text;
private int glb_widthText;
private int glb_heightText;
private Color glb_colorFontText;
private Color glb_colorFontBackRound;
private Font glb_font;
private int glb_fontSize = 15;
private Color glb_backButtonColor = Defines.UIcolBackGDrawing;
private Color glb_foreButtonColor = Defines.UIcolForeGText;
private Color glb_clickButtonColor = Defines.UIcolForeGText.brighter();
public RoundButton(String prm_text,Color prm_backButtonColor,Color prm_foreButtonColor,int prm_fontSize) {
glb_backButtonColor = prm_backButtonColor;
glb_foreButtonColor = prm_foreButtonColor;
glb_clickButtonColor = prm_foreButtonColor.brighter();
glb_fontSize = prm_fontSize;
InitButton(prm_text);
}
public RoundButton(String prm_text) {
InitButton(prm_text);
}
public void InitButton(String prm_text) {
glb_text = prm_text;
glb_font = new Font("Dialog",Font.BOLD,glb_fontSize);
glb_colorFontText = glb_foreButtonColor;
glb_colorFontBackRound = glb_backButtonColor;
GetTextDimensions();
addMouseListener(this);
setBorder(BorderFactory.createEmptyBorder());
}
public int GetWidthText() { return(glb_widthText); }
public int GetHeightText() { return(glb_heightText); }
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(getBackground());
g2.fillRect(0,0,glb_widthText,glb_heightText);
g2.setPaint(glb_colorFontBackRound);
g2.fillRoundRect(0,0,glb_widthText,glb_heightText,glb_heightText,glb_heightText);
AffineTransform afT = new AffineTransform();
Font fontAffichage = glb_font.deriveFont(afT);
AttributedString ats = new AttributedString(glb_text);
ats.addAttribute(TextAttribute.FONT,fontAffichage);
AttributedCharacterIterator iter = ats.getIterator();
FontRenderContext frc = g2.getFontRenderContext();
TextLayout myLayout = new TextLayout(iter, frc);
g2.setPaint(glb_colorFontText);
myLayout.draw(g2,glb_heightText/2,glb_heightText-4);
}
public void mouseEntered(MouseEvent evt) {
glb_colorFontText = glb_backButtonColor;
glb_colorFontBackRound = glb_foreButtonColor;
update(getGraphics());
}
public void mouseExited(MouseEvent evt) {
glb_colorFontText = glb_foreButtonColor;
glb_colorFontBackRound = glb_backButtonColor;
update(getGraphics());
}
public void mouseClicked(MouseEvent evt) {
}
public void mousePressed(MouseEvent evt) {
glb_colorFontText = glb_backButtonColor;
glb_colorFontBackRound = glb_clickButtonColor;
setBorder(BorderFactory.createEmptyBorder());
update(getGraphics());
}
public void mouseReleased(MouseEvent evt) {
if (glb_colorFontBackRound != glb_backButtonColor) {
glb_colorFontText = glb_backButtonColor;
glb_colorFontBackRound = glb_foreButtonColor;
if (getGraphics() != null)
update(getGraphics());
}
}
public void GetTextDimensions() {
FontMetrics fontMetrics = getFontMetrics(glb_font);
glb_widthText = fontMetrics.stringWidth(glb_text);
glb_heightText = fontMetrics.getHeight();
glb_widthText += glb_heightText;
glb_heightText++;
}
public void ClearButton() {
glb_colorFontText = glb_foreButtonColor;
glb_colorFontBackRound = glb_backButtonColor;
update(getGraphics());
}
}