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