source: lost-perception/gamegui/Menu.java@ 0b84f13

Last change on this file since 0b84f13 was ebd3538, checked in by Dmitry Portnoy <dmitry.portnoy@…>, 6 years ago

Initial commit. This codebase for the Lost Perception game was created by decompiling code from a jar file.

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