1 | package gamegui;
|
---|
2 |
|
---|
3 | import java.awt.Color;
|
---|
4 | import java.awt.Font;
|
---|
5 | import java.awt.FontMetrics;
|
---|
6 | import java.awt.Graphics;
|
---|
7 | import java.awt.event.MouseEvent;
|
---|
8 | import java.util.ArrayList;
|
---|
9 |
|
---|
10 | public class Menu extends Member {
|
---|
11 |
|
---|
12 | private ArrayList<String> items;
|
---|
13 | private int selectedIndex;
|
---|
14 | private String label;
|
---|
15 | private Font font;
|
---|
16 | private boolean open;
|
---|
17 | private FontMetrics metrics;
|
---|
18 |
|
---|
19 | public Menu(String newName, int newX, int newY, int newWidth, int newHeight, String newLabel, Font newFont) {
|
---|
20 | super(newName, newX, newY, newWidth, newHeight);
|
---|
21 | this.items = new ArrayList<String>();
|
---|
22 | this.selectedIndex = -1;
|
---|
23 | this.label = newLabel;
|
---|
24 | this.font = newFont;
|
---|
25 | this.open = false;
|
---|
26 | }
|
---|
27 |
|
---|
28 | public boolean handleEvent(MouseEvent e) {
|
---|
29 | if (getX() + this.metrics.stringWidth(this.label) + 4 <= e.getX() && e.getX() <= getX() + getWidth() && getY() + getHeight() <= e.getY() && e.getY() <= getY() + getHeight() + 15 * this.items.size() && this.open) {
|
---|
30 | this.selectedIndex = (e.getY() - getY() - getHeight()) / 15;
|
---|
31 | this.open = false;
|
---|
32 | return true;
|
---|
33 | }
|
---|
34 | if (getX() + getWidth() - getHeight() <= e.getX() && e.getX() <= getX() + getWidth() && getY() <= e.getY() && e.getY() <= getY() + getHeight()) {
|
---|
35 | this.open = !this.open;
|
---|
36 | return true;
|
---|
37 | }
|
---|
38 | return false;
|
---|
39 | }
|
---|
40 |
|
---|
41 | public void clear() {
|
---|
42 | if (this.selectedIndex != -1)
|
---|
43 | this.selectedIndex = 0;
|
---|
44 | }
|
---|
45 |
|
---|
46 | public void draw(Graphics g) {
|
---|
47 | this.metrics = g.getFontMetrics(this.font);
|
---|
48 | g.setColor(Color.black);
|
---|
49 | g.fillRect(getX(), getY(), getWidth(), getHeight());
|
---|
50 | g.setColor(Color.red);
|
---|
51 | g.drawRect(getX(), getY(), getWidth(), getHeight());
|
---|
52 | g.drawLine(getX() + this.metrics.stringWidth(this.label) + 4, getY(), getX() + this.metrics.stringWidth(this.label) + 4, getY() + getHeight());
|
---|
53 | g.drawLine(getX() + getWidth() - getHeight(), getY(), getX() + getWidth() - getHeight(), getY() + getHeight());
|
---|
54 | g.drawLine(getX() + getWidth() - getHeight() * 17 / 20, getY() + getHeight() * 3 / 20, getX() + getWidth() - getHeight() * 3 / 20, getY() + getHeight() * 3 / 20);
|
---|
55 | g.drawLine(getX() + getWidth() - getHeight() * 17 / 20, getY() + getHeight() * 3 / 20, getX() + getWidth() - getHeight() / 2, getY() + getHeight() * 17 / 20);
|
---|
56 | g.drawLine(getX() + getWidth() - getHeight() / 2, getY() + getHeight() * 17 / 20, getX() + getWidth() - getHeight() * 3 / 20, getY() + getHeight() * 3 / 20);
|
---|
57 | g.setColor(Color.green);
|
---|
58 | g.setFont(this.font);
|
---|
59 | g.drawString(this.label, getX() + 2, getY() + (getHeight() + this.metrics.getHeight()) / 2 - 2);
|
---|
60 | g.drawString(this.items.get(this.selectedIndex), getX() + this.metrics.stringWidth(this.label) + 8, getY() + (getHeight() + this.metrics.getHeight()) / 2 - 2);
|
---|
61 | if (this.open) {
|
---|
62 | g.setColor(Color.black);
|
---|
63 | g.fillRect(getX() + this.metrics.stringWidth(this.label) + 4, getY() + getHeight(), getWidth() - this.metrics.stringWidth(this.label) - 4, this.items.size() * 15);
|
---|
64 | g.setColor(Color.red);
|
---|
65 | g.drawRect(getX() + this.metrics.stringWidth(this.label) + 4, getY() + getHeight(), getWidth() - this.metrics.stringWidth(this.label) - 4, this.items.size() * 15);
|
---|
66 | if (this.selectedIndex != -1) {
|
---|
67 | g.setColor(Color.blue);
|
---|
68 | g.fillRect(getX() + this.metrics.stringWidth(this.label) + 5, getY() + getHeight() + 1 + 15 * this.selectedIndex, getWidth() - this.metrics.stringWidth(this.label) - 5, 14);
|
---|
69 | }
|
---|
70 | g.setColor(Color.green);
|
---|
71 | for (int x = 0; x < this.items.size(); x++)
|
---|
72 | g.drawString(this.items.get(x), getX() + this.metrics.stringWidth(this.label) + 8, getY() + (getHeight() + this.metrics.getHeight()) / 2 + 15 * (x + 1));
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | public void add(String newString) {
|
---|
77 | this.selectedIndex = 0;
|
---|
78 | this.items.add(newString);
|
---|
79 | }
|
---|
80 |
|
---|
81 | public String getSelected() {
|
---|
82 | return this.items.get(this.selectedIndex);
|
---|
83 | }
|
---|
84 | }
|
---|