Class ShapeCanvas

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.Canvas
              |
              +--ShapeCanvas

class ShapeCanvas
extends java.awt.Canvas
implements java.awt.event.ActionListener, java.awt.event.ItemListener, java.awt.event.MouseListener, java.awt.event.MouseMotionListener

ShapeCanvas class This class represents a canvas that can display colored shapes and let the user drag them around. It uses an off-screen images to make the dragging look as smooth as possible.

Since:
11/10/2000
 Update History
 --------------
 Date		By		Description
 ----		--		-----------
 11/10/00	Asha Mony	Creation
See Also:
Serialized Form

Inner classes inherited from class java.awt.Component
java.awt.Component.AWTTreeLock
 
Field Summary
(package private)  java.awt.Color currentColor
           
(package private)  java.awt.Image offScreenCanvas
           
(package private)  java.awt.Graphics offScreenGraphics
           
(package private)  int prevDragX
           
(package private)  int prevDragY
           
(package private)  Shape shapeBeingDragged
           
(package private)  java.util.Vector shapes
           
 
Fields inherited from class java.awt.Canvas
base, graphicsConfig, nameCounter, serialVersionUID
 
Fields inherited from class java.awt.Component
actionListenerK, adjustmentListenerK, appContext, assert, background, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, changeSupport, componentListener, componentListenerK, componentOrientation, componentSerializedDataVersion, containerListenerK, cursor, dropTarget, enabled, eventMask, focusListener, focusListenerK, font, foreground, hasFocus, height, incRate, inputMethodListener, inputMethodListenerK, isInc, isPacked, itemListenerK, keyListener, keyListenerK, LEFT_ALIGNMENT, locale, LOCK, minSize, mouseListener, mouseListenerK, mouseMotionListener, mouseMotionListenerK, name, nameExplicitlySet, newEventsOnly, ownedWindowK, parent, peer, peerFont, popups, prefSize, RIGHT_ALIGNMENT, serialVersionUID, textListenerK, TOP_ALIGNMENT, valid, visible, width, windowListenerK, x, y
 
Constructor Summary
(package private) ShapeCanvas()
           
 
Method Summary
 void actionPerformed(java.awt.event.ActionEvent evt)
          Called to respond to action events.
(package private)  void addShape(Shape shape)
           
 void itemStateChanged(java.awt.event.ItemEvent evt)
          This is called to respond to item events.
(package private)  void makeOffScreenCanvas()
           
 void mouseClicked(java.awt.event.MouseEvent evt)
           
 void mouseDragged(java.awt.event.MouseEvent evt)
          User has moved the mouse.
 void mouseEntered(java.awt.event.MouseEvent evt)
           
 void mouseExited(java.awt.event.MouseEvent evt)
           
 void mouseMoved(java.awt.event.MouseEvent evt)
           
 void mousePressed(java.awt.event.MouseEvent evt)
          User has pressed the mouse.
 void mouseReleased(java.awt.event.MouseEvent evt)
          User has released the mouse.
 void paint(java.awt.Graphics g)
          In the paint method, everything is drawn to an off-screen canvas, and then that canvas is copied onto the screen.
 void update(java.awt.Graphics g)
          Update method is called when canvas is to be redrawn.
 
Methods inherited from class java.awt.Canvas
, addNotify, constructComponentName, postsOldMouseEvents
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addPropertyChangeListener, addPropertyChangeListener, areInputMethodsEnabled, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, deliverEvent, disable, disableEvents, dispatchEvent, dispatchEventImpl, doLayout, enable, enable, enableEvents, enableInputMethods, eventEnabled, firePropertyChange, getAlignmentX, getAlignmentY, getBackground, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentOrientation, getCursor, getDropTarget, getFont_NoClientCode, getFont, getFontMetrics, getForeground, getGraphics, getHeight, getInputContext, getInputMethodRequests, getIntrinsicCursor, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMinimumSize, getName, getNativeContainer, getParent_NoClientCode, getParent, getPeer, getPreferredSize, getSize, getSize, getToolkit, getToolkitImpl, getTreeLock, getWidth, getWindowForObject, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, initIDs, inside, invalidate, isDisplayable, isDoubleBuffered, isEnabled, isEnabledImpl, isFocusTraversable, isLightweight, isOpaque, isShowing, isValid, isVisible, keyDown, keyUp, layout, lightweightPrint, list, list, list, list, list, locate, location, lostFocus, minimumSize, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, paramString, postEvent, preferredSize, prepareImage, prepareImage, print, printAll, printHeavyweightComponents, processComponentEvent, processEvent, processFocusEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, readObject, remove, removeComponentListener, removeFocusListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFont, setForeground, setLocale, setLocation, setLocation, setName, setSize, setSize, setVisible, show, show, size, toString, transferFocus, validate, writeObject
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, wait, wait, wait
 

Field Detail

offScreenCanvas

java.awt.Image offScreenCanvas

offScreenGraphics

java.awt.Graphics offScreenGraphics

shapes

java.util.Vector shapes

currentColor

java.awt.Color currentColor

shapeBeingDragged

Shape shapeBeingDragged

prevDragX

int prevDragX

prevDragY

int prevDragY
Constructor Detail

ShapeCanvas

ShapeCanvas()
Method Detail

paint

public void paint(java.awt.Graphics g)
In the paint method, everything is drawn to an off-screen canvas, and then that canvas is copied onto the screen.
Overrides:
paint in class java.awt.Canvas
Since:
11/10/2000
 Update History
 --------------
 Date		By		Description
 ----		--		-----------
 11/10/00	Asha Mony	Creation

update

public void update(java.awt.Graphics g)
Update method is called when canvas is to be redrawn. Just call the paint method.
Overrides:
update in class java.awt.Component
Since:
11/10/2000
 Update History
 --------------
 Date		By		Description
 ----		--		-----------
 11/10/00	Asha Mony	Creation

makeOffScreenCanvas

void makeOffScreenCanvas()

itemStateChanged

public void itemStateChanged(java.awt.event.ItemEvent evt)
This is called to respond to item events. Such events can only be sent by the color choice menu, so respond by setting the current color according to the selected item in that menu.
Specified by:
itemStateChanged in interface java.awt.event.ItemListener
Since:
11/10/2000
 Update History
 --------------
 Date		By		Description
 ----		--		-----------
 11/10/00	Asha Mony	Creation

actionPerformed

public void actionPerformed(java.awt.event.ActionEvent evt)
Called to respond to action events. The three shape-adding buttons have been set up to send action events to this canvas. Respond by adding the appropriate shape to the canvas.
Specified by:
actionPerformed in interface java.awt.event.ActionListener
Since:
11/10/2000
 Update History
 --------------
 Date		By		Description
 ----		--		-----------
 11/10/00	Asha Mony	Creation

addShape

void addShape(Shape shape)

mousePressed

public void mousePressed(java.awt.event.MouseEvent evt)
User has pressed the mouse. Find the shape that the user has clicked on, if any. If there is a shape at the position when the mouse was clicked, then start dragging it. If the user was holding down the shift key, then bring the dragged shape to the front, in front of all the other shapes.
Specified by:
mousePressed in interface java.awt.event.MouseListener
Since:
11/10/2000
 Update History
 --------------
 Date		By		Description
 ----		--		-----------
 11/10/00	Asha Mony	Creation

mouseDragged

public void mouseDragged(java.awt.event.MouseEvent evt)
User has moved the mouse. Move the dragged shape by the same amount.
Specified by:
mouseDragged in interface java.awt.event.MouseMotionListener
Since:
11/10/2000
 Update History
 --------------
 Date		By		Description
 ----		--		-----------
 11/10/00	Asha Mony	Creation

mouseReleased

public void mouseReleased(java.awt.event.MouseEvent evt)
User has released the mouse. Move the dragged shape, then set shapeBeingDragged to null to indicate that dragging is over. if the shape lies completely outside the canvas, remove it from the list of shapes (since there is no way to ever move it back onscreen).
Specified by:
mouseReleased in interface java.awt.event.MouseListener
Since:
11/10/2000
 Update History
 --------------
 Date		By		Description
 ----		--		-----------
 11/10/00	Asha Mony	Creation

mouseEntered

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

mouseExited

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

mouseMoved

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

mouseClicked

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