org.peace_tools.generic
Class CustomPanel

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.JPanel
                  extended by org.peace_tools.generic.CustomPanel
All Implemented Interfaces:
java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, javax.accessibility.Accessible

public class CustomPanel
extends javax.swing.JPanel

A Custom panel that can have an Image for its Background

This class provides a CustomPanel for fancier display. The CustomPanel extends the Java Swing JPanel class and customizes its functionality by provding additional option to use a Image (PNG/JPEG/Gif) as the background for the JPanel. The graphic formats (such as GIF/JPEG etc.) supported will depend on the Java version being used.This image provides a fancy display on top of which the other GUI objects class can be conveniently displayed.

This class provides all the functionality of the JPanel class. Refer to the API documentation on the JPanel class for additional information on the methods that can be invoked on this class.

The image to be used for the background must be set through a suitable call to the setImage() method. By default this class has no image.

See Also:
JPanel, ImageIcon, Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.JPanel
javax.swing.JPanel.AccessibleJPanel
 
Nested classes/interfaces inherited from class javax.swing.JComponent
javax.swing.JComponent.AccessibleJComponent
 
Nested classes/interfaces inherited from class java.awt.Container
java.awt.Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BaselineResizeBehavior, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
private  javax.swing.ImageIcon backgroundImage
          The image used for the background This member object is used to maintain the image that is used as the background for this panel.
private static long serialVersionUID
          The serial version ID associated with this class for serialization.
private  boolean useImageHeight
          Flag to indicate if the size of the image should be used as the preferred size for this panel.
private  boolean useImageWidth
          Flag to indicate if the size of the image should be used as the preferred size for this panel.
 
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.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
CustomPanel()
          Creates a default CustomPanel Object.
CustomPanel(java.awt.LayoutManager layout)
          Creates a default CustomPanel Object.
 
Method Summary
(package private)  javax.swing.ImageIcon getImage()
          Obtain the image associated with this panel (if any).
 java.awt.Dimension getPreferredSize()
          Provide feedback on the preferred size of this panel This method overrides the default implementation of the getPreferredSize() method in the super class.
 void paintComponent(java.awt.Graphics g)
          Paint the background image as and when necessary This method is automatically invoked by the core Java AWT runtime engine whenever the background for the CustomPanel needs to be repainted.
 boolean setImage(java.lang.String fileName)
          Set the image to be used for the background This method can be used to set the image that must be used as the background by the CustomPanel object.
 void setUseImageSize(boolean useImageWidth, boolean useImageHeight)
          Method to set if the panel should use the background image size as the preferred size for the panel.
 
Methods inherited from class javax.swing.JPanel
getAccessibleContext, getUI, getUIClassID, paramString, setUI, updateUI
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, 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, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, 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, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, 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, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

useImageWidth

private boolean useImageWidth
Flag to indicate if the size of the image should be used as the preferred size for this panel.


useImageHeight

private boolean useImageHeight
Flag to indicate if the size of the image should be used as the preferred size for this panel.


backgroundImage

private javax.swing.ImageIcon backgroundImage
The image used for the background This member object is used to maintain the image that is used as the background for this panel.


serialVersionUID

private static final long serialVersionUID
The serial version ID associated with this class for serialization.

See Also:
Constant Field Values
Constructor Detail

CustomPanel

public CustomPanel()
Creates a default CustomPanel Object.

This constructor can be used to create a default CustomPanel object. Once the CustomPanel object has been created, its properties can be further modified through suitable methods.

By default the CustomPanel has the same settings as the default JPanel (super) object. In addition, the default background image is set to null, indicating that there is no background image set. In this case, the background color set for the panel is automatically used by the super class.


CustomPanel

public CustomPanel(java.awt.LayoutManager layout)
Creates a default CustomPanel Object.

This constructor can be used to create a default CustomPanel object. Once the CustomPanel object has been created, its properties can be further modified through suitable methods.

By default the CustomPanel has the same settings as the default JPanel (super) object. In addtion, the default background image is set to null, indicating that there is no background image set. In this case, the background color set for the panel is automatically used by the super class.

Parameters:
layout - The layout manager to be set for this panel.
Method Detail

setImage

public boolean setImage(java.lang.String fileName)
Set the image to be used for the background This method can be used to set the image that must be used as the background by the CustomPanel object. The file name (with necessary path) must be passed in as the parameter. This method automatically loads the image from the file and utilizes it for painting the background. The image can be a GIF or JPEG. Other formats may be supported but compatibility with a given format depends on the Java version being used.

If the image is loaded successfully from the file, this method returns true. In addtion, it updates the size of the panel to reflect the dimensions of the image so that the entire image can be displayed. If an error occurs this method returns false and the CustomContainer reverts back to its earlier image (if any).

Parameters:
fileName - The name of the image file to be used
Returns:
This method returns true on success and false on failure.

setUseImageSize

public void setUseImageSize(boolean useImageWidth,
                            boolean useImageHeight)
Method to set if the panel should use the background image size as the preferred size for the panel.

Parameters:
useImageWidth - If the parameter is true, then the panel reports the width of the background image size as the preferred image width.
useImageHeight - If the parameter is true, then the panel reports the height of the background image size as the preferred image height.

getImage

javax.swing.ImageIcon getImage()
Obtain the image associated with this panel (if any).

Returns:
The image associated with this panel. This method returns null if an image has not been set.

paintComponent

public void paintComponent(java.awt.Graphics g)
Paint the background image as and when necessary This method is automatically invoked by the core Java AWT runtime engine whenever the background for the CustomPanel needs to be repainted. The Graphics object that must be used to paint the panel is passed in as the parameter by the core AWT engine.

This method paints the background image, if a valid background image is available. Otherwise it simply drops control to the super JPanel class. Refer to the documentation on the paint() method on the JPanel class for information on the working of the JPanel.

Overrides:
paintComponent in class javax.swing.JComponent
Parameters:
g - The graphics object to be used for painting.
See Also:
JPanel, Graphics

getPreferredSize

public java.awt.Dimension getPreferredSize()
Provide feedback on the preferred size of this panel This method overrides the default implementation of the getPreferredSize() method in the super class. This method basically returns the current size of the CustomPanel. If an image has been specified for this panel, this method basically returns the size of the image (unless the size has been modified after the image was set).

Overrides:
getPreferredSize in class javax.swing.JComponent
Returns:
The preferred size for this CustomPanel
See Also:
JPanel