sm
Class SMCanvas

java.lang.Object
  extended byjava.awt.Component
      extended byjava.awt.Container
          extended byjavax.swing.JComponent
              extended byjavax.swing.JPanel
                  extended bysm.SMCanvas
All Implemented Interfaces:
javax.accessibility.Accessible, java.util.EventListener, java.awt.image.ImageObserver, InteractiveObject, java.awt.event.KeyListener, java.awt.MenuContainer, java.awt.event.MouseListener, java.awt.event.MouseMotionListener, java.io.Serializable

public class SMCanvas
extends javax.swing.JPanel
implements java.awt.event.MouseListener, java.awt.event.MouseMotionListener, java.awt.event.KeyListener, InteractiveObject

A canvas that manages a display list.

The display list contains SMShape objects. It is ordered from back to front, i.e. the frontmost object is the last in the list. Whenever the contents of the display list is modified, a repaint is triggered to update the display.

The state machines are used to manage interaction with the objects in the canvas. See StateMachine for a description of the syntax and use of state machines. Once a state machine is attached to a canvas and is active, it receives and handles all input events. The canvas may have several active state machines; they receive and handle the same events.

The events that are handled by the state machines are the mouse events, the keyboard events, the time out events (triggered by armTimer) and the gesture events.

Author:
Caroline Appert
See Also:
Serialized Form

Nested Class Summary
 
Nested classes inherited from class javax.swing.JComponent
javax.swing.JComponent.AccessibleJComponent
 
Field Summary
 
Fields inherited from class javax.swing.JComponent
TOOL_TIP_TEXT_KEY, 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
SMCanvas(int w, int h)
          Builds a SMCanvas.
 
Method Summary
 SMCanvas addShape(SMShape sms)
          Adds a shape to the canvas.
 void attachSM(StateMachine sm, boolean reset)
          Attaches a state machine to this interactive object to describe its behavior.
 SMCanvas attachSM(java.lang.String machine, boolean reset)
          Attaches a state machine to the canvas and resets it.
 boolean containsPoint(double x, double y)
          Tests if a point is in this interactive object.
 boolean containsPoint(java.awt.geom.Point2D point)
          Tests if a point is in this interactive object.
 void detachSM(StateMachine sm)
          Detaches a state machine from this interactive object.
 SMCanvas getCanvas()
          Returns the canvas in which this interactive object is displayed.
 java.awt.Dimension getPreferredSize()
           
 java.util.Vector getSMs()
          Returns the state machines attached to this canvas.
 boolean isAntialiased()
          Returns True if the canvas is antialiased.
 void keyboardEnable(boolean keyboard)
          Makes this canvas be able or disable to receive keyboard events.
 void keyPressed(java.awt.event.KeyEvent arg0)
           
 void keyReleased(java.awt.event.KeyEvent arg0)
           
 void keyTyped(java.awt.event.KeyEvent arg0)
           
 void mouseClicked(java.awt.event.MouseEvent arg0)
           
 void mouseDragged(java.awt.event.MouseEvent arg0)
           
 void mouseEntered(java.awt.event.MouseEvent arg0)
           
 void mouseExited(java.awt.event.MouseEvent arg0)
           
 void mouseMoved(java.awt.event.MouseEvent arg0)
           
 void mousePressed(java.awt.event.MouseEvent arg0)
           
 void mouseReleased(java.awt.event.MouseEvent arg0)
           
 SMEllipse newEllipse(double x, double y, double w, double h)
          Creates a new ellipse and adds it to the canvas.
 SMEllipse newEllipse(java.awt.geom.Point2D p, double w, double h)
          Creates a new ellipse and adds it to the canvas.
 SMImage newImage(double x, double y, java.lang.String file)
          Create a new image and adds it to the canvas
 SMPolyLine newPolyLine(double x, double y)
          Create a new polyline and adds it to the canvas.
 SMPolyLine newPolyLine(java.awt.geom.Point2D p)
          Create a new polyline and adds it to the canvas.
 SMRectangle newRectangle(double x, double y, double w, double h)
          Creates a new rectangle and adds it to the canvas.
 SMRectangle newRectangle(java.awt.geom.Point2D p, double w, double h)
          Creates a new rectangle and adds it to the canvas.
 SMSegment newSegment(double x1, double y1, double x2, double y2)
          Creates a new segment and adds it to the canvas.
 SMSegment newSegment(java.awt.geom.Point2D p1, java.awt.geom.Point2D p2)
          Creates a new segment and adds it to the canvas.
 SMNamedTag newTag(java.lang.String t)
          Returns a tag object given its name.
 SMText newText(double x, double y, java.lang.String text, java.awt.Font font)
          Create a new text and adds it to the canvas
 SMText newText(java.awt.geom.Point2D p, java.lang.String text, java.awt.Font font)
          Create a new text and adds it to the canvas
 SMWidget newWidget(javax.swing.JComponent b)
          Create a new widget and adds it to the canvas
 SMWidget newWidget(javax.swing.JComponent b, double x, double y)
          Create a new widget and adds it to the canvas
 SMWidget newWidget(javax.swing.JComponent b, double x, double y, double w, double h)
          Create a new widget and adds it to the canvas
 void paintComponent(java.awt.Graphics g)
           
 SMShape pick(java.awt.geom.Point2D p)
          Returns the topmost shape at a given position.
 java.util.LinkedList pickAll(java.awt.geom.Point2D p)
          Returns a list containing all the shapes at a given position.
 SMCanvas removeShape(SMShape sms)
          Removes a shape from the canvas.
 SMCanvas resume(StateMachine machine)
          Resumes the state machine machine if it is associated to this canvas.
 SMCanvas resume(java.lang.String machine)
          Resumes the state machine machine if it is associated to this canvas.
 SMCanvas setAntialiased(boolean a)
          Sets the antialiasing for every SMShape drawn in this canvas (global antialiasing)
 SMCanvas suspend(StateMachine machine)
          Suspends the state machine machine if it is associated to this canvas.
 SMCanvas suspend(java.lang.String machine)
          Suspends the state machine machine if it is associated to this canvas.
 
Methods inherited from class javax.swing.JPanel
getAccessibleContext, getUI, getUIClassID, 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, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getGraphics, getHeight, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, 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, paintImmediately, paintImmediately, print, printAll, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, 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, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, 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, remove, remove, removeAll, removeContainerListener, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, 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, createImage, createImage, createVolatileImage, createVolatileImage, dispatchEvent, enable, 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, 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
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SMCanvas

public SMCanvas(int w,
                int h)
Builds a SMCanvas.

Parameters:
w - The width of the canvas
h - The height of the canvas
Method Detail

getPreferredSize

public java.awt.Dimension getPreferredSize()

keyboardEnable

public void keyboardEnable(boolean keyboard)
Makes this canvas be able or disable to receive keyboard events.

Parameters:
keyboard - True if the canvas must be able to handle keyboard events, false otherwise.

paintComponent

public void paintComponent(java.awt.Graphics g)

mouseClicked

public void mouseClicked(java.awt.event.MouseEvent arg0)
Specified by:
mouseClicked in interface java.awt.event.MouseListener

mouseReleased

public void mouseReleased(java.awt.event.MouseEvent arg0)
Specified by:
mouseReleased in interface java.awt.event.MouseListener

mouseEntered

public void mouseEntered(java.awt.event.MouseEvent arg0)
Specified by:
mouseEntered in interface java.awt.event.MouseListener

mouseExited

public void mouseExited(java.awt.event.MouseEvent arg0)
Specified by:
mouseExited in interface java.awt.event.MouseListener

mouseDragged

public void mouseDragged(java.awt.event.MouseEvent arg0)
Specified by:
mouseDragged in interface java.awt.event.MouseMotionListener

mouseMoved

public void mouseMoved(java.awt.event.MouseEvent arg0)
Specified by:
mouseMoved in interface java.awt.event.MouseMotionListener

mousePressed

public void mousePressed(java.awt.event.MouseEvent arg0)
Specified by:
mousePressed in interface java.awt.event.MouseListener

keyTyped

public void keyTyped(java.awt.event.KeyEvent arg0)
Specified by:
keyTyped in interface java.awt.event.KeyListener

keyPressed

public void keyPressed(java.awt.event.KeyEvent arg0)
Specified by:
keyPressed in interface java.awt.event.KeyListener

keyReleased

public void keyReleased(java.awt.event.KeyEvent arg0)
Specified by:
keyReleased in interface java.awt.event.KeyListener

pick

public SMShape pick(java.awt.geom.Point2D p)
Returns the topmost shape at a given position.

Parameters:
p - The position.
Returns:
Returns the topmost shape under p, or null if there is no shape at this postion.

pickAll

public java.util.LinkedList pickAll(java.awt.geom.Point2D p)
Returns a list containing all the shapes at a given position.

Parameters:
p - The position.
Returns:
Returns the list of shapes, with the topmost shape first.

addShape

public SMCanvas addShape(SMShape sms)
Adds a shape to the canvas.

Parameters:
sms - the shape to add.
Returns:
Returns this canvas.

removeShape

public SMCanvas removeShape(SMShape sms)
Removes a shape from the canvas. Before being removed, the tags of this shape are removed, the shape is removed from its hierarchy (its parent and children are set to null), and its ghost, if any, is removed.

Parameters:
sms - The shape that have to be deleted.
Returns:
Returns this canvas.

newSegment

public SMSegment newSegment(java.awt.geom.Point2D p1,
                            java.awt.geom.Point2D p2)
Creates a new segment and adds it to the canvas.

Parameters:
p1 - The first point of the segment.
p2 - The second point of the segment.
Returns:
Returns the newly created segment.

newSegment

public SMSegment newSegment(double x1,
                            double y1,
                            double x2,
                            double y2)
Creates a new segment and adds it to the canvas.

Parameters:
x1 - The x-coordinate of the first point of the segment.
y1 - The y-coordinate of the first point of the segment.
x2 - The x-coordinate of the second point of the segment.
y2 - The y-coordinate of the second point of the segment.
Returns:
Returns the newly created segment.

newRectangle

public SMRectangle newRectangle(double x,
                                double y,
                                double w,
                                double h)
Creates a new rectangle and adds it to the canvas.

Parameters:
x - The x coordinate of the upper left point of the bounding box.
y - The y coordinate of the upper left point of the bounding box.
w - The width of the bounding box.
h - The height of the bounding box.
Returns:
Returns the newly created rectangle.

newRectangle

public SMRectangle newRectangle(java.awt.geom.Point2D p,
                                double w,
                                double h)
Creates a new rectangle and adds it to the canvas.

Parameters:
p - The upper left point of the bounding box.
w - The width of the bounding box.
h - The height of the bounding box.
Returns:
Returns the newly created rectangle.

newEllipse

public SMEllipse newEllipse(java.awt.geom.Point2D p,
                            double w,
                            double h)
Creates a new ellipse and adds it to the canvas.

Parameters:
p - The upper left point of the bounding box.
w - The width of the bounding box.
h - The height of the bounding box.
Returns:
Returns the newly created ellipse.

newEllipse

public SMEllipse newEllipse(double x,
                            double y,
                            double w,
                            double h)
Creates a new ellipse and adds it to the canvas.

Parameters:
x - The x coordinate of the upper left point of the bounding box.
y - The y coordinate of the upper left point of the bounding box.
w - The width of the bounding box.
h - The height of the bounding box.
Returns:
Returns the newly created ellipse.

newPolyLine

public SMPolyLine newPolyLine(java.awt.geom.Point2D p)
Create a new polyline and adds it to the canvas.

Parameters:
p - The starting point.
Returns:
Returns the newly created polyline.

newPolyLine

public SMPolyLine newPolyLine(double x,
                              double y)
Create a new polyline and adds it to the canvas.

Parameters:
x - The x coordinate of the starting point.
y - The y coordinate of the starting point.
Returns:
Returns the newly created polyline.

newText

public SMText newText(java.awt.geom.Point2D p,
                      java.lang.String text,
                      java.awt.Font font)
Create a new text and adds it to the canvas

Parameters:
p - The lower left point.
text - The text to display.
font - The font to use to render this text.
Returns:
the newly created text.

newText

public SMText newText(double x,
                      double y,
                      java.lang.String text,
                      java.awt.Font font)
Create a new text and adds it to the canvas

Parameters:
x - The x coordinate of the lower left point.
y - The y coordinate of the lower left point.
text - The text to display.
font - The font to use to render this text.
Returns:
the newly created text.

newImage

public SMImage newImage(double x,
                        double y,
                        java.lang.String file)
Create a new image and adds it to the canvas

Parameters:
x - The x coordinate of the lower left point.
y - The y coordinate of the lower left point.
file - The name of the file image (gif, jpeg or png)
Returns:
the newly created image.

newWidget

public SMWidget newWidget(javax.swing.JComponent b)
Create a new widget and adds it to the canvas

Parameters:
b - The swing widget.
Returns:
The newly created widget.

newWidget

public SMWidget newWidget(javax.swing.JComponent b,
                          double x,
                          double y)
Create a new widget and adds it to the canvas

Parameters:
b - The swing widget.
x - The x coordinate of the upper left point.
y - The y coordinate of the upper left point.
Returns:
The newly created widget.

newWidget

public SMWidget newWidget(javax.swing.JComponent b,
                          double x,
                          double y,
                          double w,
                          double h)
Create a new widget and adds it to the canvas

Parameters:
b - The swing widget.
x - The x coordinate of the upper left point.
y - The y coordinate of the upper left point.
w - The width of the bounding box.
h - The height of the bounding box.
Returns:
The newly created widget.

getSMs

public java.util.Vector getSMs()
Returns the state machines attached to this canvas.

Returns:
Returns the state machines attached to this canvas.

attachSM

public SMCanvas attachSM(java.lang.String machine,
                         boolean reset)
                  throws StateMachineNotfoundException
Attaches a state machine to the canvas and resets it.

Parameters:
machine - The name of the state machine.
reset - True if the machine must be reset.
Returns:
Returns this canvas
Throws:
StateMachineNotfoundException - if the machine does not exist

suspend

public SMCanvas suspend(StateMachine machine)
Suspends the state machine machine if it is associated to this canvas. Once suspended, a state machine does not process events until it is resumed.

Parameters:
machine - The state machine.
Returns:
Returns this canvas
See Also:
StateMachine.suspend()

suspend

public SMCanvas suspend(java.lang.String machine)
                 throws StateMachineNotfoundException
Suspends the state machine machine if it is associated to this canvas. Once suspended, a state machine does not process events until it is resumed.

Parameters:
machine - The name of the state machine.
Returns:
Returns this canvas
Throws:
StateMachineNotfoundException - if the machine does not exist
See Also:
StateMachine.suspend()

resume

public SMCanvas resume(StateMachine machine)
Resumes the state machine machine if it is associated to this canvas.

Parameters:
machine - The state machine.
Returns:
Returns this canvas
See Also:
StateMachine.resume()

resume

public SMCanvas resume(java.lang.String machine)
                throws StateMachineNotfoundException
Resumes the state machine machine if it is associated to this canvas.

Parameters:
machine - The name of the state machine.
Returns:
Returns this canvas
Throws:
StateMachineNotfoundException - if the machine does not exist
See Also:
StateMachine.resume()

isAntialiased

public boolean isAntialiased()
Returns True if the canvas is antialiased.

Returns:
Returns the value of the global antialiasing.

setAntialiased

public SMCanvas setAntialiased(boolean a)
Sets the antialiasing for every SMShape drawn in this canvas (global antialiasing)

Parameters:
a - True if the canvas is antialiased.
Returns:
Returns this canvas

newTag

public SMNamedTag newTag(java.lang.String t)
Returns a tag object given its name. If a tag with this name already exists, return it, otherwise create a new one.

Parameters:
t - The name of the tag
Returns:
The tag.

getCanvas

public SMCanvas getCanvas()
Description copied from interface: InteractiveObject
Returns the canvas in which this interactive object is displayed.

Specified by:
getCanvas in interface InteractiveObject
Returns:
Returns the canvas.

attachSM

public void attachSM(StateMachine sm,
                     boolean reset)
Description copied from interface: InteractiveObject
Attaches a state machine to this interactive object to describe its behavior.

Specified by:
attachSM in interface InteractiveObject
Parameters:
sm - The State machine that describes the behavior of this interactive object.
reset - True if the machine must be reset, false otherwise.

detachSM

public void detachSM(StateMachine sm)
Description copied from interface: InteractiveObject
Detaches a state machine from this interactive object.

Specified by:
detachSM in interface InteractiveObject
Parameters:
sm - The State machine to detach.

containsPoint

public boolean containsPoint(java.awt.geom.Point2D point)
Description copied from interface: InteractiveObject
Tests if a point is in this interactive object.

Specified by:
containsPoint in interface InteractiveObject
Parameters:
point - The point to test.
Returns:
Returns true if point is in this interactive object, false otherwise.

containsPoint

public boolean containsPoint(double x,
                             double y)
Description copied from interface: InteractiveObject
Tests if a point is in this interactive object.

Specified by:
containsPoint in interface InteractiveObject
Parameters:
x - The x-coordinate of the point to test.
y - The y-coordinate of the point to test.
Returns:
Returns true if (x, y) is in this interactive object, false otherwise.