-
Notifications
You must be signed in to change notification settings - Fork 3
/
GButton.java
106 lines (98 loc) · 4.22 KB
/
GButton.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
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
/**
* A custom extnesion of JButton to allow for the use of custom images for
* buttons but retain the functionality of a JButton
*
* @author Sean Walsh
* @version 1.0
*/
public class GButton extends JButton {
/**
* Constructor for GButton
*
* @param bounds array of size [4] that contins the x,y,with,height of the
* button
* @param hover A Image to be shown when the mouse is over the button
* @prarm base A Image to be shown by default
*/
public GButton(int[] bounds, Image base, Image hover) {
// sets text to null
this.setText("");
// sets the bounds to the param provided
this.setBounds(bounds[0], bounds[1], bounds[2], bounds[3]);
// sets the image icon of the button to the given base image
this.setIcon(new ImageIcon(base));
// needed to make the icon the only visible part of the button
this.setBorderPainted(false);
this.setOpaque(false);
this.setContentAreaFilled(false);
// self refrence for use in actions
GButton self = this;
this.addMouseListener(new java.awt.event.MouseAdapter() {
// action for when the mouse enters the buttons bounds
public void mouseEntered(java.awt.event.MouseEvent evt) {
// sets the image icon of the
//button to the given hover image
self.setIcon(((Icon) new ImageIcon(hover)));
}
// action for when the mouse exits the buttons bounds
public void mouseExited(java.awt.event.MouseEvent evt) {
// sets the image icon of the
//button to the given base image
self.setIcon(((Icon) new ImageIcon(base)));
}
});
}
/**
* Constructor for GButton
*
* @param bounds array of size [4] that contins the x,y,with,height of the
* button
* @param hover A Image to be shown when the mouse is over the button
* @prarm base A Image to be shown by default
* @param pressed A Image to be shown when the mouse is pressed on
* a button
*/
public GButton(int[] bounds, Image base, Image hover, Image pressed) {
// sets text to null
this.setText("");
// sets the bounds to the param provided
this.setBounds(bounds[0], bounds[1], bounds[2], bounds[3]);
// sets the image icon of the button to the given base image
this.setIcon(new ImageIcon(base));
// needed to make the icon the only visible part of the button
this.setBorderPainted(false);
this.setOpaque(false);
this.setContentAreaFilled(false);
// self refrence for use in actions
GButton self = this;
this.addMouseListener(new java.awt.event.MouseAdapter() {
// action for when the mouse enters the buttons bounds
public void mouseEntered(java.awt.event.MouseEvent evt) {
// sets the image icon of the
// button to the given hover image
self.setIcon(((Icon) new ImageIcon(hover)));
}
// action for when the mouse exits the buttons bounds
public void mouseExited(java.awt.event.MouseEvent evt) {
// sets the image icon of the
// button to the given base image
self.setIcon(((Icon) new ImageIcon(base)));
}
//action for when the mouse is pressed
public void mousePressed(java.awt.event.MouseEvent evt) {
// sets the image icon of the
// button to the given hover image
self.setIcon(((Icon) new ImageIcon(pressed)));
}
//action for when the mouse is released
public void mouseReleased(java.awt.event.MouseEvent evt) {
// sets the image icon of the
// button to the given base image
self.setIcon(((Icon) new ImageIcon(base)));
}
});
}
}