I am making a game and I am stuck on something. In a loop I want to fill buttons with data from an Vector. Each one should have a different JPopupMenu on right click. If the item is a sword, and I right click on the button, it should say Equip sword and Destroy sword.
Item Class
Code:
import java.util.Vector;
public class Item {
protected String weaponName, modifierType, classType;
protected int damage, levelReq;
protected static Vector<Item> allWeapons = new Vector<Item>();
protected static Vector<Item> allItems = new Vector<Item>();
//default constructor initializes weapons and items---->
Item() {
initWeapons();
}
//constructor for weapons creation--------------------->
Item(String _Name, String _Modifier, String _class, int _level, int _damage){
setName(_Name);
setModifierType(_Modifier);
setClass(_class);
setLevel(_level);
setDamage(_damage);
}
//initialize all of the games weapons------------------>
public void initWeapons(){
Item s = new Item("Sword", "Normal", "Swordsman", 1, 3);
Item ar = new Item("Bow", "Normal", "Archer", 1, 3);
Item ac = new Item("Staff", "Normal","Acolyte", 1, 3);
Item s2 = new Item("Straight Sword", "Normal", "Swordsman", 3, 5);
Item ultraBlade = new Item("Dark-Chasm Staff", "Flame", "NONE", 30, 60);
for ( int i = 0; i < 23; i ++)
allWeapons.add(ultraBlade);
allWeapons.add(s);
allWeapons.add(ar);
}
//set functions--------------------------------------->
public void setName(String _Name) {
weaponName = _Name;
}
public void setModifierType(String _Modifier){
modifierType = _Modifier;
}
public void setDamage(int _damage){
damage = _damage;
}
public void setClass(String _class) {
classType = _class;
}
public void setLevel(int level) {
levelReq = level;
}
//get functions-------------------------------------->
public String getName() {
return weaponName;
}
public String getModifier(){
return modifierType;
}
public String getClassType(){
return classType;
}
public int getLevel(){
return levelReq;
}
public int getDamage(){
return damage;
}
}
Inventory Class
Code:
import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Inventory {
private static int inventorySize = 25;
private int capRemaining;
private JButton[] inventorySlot;
protected static Vector<Item> inventory = new Vector<Item>(inventorySize);
public JMenuItem destroy, equip;
public JPopupMenu popup;
//Constructor for making users inventory-------------------->
public Inventory(int _inventorySize){
setSize(_inventorySize);
for(int i = 0; i < 25; i ++)
inventory.add(Item.allWeapons.elementAt(i));
createButtons();
}
//Create buttons-------------------------------------------->
public void createButtons(){
inventorySlot = new JButton[inventorySize];
//Inner class to handle buttons clicked----------------->
class InventoryHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
Object source = e.getActionCommand();
System.out.println(source);
}
}
class popupHandler extends MouseAdapter {
public void mousePressed(MouseEvent e) {
maybeShowPopup(e);
}
public void mouseReleased(MouseEvent e) {
maybeShowPopup(e);
}
private void maybeShowPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
popup.show(e.getComponent(),
e.getX(), e.getY());
}
}
}
//Fill buttons with Item Data--------------------------->
for (int i = 0; i < inventory.size(); i++) {
inventorySlot[i] = new JButton(inventory.elementAt(i).weaponName);
inventorySlot[i].setToolTipText("Required Level: "
+ inventory.elementAt(i).levelReq
+ "\n **Weapon name: " + inventory.elementAt(i).weaponName
+ "\n **Weapon modifier: " + inventory.elementAt(i).modifierType
+ "\n **Attack: +" + inventory.elementAt(i).damage
+ "\n **REQUIRED LEVEL: " + inventory.elementAt(i).levelReq
+ "\n **REQUIRED CLASS: " + inventory.elementAt(i).classType);
GUI.mInventoryPanel.add(inventorySlot[i]);
equip = new JMenuItem("Equip " + inventory.elementAt(i).weaponName);
destroy = new JMenuItem("Destroy " + inventory.elementAt(i).weaponName);
popup = new JPopupMenu();
popup.add(equip);
popup.add(destroy);
inventorySlot[i].addActionListener(new InventoryHandler());
inventorySlot[i].setMnemonic(i);
inventorySlot[i].addMouseListener(new popupHandler());
}
//Fill the rest with Empty------------------------------>
for (int i = 0; i < getCapRemaining(); i++) {
inventorySlot[i] = new JButton("empty");
GUI.mInventoryPanel.add(inventorySlot[i]);
}
}
//Set Inventories size-------------------------------------->
private void setSize(int _inventorySize){
inventorySize = _inventorySize;
}
//Get inventory size---------------------------------------->
private int getSize(){
return inventorySize;
}
//Get capacity remaining of inventory----------------------->
private int getCapRemaining(){
return capRemaining = getSize()- inventory.size();
}
//remove an item from the inventory------------------------->
private void removeAt(int location){
inventory.remove(location);
}
}
Right now I have just stupid items filling the array for testing. I tried setting up a JPopupMenu[] but I couldn't think of a way to do it to return to the popupHandler class... I set the mnemonic as a reference to which button it is in order. I couldn't find a way to use that either. I honestly want to learn how to figure this out for future use. Thank you in advance.