-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenuButton.java
More file actions
36 lines (32 loc) · 1.24 KB
/
MenuButton.java
File metadata and controls
36 lines (32 loc) · 1.24 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
import java.awt.image.BufferedImage;
import java.awt.Graphics;
public class MenuButton {
private int xPos;
private int yPos;
private int w;
private int h;
private String text;
public MenuButton(int xPos, int yPos, int w, int h, String text) {
this.xPos = xPos;
this.yPos = yPos;
this.w = w;
this.h = h;
this.text = text;
}
public void drawButton(BufferedImage image, Graphics g) {
for(int x = xPos; x < xPos + w; x++) {
for(int y = yPos; y < yPos + h; y++) {
if(x == xPos || x == xPos + w - 1 || y == yPos || y == yPos + h - 1) {
image.setRGB(x, y, 0); //black outline
}
else{
image.setRGB(x, y, -4934476); //light gray inside
}
}
}
g.drawString(text, xPos + Math.abs(g.getFontMetrics().stringWidth(text) - w)/2, yPos + Math.abs(g.getFontMetrics().getHeight() - h) / 2 + g.getFontMetrics().getHeight());
}
public boolean clickingInBounds(int x, int y) { //returns true if mouse cursor is clicking within bounds of button
return (x >= xPos && y >= yPos && x < xPos + w && y < yPos + h);
}
}