This repository has been archived by the owner on Aug 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphicComponent.java
159 lines (130 loc) · 3.26 KB
/
GraphicComponent.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
/**
* A graphic component
*
* Last edit: 5/28/2020
* @author Celeste
* @version 1.0
* @since 1.0
*/
import java.awt.*;
import java.awt.event.*;
public abstract class GraphicComponent implements MouseMotionListener, MouseListener, KeyListener {
/**
* The field's x-coordinate (top left corner)
*/
protected int x_coord;
/**
* The field's y-coordinate (top left corner)
*/
protected int y_coord;
/**
* The field's width
*/
protected int width;
/**
* The field's height
*/
protected int height;
/**
* The Font of the name, if drawn
*/
protected Font text_font;
/**
* Whether the user just clicked the component
*/
protected boolean isClicked;
/**
* Whether the user is hovering over the component
*/
protected boolean isHovered;
/**
* The name of the component
*/
protected String name;
/**
* @return whether the component has just been clicked by the user
*/
public boolean isClicked() {
return isClicked;
}
/**
* @return whether the user is hovering over the component
*/
public boolean isHovered() {
return isHovered;
}
public String getName() {
return name;
}
/**
* Draws the component
* @param g the Graphics object to draw on
*/
public abstract void draw(Graphics g);
/**
* Activates the component's listeners and the like
* @param mouse whether to listen for mouse events
* @param key whether to listen for key events
*/
public void activate(boolean mouse, boolean key) {
if (mouse) {
CovidCashier.frame.addMouseListener(this);
CovidCashier.frame.addMouseMotionListener(this);
resetClicked();
}
if (key) {
CovidCashier.frame.addKeyListener(this);
}
}
/**
* Deativates the components's listeners and the like
* @param mouse whether to stop listening for mouse events
* @param key whether to stop listening for key events
*/
public void deactivate(boolean mouse, boolean key) {
if (mouse) {
CovidCashier.frame.removeMouseListener(this);
CovidCashier.frame.removeMouseMotionListener(this);
resetClicked();
}
if (key) {
CovidCashier.frame.removeKeyListener(this);
}
}
/**
* Handles the mouse click event
* @param e the event object and data
*/
public void mouseClicked(MouseEvent e) {
isClicked = withinCoordinates();
if (isClicked) CovidCashier.frame.repaint();
}
/**
* Sets click and hover properties to false
*/
public void resetClicked() {
isClicked = false;
}
/**
* Handles the mouse move event
* @param e the event object and data
*/
public void mouseMoved(MouseEvent e) {
boolean before = isHovered;
isHovered = withinCoordinates();
if (isHovered != before) CovidCashier.frame.repaint();
}
/**
* Check whether the user's mouse is within the boundaries of this Button
* @return whether the mouse is within the boundaries of this Button
*/
protected abstract boolean withinCoordinates();
public void mouseExited(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {}
public void keyPressed(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
}