solitaire
Class GuiCardProxy

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 solitaire.GuiCardProxy
All Implemented Interfaces:
java.awt.event.MouseListener, java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, java.util.EventListener, javax.accessibility.Accessible, IGuiCard

public class GuiCardProxy
extends javax.swing.JPanel
implements IGuiCard, java.awt.event.MouseListener

The GUI component that represents a card in the Solitaire package. A GuiCardProxy object is a decorator for a "real" card in that it implements the IGuiCard interface but also wraps an IGuiCard object, e.g., as set via this class's setCard method. An object of this class listen's for mouse events and reports a click as an event that holds both the card clicked (via it's id) and the pile the card is in (from the setPile method). A click generates a Solitaire-semantic event, not a Java/awt mouseclick event. Listeners to these semantic events receive every such event, adding a listener is a class-wide/static process using the addSolitaireListener method. Although all events are received by each listener, the events encode the card (and the pile) clicked on when Proxy objects are used properly.

To use a GuiCardProxy for a "real" card in a game, typically the GuiPileView class does the work. Most programs will use that class rather than this class. Note that the rank/suit constructor for this class is private, GuiCardProxy objects are typically accessed via the static getProxy method. However, "blank" cards that report a string when clicked (as opposed to an ID that conforms to the contract in IGuiCard) can be constructed directly.

Proxy objects that represent real cards (not blank cards) can be toggled via the toggleSelected method which grays-out a card (or sets it to not-grayed-out). Alternatively, the setSelected method can set a proxy card to display as gray or not rather than toggling the proxy's state.

See GuiPileView for more information and the sample Solitaire game in GuiDemo (the latter is not in this package).

See Also:
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.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
 
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 solitaire.IGuiCard
CLUBS, DIAMONDS, HEARTS, rankPrefixes, SPADES, suitPrefixes
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
GuiCardProxy(java.lang.String s)
          Construct a blank-card, e.g., as used on a free-cell pile when there's nothing there.
 
Method Summary
static void addSolitaireListener(ISolitaireListener isl)
          Add a listener for every SolitaireEvent generated when some card is clicked.
 void fireSolitaireEvent(SolitaireEvent event)
          Fire a solitaire event to all listeners.
 java.lang.String getCardId()
          Return the id of the card this wraps, or the string of a blank card.
static GuiCardProxy getProxy(java.lang.String id)
          Return the GuiCardProxy object for a specific card id --- where the id conforms to the contract described in IGuiCard
 IGuiCard getWrappedCard()
          Return the wrapped IGuiCard for this proxy.
 boolean isFaceUp()
          Reports state of wrapped card, or true if there is no wrapped card.
 void mouseClicked(java.awt.event.MouseEvent e)
          Listen for mouse clicks and report them to listeners as semantic events.
 void mouseEntered(java.awt.event.MouseEvent e)
          Required by MouseListener interface, currently does nothing.
 void mouseExited(java.awt.event.MouseEvent e)
          Required by MouseListener interface, currently does nothing.
 void mousePressed(java.awt.event.MouseEvent e)
          Required by MouseListener interface, currently does nothing.
 void mouseReleased(java.awt.event.MouseEvent e)
          Required by MouseListener interface, currently does nothing.
 void paintComponent(java.awt.Graphics g)
          Paint appropriate image (card, grayed-image, blank, or back) when painting is needed, e.g., when game is displayed or cards moved.
static void removeSolitaireListener(ISolitaireListener isl)
          Remove a listener from the list of Proxy listeners
 void setCard(IGuiCard card)
          Set the "real" card for which this card is a Gui/proxy.
 void setCoordinates(int x, int y, int width, int height)
          Set coordinates for the bounds of this card.
 void setPile(GuiPileView pile)
          Set the pile in which the card wrapped by this proxy is stored.
 void setSelected(boolean selected)
          Set selected state (true means gray image).
 void toggleSelected()
          Change state of selected/gray, e.g., if false, make it true and vice versa.
 java.lang.String toString()
           
 
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, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, 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, 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, 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, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

GuiCardProxy

public GuiCardProxy(java.lang.String s)
Construct a blank-card, e.g., as used on a free-cell pile when there's nothing there. The blank card displays as white (modifiable by changing CardImageFactory) and reports the string by which it's constructed when it's clicked on. Proxy cards constructed this way aren't displayed as gray when the toggleSelected/setSelected methods are called.

Parameters:
s - is the string reported as this proxy's id
Method Detail

addSolitaireListener

public static void addSolitaireListener(ISolitaireListener isl)
Add a listener for every SolitaireEvent generated when some card is clicked. Note that any listener will receive events from every card, but the event encodes the card actually clicked.

Parameters:
isl - is the listener added

removeSolitaireListener

public static void removeSolitaireListener(ISolitaireListener isl)
Remove a listener from the list of Proxy listeners

Parameters:
isl - is the removed listener (previously added)

fireSolitaireEvent

public void fireSolitaireEvent(SolitaireEvent event)
Fire a solitaire event to all listeners.

Parameters:
event - is the event sent to all listeners (via there processSolitaireEvent method).

getProxy

public static GuiCardProxy getProxy(java.lang.String id)
Return the GuiCardProxy object for a specific card id --- where the id conforms to the contract described in IGuiCard

Parameters:
id - is the card id
Returns:
the proxycard object for the given id
See Also:
IGuiCard

toString

public java.lang.String toString()
Overrides:
toString in class java.awt.Component

setPile

public void setPile(GuiPileView pile)
Set the pile in which the card wrapped by this proxy is stored. Note that the pile-view likely corresponds to a model (see IGuiPileModel) in which the wrapped card is stored.

Failure to set the pile appropriately can result in incorrect semantic events being generated when a card is clicked.

Parameters:
pile - is the pile/view in which this proxy's wrapped IGuiCard card can be found.

setCard

public void setCard(IGuiCard card)
Set the "real" card for which this card is a Gui/proxy. Failure to set this appropriately will result in improperly displayed cards and semantic solitaire events that may not work properly in the context of a game.

Parameters:
card - is the card wrapped by this proxy

getCardId

public java.lang.String getCardId()
Return the id of the card this wraps, or the string of a blank card.

Specified by:
getCardId in interface IGuiCard
Returns:
this proxy's id (the wrapped id, or a different id for each blank card properly constructed).

getWrappedCard

public IGuiCard getWrappedCard()
Return the wrapped IGuiCard for this proxy.

Returns:
the wrapped/decorated card for this proxy

setCoordinates

public void setCoordinates(int x,
                           int y,
                           int width,
                           int height)
Set coordinates for the bounds of this card. Only GuiPileView should call this method directly unless you really know what you're doing when you call it. This method calls setBounds so the card is likely redrawn/repainted.

Parameters:
x - coordinate of upper-left of card image
y - coordinate of upper-left of card image
width - of card image shown
height - of card image shown

paintComponent

public void paintComponent(java.awt.Graphics g)
Paint appropriate image (card, grayed-image, blank, or back) when painting is needed, e.g., when game is displayed or cards moved.

Overrides:
paintComponent in class javax.swing.JComponent

isFaceUp

public boolean isFaceUp()
Reports state of wrapped card, or true if there is no wrapped card.

Specified by:
isFaceUp in interface IGuiCard
Returns:
state of face-up for wrapped card.

setSelected

public void setSelected(boolean selected)
Set selected state (true means gray image).

Parameters:
selected - determines if card is gray, true means gray.

toggleSelected

public void toggleSelected()
Change state of selected/gray, e.g., if false, make it true and vice versa.


mouseClicked

public void mouseClicked(java.awt.event.MouseEvent e)
Listen for mouse clicks and report them to listeners as semantic events. Such events store card and pile for access by listeners.

Specified by:
mouseClicked in interface java.awt.event.MouseListener

mousePressed

public void mousePressed(java.awt.event.MouseEvent e)
Required by MouseListener interface, currently does nothing.

Specified by:
mousePressed in interface java.awt.event.MouseListener

mouseReleased

public void mouseReleased(java.awt.event.MouseEvent e)
Required by MouseListener interface, currently does nothing.

Specified by:
mouseReleased in interface java.awt.event.MouseListener

mouseEntered

public void mouseEntered(java.awt.event.MouseEvent e)
Required by MouseListener interface, currently does nothing.

Specified by:
mouseEntered in interface java.awt.event.MouseListener

mouseExited

public void mouseExited(java.awt.event.MouseEvent e)
Required by MouseListener interface, currently does nothing.

Specified by:
mouseExited in interface java.awt.event.MouseListener