Jump to content

Draw Rectangle surrounding item in inventory.


Recommended Posts

Posted (edited)

There is no native support in the Item class, or even ItemContainer for that matter. You (sadly) need to make an inventory destination and get the rectangle through there.

 

EDIT: For bank, you can get it like so:

public Rectangle getRectForItem(Item i) {
    int slot = getBank().getSlot(i);
    int tab = getBank().getTabForAbsoluteSlot(slot);
    return getBank().getAbsoluteSlotPosition(tab, slot);
}
Edited by Bobrocket
Posted
InventorySlotDestination.getSlot(inventory.getSlot(item))

returns a rectangle

 

 

That makes it super simple. Thanks dude!

 

There is no native support in the Item class, or even ItemContainer for that matter. You (sadly) need to make an inventory destination and get the rectangle through there.

 

Thank you for the information......

Posted

Unfortunately from the current api it seems you can only draw the first slot as mousedestination returns the first item in the inventory (L->R,T->B)

 

But in order not to overlap the number, you can do something like:

g.drawRect(
								(int) this.inventory
										.getMouseDestination(
												this.inventory.getSlot(item))
										.getBoundingBox().getX() - 1,
								(int) this.inventory
										.getMouseDestination(
												this.inventory.getSlot(item))
										.getBoundingBox().getY() - 1,
								(int) this.inventory
										.getMouseDestination(
												this.inventory.getSlot(item))
										.getBoundingBox().getWidth() + 2,
								(int) this.inventory
										.getMouseDestination(
												this.inventory.getSlot(item))
										.getBoundingBox().getHeight() + 2);

where the additions correct the rectangle dimensions

 

apa

Posted

Unfortunately from the current api it seems you can only draw the first slot as mousedestination returns the first item in the inventory (L->R,T->cool.png

 

But in order not to overlap the number, you can do something like:

g.drawRect(
								(int) this.inventory
										.getMouseDestination(
												this.inventory.getSlot(item))
										.getBoundingBox().getX() - 1,
								(int) this.inventory
										.getMouseDestination(
												this.inventory.getSlot(item))
										.getBoundingBox().getY() - 1,
								(int) this.inventory
										.getMouseDestination(
												this.inventory.getSlot(item))
										.getBoundingBox().getWidth() + 2,
								(int) this.inventory
										.getMouseDestination(
												this.inventory.getSlot(item))
										.getBoundingBox().getHeight() + 2);

where the additions correct the rectangle dimensions

 

apa

 

 

Actually this is the method i went with:

if (getInventory().contains(other)) {
	Rectangle rec = new Rectangle(InventorySlotDestination.getSlot(inventory.getSlot(other)));
	g.setColor(Color.ORANGE);
	g.draw(rec);
}
Posted

 

Actually this is the method i went with:

if (getInventory().contains(other)) {
	Rectangle rec = new Rectangle(InventorySlotDestination.getSlot(inventory.getSlot(other)));
	g.setColor(Color.ORANGE);
	g.draw(rec);
}

 

That will work just fine too however the rect may overlap with the stack size should the item be stackable

 

apa

Posted

 

There is no native support in the Item class, or even ItemContainer for that matter. You (sadly) need to make an inventory destination and get the rectangle through there.

 

EDIT: For bank, you can get it like so:

public Rectangle getRectForItem(Item i) {
    int slot = getBank().getSlot(i);
    int tab = getBank().getTabForAbsoluteSlot(slot);
    return getBank().getAbsoluteSlotPosition(tab, slot);
}

 

 

This method isn't working for drawing in bank. Not sure why, it seems logical but it just wont draw the rect in the right spot. It's drawing it on a completely different item.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...