Jump to content

Search the Community

Showing results for tags 'container'.



More search options

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


Forums

  • RuneLocus
    • News & Announcements
    • Information Booth
    • Website Support
    • Introductions
    • Chat
    • Forum Games
  • RuneScape Development
    • RSPS General
    • RuneScape Private Server (RS2)
    • RuneScape Private Server (503+)
    • RuneScape Private Server (EoC 742+)
  • Other
    • Digital Art
    • Gaming
    • Webdevelopment
    • Computers
  • Marketplace
    • RuneScape Market
    • RSPS Market
    • Others Market
    • Freelance Middleman Services
  • Super Secret Club's Topics

Find results in...

Find results that contain...


Date Created

  • Start

    End


Last Updated

  • Start

    End


Filter by number of...

Joined

  • Start

    End


Group


Found 2 results

  1. Okay so, I'm making a applet for my website. And it's full screen. The only thing I want is that one of my panels center in the container that stretches all over the screen. I used this code but, It doesn't do anything. [code] boundBG = pnlBG.getBounds(); boundLogin = pnlLogin.getBounds(); pnlLogin.setLocation((boundBG.width/2) - (boundLogin.width/2), (boundBG.height/2) - (boundLogin.height/2)); [/code] Help?
  2. Hey all, I've made some modifications to the Container class from the Hyperion source. It is aimed to improve performance, which is still an important factor even though Hyperion already has decent performance. And when talking about such a commonly used class as this one, every little bit of performance counts. I believe I've correctly renamed everything to Hyperion standards (since I improved it with the use of my own). I've mainly reduced the amount of loops which occurs in several methods, which is quite important when you've got a lot of players to process. There are also some methods added which you may find very useful for certain features, I know I certainly do. Here you are; [CODE]package org.hyperion.rs2.model.container; import static java.lang.System.arraycopy; import static java.util.Collections.unmodifiableCollection; import static org.hyperion.rs2.model.ItemDefinition.getItemDefinition; import java.util.Collection; import java.util.LinkedList; import java.util.List; import org.hyperion.rs2.model.Item; /** * A container holds a group of items. * * @author Graham * @author optimized by Mike * */ public final class Container { // Placed the max stack constant here, since it is located at other classes through many different sources. private static final int ITEM_STACK_CAPACITY = Integer.MAX_VALUE; /** * The type of container. * * @author Graham * */ public enum Type { /** * A standard container such as inventory. */ STANDARD, /** * A container which always stacks, e.g. the bank, regardless of the * item. */ ALWAYS_STACK, } /** * The capacity of this container. */ private final int capacity; /** * The items in this container. */ private Item[] items; /** * A list of listeners. */ private final List<ContainerListener> listeners = new LinkedList<ContainerListener>(); /** * The container type. */ private final Type type; /** * Firing events flag. */ private boolean firingEvents = true; /** * Creates the container with the specified capacity. * * @param type The type of this container. * @param capacity The capacity of this container. */ public Container(Type type, int capacity) { this.type = type; this.capacity = capacity; this.items = new Item[capacity]; } /** * Sets the firing events flag. * * @param firingEvents The flag. */ public void setFiringEvents(boolean firingEvents) { this.firingEvents = firingEvents; } /** * Checks the firing events flag. * * @return <code>true</code> if events are fired, <code>false</code> if not. */ public boolean isFiringEvents() { return firingEvents; } /** * Gets the listeners of this container. * * @return The listeners of this container. */ public Collection<ContainerListener> getListeners() { return unmodifiableCollection(listeners); } /** * Adds a listener. * * @param listener The listener to add. */ public void addListener(ContainerListener listener) { listeners.add(listener); listener.itemsChanged(this); } /** * Removes a listener. * * @param listener The listener to remove. */ public void removeListener(ContainerListener listener) { listeners.remove(listener); } /** * Removes all listeners. */ public void removeAllListeners() { listeners.clear(); } /** * Shifts all items to the top left of the container leaving no gaps. */ public void shift() { final Item[] old = items; items = new Item[capacity]; int newIndex = 0; for (int i = 0; i < capacity; i++) { if (old[i] != null) { items[newIndex] = old[i]; newIndex++; } } if (firingEvents) { fireItemsChanged(); } } /** * Gets the next free slot. * * @return The slot, or {@code -1} if there are no available slots. */ public int freeSlot() { for (int i = 0; i < capacity; i++) { if (items[i] == null) { return i; } } return -1; } /** * Gets the last free slot. * * @return The slot, or {@code -1} if there are no available slots. */ public int lastFreeSlot() { for (int i = capacity - 1; i >= 0; i--) { if (items[i] == null) { return i; } } return -1; } /** * Returns an {@code int} array with all free slots. This method returns an * empty array with {@code length} 0 if there are no free slots. * * @return All free slots. */ public int[] getFreeSlots() { final int count = freeSlots(); if (count == 0) { return new int[0]; } int[] slots = new int[count]; int ptr = -1; for (int i = 0; i < capacity; i++) { if (items[i] == null) { slots[(ptr += 1)] = i; } } return slots; } /** * Attempts to return an {@code int} array with the specified number of free * slots. Meaning that upon success, the returned array {@code length} would * be {@code count + 1}. * * @param count The amount of free slots to obtain. * @return An {@code int} array with the specified amount of slots to * obtain, or less if there aren't that many available. */ public int[] getFreeSlots(int count) { int[] slots = new int[count + 1]; int ptr = -1; for (int i = 0; i < capacity && ptr < count; i++) { if (items[i] == null) { slots[(ptr += 1)] = i; } } return slots; } /** * Returns an {@code int} array with all free slots, in reversed order. This * method returns an empty array with {@code length} 0 if there are no free * slots. * * @return All free slots. */ public int[] getFreeSlotsReversed() { final int count = freeSlots(); if (count == 0) { return new int[0]; } int[] slots = new int[count]; int ptr = capacity; for (int i = capacity - 1; i >= 0; i--) { if (items[i] == null) { slots[(ptr -= 1)] = i; } } return slots; } /** * Attempts to return an {@code int} array with the specified number of free * slots. Meaning that upon success, the returned array {@code length} would * be {@code count + 1}. * * @param count The amount of free slots to obtain. * @return An {@code int} array with the specified amount of slots to * obtain, or less if there aren't that many available. */ public int[] getFreeSlotsReversed(int count) { int[] slots = new int[count + 1]; int ptr = -1; for (int i = capacity - 1; i >= 0 && ptr < count; i--) { if (items[i] == null) { slots[(ptr -= 1)] = i; } } return slots; } /** * Gets the amount of free space this container has for left the specified * item ID. * * @param id The item ID. * @return The amount of free spaces (0 if none). */ public int freeSpace(int id) { if (getItemDefinition(id).isStackable() || type == Type.ALWAYS_STACK) { boolean freeSlot = false; for (Item i : items) { if (i != null) { if (i.getId() == id) { return ITEM_STACK_CAPACITY - i.getCount(); } } else { freeSlot = true; } } if (freeSlot) { return ITEM_STACK_CAPACITY; } } else { return freeSlots(); } return 0; } /** * Attempts to add an item into the next free slot. * * @param item The item. * @return <code>true</code> if the item was added, <code>false</code> if * not. */ public boolean add(Item item) { final int id = item.getId(); if (item.getDefinition().isStackable() || type.equals(Type.ALWAYS_STACK)) { for (int i = 0; i < capacity; i++) { if (items[i] != null && items[i].getId() == id) { final int totalCount = item.getCount() + items[i].getCount(); if (totalCount > ITEM_STACK_CAPACITY || totalCount < 1) { return false; } set(i, new Item(id, totalCount)); return true; } } final int slot = freeSlot(); if (slot == -1) { return false; } else { set(slot, item); return true; } } else { final int count = item.getCount(); final int[] slots = getFreeSlots(count); if (slots.length >= count) { final boolean b = firingEvents; firingEvents = false; try { for (int i = 0; i < count; i++) { set(slots[i], new Item(id)); } if (b) { fireItemsChanged(); } return true; } finally { firingEvents = b; } } else { return false; } } } /** * Attempts to add all {@link Item}s in the specified {@link Container} to * this container. * * @param c The container. * @return {@code true} if all items were added successfully, {@code false} * otherwise. */ public boolean addAll(Container c) { boolean b = firingEvents; firingEvents = false; try { for (Item item : c.toArray()) { if (item != null) { if (!add(item)) { return false; } } } if (b) { fireItemsChanged(); } return true; } finally { firingEvents = b; } } /** * Attempts to add all {@link Item}s in the specified {@link Container} to * this container, in reversed order. Please note that this adds the items * in <b>reversed order</b>, and does <b>not</b> actually starts at the last * free slot. For those actions, referr to * {@link #addAllReversedSlots(Container)}. * * @param c The container. * @return {@code true} if all items were added successfully, {@code false} * otherwise. */ public boolean addAllReversed(Container c) { boolean b = firingEvents; firingEvents = false; try { Item item; for (int i = capacity - 1; i >= 0; i--) { if ((item = c.items[i]) != null) { if (!add(item)) { return false; } } } return true; } finally { firingEvents = b; } } /** * Attempts to add all {@link Item}s in the specified {@link Container} to * this container, in reversed order and starting from the last slot. * * @param c The container * @return {@code true} if all items were added successfully, {@code false} * otherwise. */ public boolean addAllReversedSlots(Container c) { boolean b = firingEvents; firingEvents = false; try { Item item; for (int i = capacity - 1; i >= 0; i--) { if ((item = c.items[i]) != null) { if (!addReversed(item)) { return false; } } } return true; } finally { firingEvents = b; } } /** * Attempts to add an item into the last free slot. * * @param item The item. * @return <code>true</code> if the item was added, <code>false</code> if * not. */ public boolean addReversed(Item item) { final int id = item.getId(); if (item.getDefinition().isStackable() || type.equals(Type.ALWAYS_STACK)) { for (int i = capacity - 1; i >= 0; i--) { if (items[i] != null && items[i].getId() == id) { final int totalCount = item.getCount() + items[i].getCount(); if (totalCount > ITEM_STACK_CAPACITY || totalCount < 1) { return false; } set(i, new Item(id, totalCount)); return true; } } final int slot = lastFreeSlot(); if (slot == -1) { return false; } else { set(slot, item); return true; } } else { final int count = item.getCount(); final int[] slots = getFreeSlotsReversed(count); if (slots.length >= count) { final boolean b = firingEvents; firingEvents = false; try { for (int i = 0; i < count; i++) { set(lastFreeSlot(), new Item(id)); } if (b) { fireItemsChanged(); } return true; } finally { firingEvents = b; } } else { return false; } } } /** * Gets the number of free slots. * * @return The number of free slots. */ public int freeSlots() { return capacity - size(); } /** * Gets an item. * * @param index The position in the container. * @return The item. */ public Item get(int index) { return items[index]; } /** * Gets an item by ID. * * @param id The ID. * @return The item, or <code>null</code> if it could not be found. */ public Item getById(int id) { for (Item item : items) { if (item != null && item.getId() == id) { return item; } } return null; } /** * Gets a slot by ID. * * @param id The ID. * @return The slot, or {@code -1} if it could not be found. */ public int getSlotById(int id) { for (int i = 0; i < capacity; i++) { if (items[i] != null && items[i].getId() == id) { return i; } } return -1; } /** * Gets the last slot by ID. * * @param id The ID. * @return The slot, or {@code -1} if it could not be found. */ public int getLastSlotById(int id) { for (int i = capacity - 1; i >= 0; i--) { if (items[i] != null && items[i].getId() == id) { return i; } } return -1; } /** * Sets an item. * * @param index The position in the container. * @param item The item. */ public void set(int index, Item item) { items[index] = item; if (firingEvents) { fireItemChanged(index); } } /** * Gets the capacity of this container. * * @return The capacity of this container. */ public int capacity() { return capacity; } /** * Gets the size of this container. * * @return The size of this container. */ public int size() { int size = 0; for (Item item : items) { if (item != null) { size++; } } return size; } /** * Checks if this container is empty. * * @return {@code true} if so, {@code false} otherwise. */ public boolean isEmpty() { for (Item item : items) { if (item != null) { return false; } } return true; } /** * Clears this container. */ public void clear() { items = new Item[items.length]; if (firingEvents) { fireItemsChanged(); } } /** * Returns an array representing this container. * * @return The array. */ public Item[] toArray() { return items; } /** * Checks if a slot is used. * * @param slot The slot. * @return <code>true</code> if an item is present, <code>false</code> * otherwise. */ public boolean isSlotUsed(int slot) { return items[slot] != null; } /** * Checks if a slot is free. * * @param slot The slot. * @return <code>true</code> if an item is not present, <code>false</code> * otherwise. */ public boolean isSlotFree(int slot) { return items[slot] == null; } /** * Removes an item. * * @param item The item to remove. * @return The number of items removed. */ public int remove(Item item) { return remove(-1, item); } /** * Removes an item. * * @param preferredSlot The preferred slot to remove from. * @param item The item to remove. * @return The number of items removed. */ public int remove(int preferredSlot, Item item) { int removed = 0; if (item.getDefinition().isStackable() || type.equals(Type.ALWAYS_STACK)) { final int slot = getSlotById(item.getId()); final Item stack = get(slot); if (stack.getCount() > item.getCount()) { removed = item.getCount(); set(slot, new Item(stack.getId(), stack.getCount() - item.getCount())); } else { removed = stack.getCount(); set(slot, null); } } else { for (int i = 0; i < item.getCount(); i++) { int slot = getSlotById(item.getId()); if (i == 0 && preferredSlot != -1) { final Item inSlot = get(preferredSlot); if (inSlot.getId() == item.getId()) { slot = preferredSlot; } } if (slot != -1) { removed++; set(slot, null); } else { break; } } } return removed; } /** * Transfers an item from one container to another. * * @param from The container to transfer from. * @param to The container to transfer to. * @param fromSlot The slot in the original container. * @param id The item id. * @return A flag indicating if the transfer was successful. */ public static boolean transfer(Container from, Container to, int fromSlot, int id) { final Item fromItem = from.get(fromSlot); if (fromItem == null || fromItem.getId() != id) { return false; } if (to.add(fromItem)) { from.set(fromSlot, null); return true; } else { return false; } } /** * Returns a copy of the specified {@link Container}. * * @param c The container to copy. * @return The newly created container. */ public static Container copyOf(Container c) { final int capacity = c.capacity; final Container newC = new Container(c.type, capacity); arraycopy(c.items, 0, newC.items, 0, capacity); return newC; } /** * Swaps two items. * * @param fromSlot From slot. * @param toSlot To slot. */ public void swap(int fromSlot, int toSlot) { final Item temp = get(fromSlot); final boolean b = firingEvents; firingEvents = false; try { set(fromSlot, get(toSlot)); set(toSlot, temp); if (b) { fireItemsChanged(fromSlot, toSlot); } } finally { firingEvents = b; } } /** * Gets the total amount of an item, including the items in stacks. * * @param id The id. * @return The amount. */ public int getCount(int id) { int total = 0; for (Item item : items) { if (item != null && item.getId() == id) { total += item.getCount(); } } return total; } /** * Inserts an item. * * @param fromSlot The old slot. * @param toSlot The new slot. */ public void insert(int fromSlot, int toSlot) { // we reset the item in the from slot final Item from = items[fromSlot]; if (from == null) { return; } items[fromSlot] = null; // find which direction to shift in if (fromSlot > toSlot) { int shiftFrom = toSlot; int shiftTo = fromSlot; for (int i = (toSlot + 1); i < fromSlot; i++) { if (items[i] == null) { shiftTo = i; break; } } final Item[] slice = new Item[shiftTo - shiftFrom]; arraycopy(items, shiftFrom, slice, 0, slice.length); arraycopy(slice, 0, items, shiftFrom + 1, slice.length); } else { int sliceStart = fromSlot + 1; final int sliceEnd = toSlot; for (int i = (sliceEnd - 1); i >= sliceStart; i--) { if (items[i] == null) { sliceStart = i; break; } } final Item[] slice = new Item[sliceEnd - sliceStart + 1]; arraycopy(items, sliceStart, slice, 0, slice.length); arraycopy(slice, 0, items, sliceStart - 1, slice.length); } // now fill in the target slot items[toSlot] = from; if (firingEvents) { fireItemsChanged(); } } /** * Fires an item changed event. * * @param slot The slot that changed. */ public void fireItemChanged(int slot) { for (ContainerListener listener : listeners) { listener.itemChanged(this, slot); } } /** * Fires an items changed event. */ public void fireItemsChanged() { for (ContainerListener listener : listeners) { listener.itemsChanged(this); } } /** * Fires an items changed event. * * @param slots The slots that changed. */ public void fireItemsChanged(int... slots) { for (ContainerListener listener : listeners) { listener.itemsChanged(this, slots); } } /** * Checks if the container contains the specified item. * * @param id The item id. * @return <code>true</code> if so, <code>false</code> if not. */ public boolean contains(int id) { return getSlotById(id) != -1; } /** * Checks if this {@link Container} has enough room for the specified * {@link Item}. * * @param item The item. * @return {@code true} if so, {@code false} otherwise. */ public boolean hasRoomFor(Item item) { return freeSpace(item.getId()) >= item.getCount(); } /** * Replaces the content of this {@link Container} with the specified one's, * filling it up to this {@link Container}'s capacity. * * @param c The container to replace this container's content with. */ public void replace(Container c) { arraycopy(c.items, 0, items, 0, capacity - 1); if (firingEvents) { fireItemsChanged(); } } }[/CODE] If there are any questions, comments or (positive) criteria, do not hesitate to reply. I believe you should just be able to copy and paste it right into your old Container class, implying that you haven't renamed anything. - Mike
×