mdh.hephaestus2.gamelib
Class ImagePanel

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.Container
              |
              +--java.awt.Panel
                    |
                    +--mdh.hephaestus2.gamelib.DoublePanel
                          |
                          +--mdh.hephaestus2.gamelib.ImagePanel
All Implemented Interfaces:
Accessible, ImageObserver, MenuContainer, Serializable

public class ImagePanel
extends DoublePanel

A double-buffered panel with an image background. The image can be aligned, tiled, or zoomed in various ways with setAlign(). You can use the ImagePanel just like any other container.

An ImagePanel is created and set up like this:

 new ImagePanel().setImage(img).setAlign(a);
 

If you do not set the alignment, it will be centered.

The image will be loaded from class resources if possible (i.e., inside the same jar), or the filesystem if not.

When started from the command line, a frame containing an ImagePanel will be shown. The filename of the image is the first argument, the alignment name is the optional second argument; if left off, the image is centered. To dismiss the frame, hit 'q' or close the window.

Author:
Copyright © 2002 by Mark Hughes <kamikaze@kuoi.asui.uidaho.edu>
See Also:
Serialized Form

Field Summary
static int A_CENTER
          Alignment: Centered on the visible area
static int A_E
          Alignment: East
static int A_N
          Alignment: North
static int A_NE
          Alignment: Northeast
static int A_NW
          Alignment: Northwest
static int A_S
          Alignment: South
static int A_SE
          Alignment: Southeast
static int A_SW
          Alignment: Southwest
static int A_TILE
          Alignment: Tiled across the visible area
static int A_W
          Alignment: West
static int A_ZOOM
          Alignment: Zoomed to fill the visible area
static String[] ALIGN_NAME
          Text equivalents of the constants above.
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
ImagePanel()
          Creates a new ImagePanel.
ImagePanel(LayoutManager layout)
          Creates a new ImagePanel with the given layout manager.
 
Method Summary
 void draw(Graphics2D g)
          Draws the background image.
 int getAlign()
          Returns the current alignment.
 Image getImage(String filename)
          Returns the Image for a given filename, or null if not found.
 Dimension getMinimumSize()
           
 Dimension getPreferredSize()
           
static void main(String[] args)
          Testing utility.
 ImagePanel setAlign(int a)
          Sets the alignment of the background image, which must be one of the A_* constants.
 ImagePanel setAlign(String a)
          Sets the alignment of the background image, which must be one of the ALIGN_NAME values.
 ImagePanel setImage(Image i)
          Sets the background image.
 ImagePanel setImage(String filename)
          Loads 'filename' and sets it as the background image.
 ImagePanel setImage(URL url)
          Loads 'url' and sets it as the background image.
 
Methods inherited from class mdh.hephaestus2.gamelib.DoublePanel
isDoubleBuffered, isFixedSize, isFocusable, isFocusTraversable, paint, redraw, repaintNow, setSize, update
 
Methods inherited from class java.awt.Panel
addNotify, getAccessibleContext
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getInsets, getLayout, getListeners, getMaximumSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, print, printComponents, remove, remove, removeAll, removeContainerListener, removeNotify, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setFont, setLayout, transferFocusBackward, transferFocusDownCycle, validate
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, disable, dispatchEvent, enable, enable, enableInputMethods, getBackground, getBounds, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusOwner, isFontSet, isForegroundSet, isLightweight, isOpaque, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, printAll, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocusInWindow, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeysEnabled, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

A_N

public static final int A_N
Alignment: North

See Also:
Constant Field Values

A_NE

public static final int A_NE
Alignment: Northeast

See Also:
Constant Field Values

A_E

public static final int A_E
Alignment: East

See Also:
Constant Field Values

A_SE

public static final int A_SE
Alignment: Southeast

See Also:
Constant Field Values

A_S

public static final int A_S
Alignment: South

See Also:
Constant Field Values

A_SW

public static final int A_SW
Alignment: Southwest

See Also:
Constant Field Values

A_W

public static final int A_W
Alignment: West

See Also:
Constant Field Values

A_NW

public static final int A_NW
Alignment: Northwest

See Also:
Constant Field Values

A_CENTER

public static final int A_CENTER
Alignment: Centered on the visible area

See Also:
Constant Field Values

A_TILE

public static final int A_TILE
Alignment: Tiled across the visible area

See Also:
Constant Field Values

A_ZOOM

public static final int A_ZOOM
Alignment: Zoomed to fill the visible area

See Also:
Constant Field Values

ALIGN_NAME

public static final String[] ALIGN_NAME
Text equivalents of the constants above.

Constructor Detail

ImagePanel

public ImagePanel()
Creates a new ImagePanel.


ImagePanel

public ImagePanel(LayoutManager layout)
Creates a new ImagePanel with the given layout manager.

Method Detail

main

public static void main(String[] args)
                 throws Exception
Testing utility.

Exception

draw

public void draw(Graphics2D g)
Draws the background image.

Specified by:
draw in class DoublePanel

getAlign

public final int getAlign()
Returns the current alignment.


getImage

public Image getImage(String filename)
Returns the Image for a given filename, or null if not found.


getMinimumSize

public Dimension getMinimumSize()
Overrides:
getMinimumSize in class DoublePanel

getPreferredSize

public Dimension getPreferredSize()
Overrides:
getPreferredSize in class DoublePanel

setAlign

public final ImagePanel setAlign(int a)
Sets the alignment of the background image, which must be one of the A_* constants.

Returns:
'this', so you can chain it: new ImagePanel().setImage(img).setAlign(a);
Throws:
IllegalArgumentException - if 'a' is unknown.

setAlign

public final ImagePanel setAlign(String a)
Sets the alignment of the background image, which must be one of the ALIGN_NAME values.

Returns:
'this', so you can chain it: new ImagePanel().setImage(img).setAlign(a);
Throws:
IllegalArgumentException - if 'a' is unknown.

setImage

public final ImagePanel setImage(Image i)
Sets the background image.

Returns:
'this', so you can chain it: new ImagePanel().setImage(img).setAlign(a);

setImage

public final ImagePanel setImage(String filename)
Loads 'filename' and sets it as the background image. Prints an error message to stderr if unable to load.

Returns:
'this', so you can chain it: new ImagePanel().setImage(file).setAlign(a);

setImage

public final ImagePanel setImage(URL url)
Loads 'url' and sets it as the background image. Prints an error message to stderr if unable to load.

Returns:
'this', so you can chain it: new ImagePanel().setImage(url).setAlign(a);