-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInfoTab.java
More file actions
157 lines (131 loc) · 4.55 KB
/
InfoTab.java
File metadata and controls
157 lines (131 loc) · 4.55 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* InfoTab.java
* This class provides the GUI for one tab of a tabbed pane. This tab contains the player character's current state.
* @author Emma Shumadine, Lily Orth-Smith, Rachel Zhang (primary)
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class InfoTab extends JPanel
{
private Game g;
private JLabel HealthLabel, ItemsLabel, DescriptionLabel, WeaponLabel, HeaderLabel;
private JComboBox InventoryCombo;
private JButton UseButton, DropButton;
private JPanel TopPanel, ListPanel, DescriptionPanel, ActionPanel;
private Vector<Item> inventory;
private int selection;
/**
* Creates the tab
* @param g the game associated with this tab
*/
public InfoTab(Game g)
{
//setBackground (Color.green);
this.g = g;
setLayout(new BorderLayout());
TopPanel = new JPanel();
ListPanel = new JPanel();
DescriptionPanel = new JPanel();
ActionPanel = new JPanel();
HealthLabel = new JLabel ("<html>" + g.getPlayerHealth()+ "</html>");
inventory = g.getInventory();
//System.out.println(inventory.get(0) + "**"); //remove
String strInventory = "<html><br/>";
for (Item i: inventory)
strInventory += i.toString() + "<br/>";
strInventory += "</html>";
ItemsLabel = new JLabel(strInventory);
DescriptionLabel = new JLabel("");
WeaponLabel = new JLabel(g.getWeapon());
HeaderLabel = new JLabel("Current Items:");
InventoryCombo = new JComboBox(inventory);
UseButton = new JButton("Use");
DropButton = new JButton("Drop");
TopPanel.setLayout(new BorderLayout());
TopPanel.add(HealthLabel, BorderLayout.WEST);
TopPanel.add(WeaponLabel, BorderLayout.EAST);
//TopPanel.setBackground(Color.green);
ListPanel.add(HeaderLabel);
ListPanel.add(ItemsLabel);
DescriptionPanel.add(DescriptionLabel);
ActionPanel.add(InventoryCombo);
ActionPanel.add(UseButton);
ActionPanel.add(DropButton);
add(TopPanel, BorderLayout.NORTH);
add(ListPanel, BorderLayout.WEST);
add(DescriptionPanel, BorderLayout.SOUTH);
add(ActionPanel, BorderLayout.CENTER);
InventoryCombo.addActionListener(new ComboListener());
UseButton.addActionListener(new UseListener());
addMouseListener (new UpdateListener());
DropButton.addActionListener(new DropListener());
}
private class ComboListener implements ActionListener
{
private int current;
public void actionPerformed (ActionEvent event)
{
current = InventoryCombo.getSelectedIndex();
selection = current;
if (current >=0 ) {
DescriptionLabel.setText(inventory.get(current).getDescription());
}
}
}
private class UseListener implements ActionListener {
private int current;
public void actionPerformed (ActionEvent event) {
current = InventoryCombo.getSelectedIndex();
if (!(inventory.size() == 0)) {
Item temp = inventory.get(current).clone();
DescriptionLabel.setText(g.useItem(temp));
HealthLabel.setText("<html>" + g.getPlayerHealth()+ "</html>");
//reset inventorylabel
}
}
}
private class DropListener implements ActionListener {
private int current;
public void actionPerformed (ActionEvent event) {
current = InventoryCombo.getSelectedIndex();
Item item = inventory.get(current);
g.dropItem(item);
DescriptionLabel.setText(item.toString() + " dropped.");
update();
}
}
/**
* Updates the player's information
*/
private void update() {
inventory = g.getInventory();
InventoryCombo.setModel(new DefaultComboBoxModel(inventory));
if (inventory.size() > 0 ) {
if (selection >= inventory.size())
selection = inventory.size() -1;
InventoryCombo.setSelectedIndex(selection);
}
HealthLabel.setText(g.getPlayerHealth());
TopPanel.setBackground(g.getHealthColor());
String strInventory = "<html><br/>";
for (Item i: inventory)
strInventory += i.toString() + "<br/>";
strInventory += "</html>";
ItemsLabel.setText(strInventory);
WeaponLabel.setText(g.getWeapon());
}
private class UpdateListener implements MouseListener
{
public void mouseEntered (MouseEvent event) {
update();
}
public void mouseClicked (MouseEvent event) {
update();
}
public void mouseExited (MouseEvent event) {}
public void mousePressed (MouseEvent event) {}
public void mouseReleased (MouseEvent event) {}
}
}