Button.java :  » Game » javadungeoncrawlerv01 » editor » menu » Java Open Source

Java Open Source » Game » javadungeoncrawlerv01 
javadungeoncrawlerv01 » editor » menu » Button.java
package editor.menu;

import java.awt.Graphics;

import applet.resource.Sprite1D;


/** */
public class Button {
  /** */
  public static enum State {
    /** */
    NORMAL(0), HOVER(1), PRESSED(2);

    /** */
    private int frame;

    /**
     * @param newFrame
     *            - the frame corresponding to this state.
     */
    private State(final int newFrame) {
      frame = newFrame;
    }

    /** @return the frame corresponding to this state. */
    public final int getFrame() {
      return frame;
    }
  }

  /** */
  public static final int WIDTH = 96;
  /** */
  public static final int HEIGHT = 32;

  /** */
  private static final int ITEM_X = 4;
  /** */
  private static final int ITEM_Y = 4;
  /** */
  private static final Sprite1D SPRITE = new Sprite1D("editor/button-2.png",
      3);

  /** */
  private int index;
  /** */
  private String name;
  /** */
  private Item item;
  /** */
  private JumpList jumpList;
  /** */
  private State state;

  /**
   * @param newIndex
   *            - the position of the button.
   * @param newName
   *            - the name of this button.
   */
  public Button(final int newIndex, final String newName) {
    index = newIndex;
    name = newName;
    item = null;
    jumpList = null;
    state = State.NORMAL;
  }

  /** @return the position of this button. */
  public final int getIndex() {
    return index;
  }

  /**
   * @param newState
   *            - the state to go to.
   */
  public final void setState(final State newState) {
    state = newState;
  }

  /**
   * @param newItem
   *            - the item attached to this button.
   */
  public final void setItem(final Item newItem) {
    item = newItem;
  }

  /**
   * @param newJumpList
   *            - the jump list attached to this button.
   */
  public final void setJumpList(final JumpList newJumpList) {
    jumpList = newJumpList;
  }

  /** @return the item attached to this button. */
  public final Item getItem() {
    return item;
  }

  /** @return the name of this button. */
  public final String getName() {
    return name;
  }

  /** @return the jump list attached to this button. */
  public final JumpList getJumpList() {
    return jumpList;
  }

  /**
   * @param g
   *            - the graphics context to draw to.
   */
  public final void draw(final Graphics g) {
    final int x = index * WIDTH;
    SPRITE.draw(g, state.getFrame(), x, Menu.Y);
    if (item != null) {
      item.draw(g, x + ITEM_X, Menu.Y + ITEM_Y);
    }
  }

  /** @return the state. */
  public final State getState() {
    return state;
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.