-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathButton.java
48 lines (44 loc) · 1.7 KB
/
Button.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
import javax.swing.*;
import java.awt.*;
//A button class that makes a button on the screen and has tinted effect
public class Button {
//The position of the button
private int x, y;
//Holds the image of the frame, tinted and non-tinted
private Image img, imgTinted;
//Rectangle to check collision with mouse
private Rectangle rect;
//Static variable of the directory of the button frame images
private static String buttonDir = "Sprites/Button.png";
private static String tintedButtonDir = "Sprites/Button_Tinted.png";
//Constructor that takes in the position and the size of this button
public Button(int x, int y, int width, int height) {
//Use the value in the parameter to assign all the private variables
//For this class
this.x = x; this.y = y;
img = new ImageIcon(buttonDir).getImage();
img = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
imgTinted = new ImageIcon(tintedButtonDir).getImage();
imgTinted = imgTinted.getScaledInstance(width, height, Image.SCALE_SMOOTH);
rect = new Rectangle(x, y, width, height);
}
//Check if the mouse is on this button
public boolean onMouse(Point MP) {
//Check if the mouse position collides with the rectangle
if (rect.contains(MP)) {
return true;
}
return false;
}
//Draws the button
public void draw(Graphics g, Point MP) {
//If the mouse is on the button, then draw the tinted button
if (onMouse(MP)) {
g.drawImage(imgTinted, x, y, null);
}
//If not, then draw the normal looking button
else {
g.drawImage(img, x, y, null);
}
}
}