net.luniks.jtvd
Class ImagePanel

java.lang.Object
  extended byjava.awt.Component
      extended byjava.awt.Container
          extended byjavax.swing.JComponent
              extended byjavax.swing.JPanel
                  extended bynet.luniks.jtvd.ImagePanel
All Implemented Interfaces:
javax.accessibility.Accessible, java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable

class ImagePanel
extends javax.swing.JPanel

This class provides the drawing area used to design the menu. It can have a plain color or an image as background and text items that can be selected and moved around with the arrow keys or by dragging them with the mouse. Any of these items have individual properties such as colors and font style. This class also provides methods to generate the background, highlight and select images for the menu.

Author:
Torsten Römer, luniks.net

Nested Class Summary
(package private)  class ImagePanel.Item
          This class provides a text item that can be drawn on for example a JPanel and selected and moved around with the arrow keys or by dragging it with the mouse.
(package private)  class ImagePanel.MenuItem
          This class is a special type of Item that can draw a shape or change its text color when it is clicked or "mouseovered".
 
Nested classes inherited from class javax.swing.JPanel
javax.swing.JPanel.AccessibleJPanel
 
Nested classes inherited from class javax.swing.JComponent
javax.swing.JComponent.AccessibleJComponent
 
Nested classes inherited from class java.awt.Container
java.awt.Container.AccessibleAWTContainer
 
Nested classes inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
private  java.awt.Color backgroundColor
           
private  java.awt.image.BufferedImage backgroundImage
           
private  java.awt.Rectangle borderRect
           
private  java.awt.BasicStroke borderStroke
           
static java.lang.String EFFECT_NONE
           
static java.lang.String EFFECT_SHAPE
           
static java.lang.String EFFECT_TEXT
           
private  java.lang.String highlightEffect
           
static int IMAGE_BACKGROUND
           
static int IMAGE_HIGHLIGHT
           
static int IMAGE_SELECT
           
private  java.util.ArrayList items
           
private  java.awt.image.BufferedImage scaledBackgroundImage
           
private  java.lang.String selectEffect
           
private  boolean showBackgroundImage
           
 
Fields inherited from class javax.swing.JPanel
 
Fields inherited from class javax.swing.JComponent
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Container
 
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()
          Sets the background color to white and focusable to true
 
Method Summary
 void addItem(ImagePanel.Item item)
          Adds the item given as argument to the drawing and draws it which implicitly makes it calculate its bounds.
 void arrangeMenuItems()
          Arranges all items of type MenuItem in columns.
 void check()
          Performs some checks to (try to) ensure that the menu will work, such as:
- If the menu contains no menu items at all
- If no menu effect has been assigned
- If menu items are (partially) outside the menu
- If menu items are too close to each other
If one of these checks fails, an exception with a descriptive message is thrown.
 void deleteItem(ImagePanel.Item item)
          Removes the item given as argument from the drawing, deletes it and repaints the drawing.
 java.awt.Color getBackgroundColor()
          Returns the current background color
 java.awt.image.IndexColorModel getColorModel(java.util.Set colors, int max)
          Returns an instance of IndexColorModel with a colormap created from the Set of colors given as argument.
 java.lang.String getHighlightEffect()
          Returns the type of effect used for highlighting menu buttons
 java.awt.image.BufferedImage getImage(int type, int width, int height)
          Returns a BufferedImage created from the drawing of the type given as argument type with the size given as arguments width and height.
 ImagePanel.Item[] getItems()
          Returns all items of the drawing
 ImagePanel.MenuItem[] getMenuItems()
          Returns all items of type MenuItem of the drawing
 ImagePanel.Item[] getSelectedItems()
          Returns all items of the drawing which are currently selected
 java.lang.String getSelectEffect()
          Returns the type of effect used for selecting menu buttons
 boolean getShowBackgroundImage()
          Returns true if the background image is shown or not
 void paintComponent(java.awt.Graphics g)
          Draws the background and items
 void removeAllMenuItems()
          Removes all items of type MenuItem from the drawing and repaints it.
 java.awt.image.BufferedImage scale(java.awt.image.BufferedImage image, int width, int height)
          Scales the BufferedImage given as first argument to the size given as the remaining arguments.
 void setBackgroundColor(java.awt.Color color)
          Sets the background color and repaints the drawing
 void setBackgroundImage(java.awt.image.BufferedImage image)
          Sets the image given as argument as background image, scales it and repaints the drawing
 void setHighlightEffect(java.lang.String effect)
          Sets the type of effect used for highlighting menu buttons and repaints the drawing.
 void setPanelSize(java.awt.Dimension size)
          Sets the size of the drawing to the dimensions given as argument and scales the background image to this size
 void setSelectEffect(java.lang.String effect)
          Sets the type of effect used for selecting menu buttons and repaints the drawing.
 void setShowBackgroundImage(boolean show)
          Sets if the background image should be shown or not and repaints the drawing
 void validateItemLocations()
          Calls validateLocation() on each item to make sure that no item's location is outside the drawing area
 
Methods inherited from class javax.swing.JPanel
getAccessibleContext, getUI, getUIClassID, paramString, setUI, updateUI
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getGraphics, getHeight, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isPreferredSizeSet, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFont, setForeground, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setLayout, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

IMAGE_BACKGROUND

public static final int IMAGE_BACKGROUND
See Also:
Constant Field Values

IMAGE_HIGHLIGHT

public static final int IMAGE_HIGHLIGHT
See Also:
Constant Field Values

IMAGE_SELECT

public static final int IMAGE_SELECT
See Also:
Constant Field Values

EFFECT_SHAPE

public static final java.lang.String EFFECT_SHAPE

EFFECT_TEXT

public static final java.lang.String EFFECT_TEXT

EFFECT_NONE

public static final java.lang.String EFFECT_NONE

backgroundImage

private java.awt.image.BufferedImage backgroundImage

scaledBackgroundImage

private java.awt.image.BufferedImage scaledBackgroundImage

backgroundColor

private java.awt.Color backgroundColor

showBackgroundImage

private boolean showBackgroundImage

highlightEffect

private java.lang.String highlightEffect

selectEffect

private java.lang.String selectEffect

items

private java.util.ArrayList items

borderRect

private java.awt.Rectangle borderRect

borderStroke

private java.awt.BasicStroke borderStroke
Constructor Detail

ImagePanel

public ImagePanel()
Sets the background color to white and focusable to true

Method Detail

getShowBackgroundImage

public boolean getShowBackgroundImage()
Returns true if the background image is shown or not

Returns:
true if the background image is shown, false otherwise

getBackgroundColor

public java.awt.Color getBackgroundColor()
Returns the current background color

Returns:
current background color

getHighlightEffect

public java.lang.String getHighlightEffect()
Returns the type of effect used for highlighting menu buttons

Returns:
current highlighting effect

getSelectEffect

public java.lang.String getSelectEffect()
Returns the type of effect used for selecting menu buttons

Returns:
current selecting effect

addItem

public void addItem(ImagePanel.Item item)
Adds the item given as argument to the drawing and draws it which implicitly makes it calculate its bounds. Registers the item as Mouse-, MouseMotion- and KeyListener.

Parameters:
item - the item to be added to the drawing

deleteItem

public void deleteItem(ImagePanel.Item item)
Removes the item given as argument from the drawing, deletes it and repaints the drawing. Unregisters the item as Mouse-, MouseMotion- and KeyListener.

Parameters:
item - the item to be removed from the drawing

removeAllMenuItems

public void removeAllMenuItems()
Removes all items of type MenuItem from the drawing and repaints it. Unregisteres all items as Mouse-, MouseMotion- and KeyListener.


getItems

public ImagePanel.Item[] getItems()
Returns all items of the drawing

Returns:
all items

getMenuItems

public ImagePanel.MenuItem[] getMenuItems()
Returns all items of type MenuItem of the drawing

Returns:
all menu items

getSelectedItems

public ImagePanel.Item[] getSelectedItems()
Returns all items of the drawing which are currently selected

Returns:
all selected items

arrangeMenuItems

public void arrangeMenuItems()
Arranges all items of type MenuItem in columns. This is probably a very clumsy implementation, if somebody could show me how to do this a bit smarter I would be grateful.


validateItemLocations

public void validateItemLocations()
Calls validateLocation() on each item to make sure that no item's location is outside the drawing area


setPanelSize

public void setPanelSize(java.awt.Dimension size)
Sets the size of the drawing to the dimensions given as argument and scales the background image to this size

Parameters:
size - the new size of the drawing

setBackgroundImage

public void setBackgroundImage(java.awt.image.BufferedImage image)
Sets the image given as argument as background image, scales it and repaints the drawing

Parameters:
image - the new background image

setShowBackgroundImage

public void setShowBackgroundImage(boolean show)
Sets if the background image should be shown or not and repaints the drawing

Parameters:
show - the background image or not

setBackgroundColor

public void setBackgroundColor(java.awt.Color color)
Sets the background color and repaints the drawing

Parameters:
color - the new background color

setHighlightEffect

public void setHighlightEffect(java.lang.String effect)
Sets the type of effect used for highlighting menu buttons and repaints the drawing. Possible values: EFFECT_TEXT, EFFECT_SHAPE and EFFECT_NONE

Parameters:
effect - for highlighting

setSelectEffect

public void setSelectEffect(java.lang.String effect)
Sets the type of effect used for selecting menu buttons and repaints the drawing. Possible values: EFFECT_TEXT, EFFECT_SHAPE and EFFECT_NONE

Parameters:
effect - for selecting

scale

public java.awt.image.BufferedImage scale(java.awt.image.BufferedImage image,
                                          int width,
                                          int height)
Scales the BufferedImage given as first argument to the size given as the remaining arguments. It uses Image.getScaledInstance with Image.SCALE_AREA_AVERAGING as hint. This gives a nicer quality than AffineTransformOp.filter with TYPE_BILINEAR.

Parameters:
image - the image to be scaled
width - the width to be scaled to
height - the height to be scaled to
Returns:
the scaled image

getColorModel

public java.awt.image.IndexColorModel getColorModel(java.util.Set colors,
                                                    int max)
Returns an instance of IndexColorModel with a colormap created from the Set of colors given as argument. The colormap includes transparency (color black with an alpha component of 0) and the first max - 1 colors from the set.


getImage

public java.awt.image.BufferedImage getImage(int type,
                                             int width,
                                             int height)
                                      throws java.lang.Exception
Returns a BufferedImage created from the drawing of the type given as argument type with the size given as arguments width and height. Possible types are IMAGE_BACKGROUND, IMAGE_HIGHLIGHT and IMAGE_SELECT

Returns:
image created from the drawing
Throws:
java.lang.Exception

paintComponent

public void paintComponent(java.awt.Graphics g)
Draws the background and items


check

public void check()
           throws java.lang.Exception
Performs some checks to (try to) ensure that the menu will work, such as:
- If the menu contains no menu items at all
- If no menu effect has been assigned
- If menu items are (partially) outside the menu
- If menu items are too close to each other
If one of these checks fails, an exception with a descriptive message is thrown.

Throws:
java.lang.Exception


Copyright (c) 2004, 2005 Torsten Römer, dode@luniks.net