19
19
*/
20
20
package ai .nets .samj .gui ;
21
21
22
- import java .awt .Font ;
23
- import java .awt .FontMetrics ;
24
22
import java .awt .Insets ;
25
23
import java .awt .event .ComponentAdapter ;
26
24
import java .awt .event .ComponentEvent ;
27
25
28
26
import javax .swing .JButton ;
29
- import javax .swing .JLabel ;
30
- import javax .swing .SwingConstants ;
31
27
/**
32
28
* TODO make it resizable
33
29
* This class is a normal button whose font is resized when the component is resized
@@ -38,14 +34,6 @@ public class ResizableButton extends JButton {
38
34
* Serial version unique identifier
39
35
*/
40
36
private static final long serialVersionUID = -958367053852506146L ;
41
- /**
42
- * Label containing the text that the button displays when it is not pressed
43
- */
44
- private JLabel textLabel ;
45
- /**
46
- * Label containing the loading animation to be displayed while the button is pressed
47
- */
48
- private int fontSize ;
49
37
50
38
private int horizontalInset ;
51
39
private int verticalInset ;
@@ -54,12 +42,12 @@ public class ResizableButton extends JButton {
54
42
* Constructor. Creates a button that has an icon inside. The icon changes when pressed.
55
43
* @param text
56
44
* the text inside the button
57
- * @param filePath
58
- * the path to the file that contains the image that is going to be used
59
- * @param filename
60
- * the name of the file that is going to be used
61
- * @param animationSize
62
- * size of the side of the squared animation inside the button
45
+ * @param fontSize
46
+ * the size of the font that we want in the button
47
+ * @param horizontalInset
48
+ * horizontal distance from the letter to the sides
49
+ * @param verticalInset
50
+ * vertical distance from the buttons to the sizes
63
51
*/
64
52
public ResizableButton (String text , int fontSize , int horizontalInset , int verticalInset ) {
65
53
super (text );
@@ -77,6 +65,11 @@ public void componentResized(ComponentEvent e) {
77
65
//adjustButtonFont();
78
66
}
79
67
68
+ /**
69
+ *
70
+ * @param fontSize
71
+ * the font size of the button
72
+ */
80
73
public void setFontSize (int fontSize ) {
81
74
setFont (getFont ().deriveFont ((float ) fontSize ));
82
75
}
@@ -90,43 +83,4 @@ public Insets getInsets() {
90
83
public Insets getInsets (Insets insets ) {
91
84
return new Insets (verticalInset , horizontalInset , verticalInset , horizontalInset );
92
85
}
93
-
94
- // Method to adjust the font size based on button size
95
- private void adjustButtonFont () {
96
- int btnHeight = this .getHeight ();
97
- int btnWidth = this .getWidth ();
98
-
99
- if (btnHeight <= 0 || btnWidth <= 0 ) {
100
- return ; // Cannot calculate font size with non-positive dimensions
101
- }
102
-
103
- // Get the button's insets
104
- Insets insets = this .getInsets ();
105
- int availableWidth = btnWidth - insets .left - insets .right ;
106
-
107
- // Start with a font size based on button height
108
- int fontSize = btnHeight - insets .top - insets .bottom ;
109
-
110
- // Get the current font
111
- Font originalFont = this .getFont ();
112
- Font font = originalFont .deriveFont ((float ) fontSize );
113
-
114
- FontMetrics fm = this .getFontMetrics (font );
115
- int textWidth = fm .stringWidth (this .getText ());
116
-
117
- // Reduce font size until text fits
118
- while (textWidth > availableWidth && fontSize > 0 ) {
119
- fontSize --;
120
- font = originalFont .deriveFont ((float ) fontSize );
121
- fm = this .getFontMetrics (font );
122
- textWidth = fm .stringWidth (this .getText ());
123
- }
124
-
125
- // Apply the new font
126
- this .setFont (font );
127
-
128
- // Center the text
129
- this .setHorizontalAlignment (JButton .CENTER );
130
- this .setVerticalAlignment (JButton .CENTER );
131
- }
132
86
}
0 commit comments