NGP.Graphics
Class Shape

java.lang.Object
  extended byNGP.Graphics.Shape
All Implemented Interfaces:
Colorable, EventListener, Graphic, MouseListener, MouseMotionListener, Reactor, Rotatable
Direct Known Subclasses:
Line, RectangularShape

public abstract class Shape
extends Object
implements Graphic, Colorable

A geometric object that knows how to draw itself on the screen.

Author:
Matt Chotin (mhc)

Field Summary
protected  Shape _awtShape
          The AWT geometric shape used for drawing
protected  DrawingPanel _dpanel
          The DrawingPanel that contains this Shape
 
Fields inherited from interface NGP.Colorable
DEFAULT_GRAY
 
Constructor Summary
Shape(DrawingPanel dpanel, Shape s)
          Create a Shape with the specified DrawingPanel
 
Method Summary
protected abstract  void actualPaint(Graphics2D g)
          Subclasses will define how painting actually works, filled or framed
 boolean contains(Point p)
          Determine if a Point is within this shape.
 void drag(MouseEvent e)
          Override to do something useful.
 Rectangle getBounds()
          Find the bounding rectangle of this Shape.
 Point getCenterLocation()
          Return the Point that represents the center of the bounding rectangle of this shape.
 Color getColor()
          Get the color of the Shape
 DrawingPanel getDrawingPanel()
          Return the DrawingPanel for this Shape
 int getRotation()
          Get the rotation of the Shape.
 void hide()
          Hide the Shape so it won't be drawn anymore (NGP will lose it's reference to it).
 boolean intersects(Graphic g)
          Determine if the passed in Graphic intersects with this Shape.
 void mouseClicked(MouseEvent e)
          Called when the Panel detects that the mouse was clicked.
 void mouseDragged(MouseEvent e)
          Called when the Panel detects that the mouse was dragged.
 void mouseEntered(MouseEvent e)
          Called when the Panel detects that the mouse entered.
 void mouseExited(MouseEvent e)
          Called when the Panel detects that the mouse exited.
 void mouseMoved(MouseEvent e)
          Called when the Panel detects that the mouse was moved.
 void mousePressed(MouseEvent e)
          Called when the Panel detects that the mouse was pressed.
 void mouseReleased(MouseEvent e)
          Called when the Panel detects that the mouse was released.
 void paint(Graphics2D g)
          Normal users need not use this!
 void react()
          Override to do something useful.
 void setColor(Color color)
          Set the color of the Shape
 void setDrawingPanel(DrawingPanel dp)
          Set the DrawingPanel of this Shape so that it appears somewhere else.
 void setRotation(int degrees)
          Set the rotation of the Shape.
 void show()
          Show the shape so it will be drawn (NGP now has a reference to it).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_dpanel

protected DrawingPanel _dpanel
The DrawingPanel that contains this Shape


_awtShape

protected Shape _awtShape
The AWT geometric shape used for drawing

Constructor Detail

Shape

public Shape(DrawingPanel dpanel,
             Shape s)
Create a Shape with the specified DrawingPanel

Parameters:
dpanel - the DrawingPanel for this Shape
Method Detail

hide

public void hide()
Hide the Shape so it won't be drawn anymore (NGP will lose it's reference to it).

Specified by:
hide in interface Graphic

show

public void show()
Show the shape so it will be drawn (NGP now has a reference to it).

Specified by:
show in interface Graphic

paint

public void paint(Graphics2D g)
Normal users need not use this!

This is how we draw graphics using Java. First set the color of the Graphics Context. Then find out what rotation we want, the finally paint. Then reset the rotation if necessary.

Specified by:
paint in interface Graphic
Parameters:
g - the Graphics2D for us to use.
See Also:
java.awt.Graphics2D

actualPaint

protected abstract void actualPaint(Graphics2D g)
Subclasses will define how painting actually works, filled or framed


setColor

public void setColor(Color color)
Set the color of the Shape

Specified by:
setColor in interface Colorable
Parameters:
color - the Color for the Shape
See Also:
java.awt.Color

getColor

public Color getColor()
Get the color of the Shape

Specified by:
getColor in interface Colorable
Returns:
the Color for the Shape
See Also:
setColor

setDrawingPanel

public void setDrawingPanel(DrawingPanel dp)
Set the DrawingPanel of this Shape so that it appears somewhere else.

Specified by:
setDrawingPanel in interface Graphic
Parameters:
dp - the new DrawingPanel for the Shape
See Also:
getDrawingPanel

getDrawingPanel

public DrawingPanel getDrawingPanel()
Return the DrawingPanel for this Shape

Specified by:
getDrawingPanel in interface Graphic
Returns:
the DrawingPanel for this Shape
See Also:
setDrawingPanel

contains

public boolean contains(Point p)
Determine if a Point is within this shape. This works even if the Shape is rotated.

Specified by:
contains in interface Graphic
Parameters:
p - the Point to check for containment
Returns:
true if the Point is within the shape, otherwise false

intersects

public boolean intersects(Graphic g)
Determine if the passed in Graphic intersects with this Shape.

Specified by:
intersects in interface Graphic
Parameters:
g - the Graphic we are testing against
Returns:
true if it does intersect, false if not
See Also:
getBounds

setRotation

public void setRotation(int degrees)
Set the rotation of the Shape.

Specified by:
setRotation in interface Rotatable
Parameters:
degrees - the degrees (clockwise) to rotate the Shape
See Also:
getRotation

getRotation

public int getRotation()
Get the rotation of the Shape.

Specified by:
getRotation in interface Rotatable
Returns:
the degrees (clockwise) the Shape is rotated
See Also:
setRotation

getBounds

public Rectangle getBounds()
Find the bounding rectangle of this Shape. This takes rotation into account.

Specified by:
getBounds in interface Graphic
Returns:
the bounding java.awt.Rectangle of this Shape

getCenterLocation

public Point getCenterLocation()
Return the Point that represents the center of the bounding rectangle of this shape.

Specified by:
getCenterLocation in interface Graphic
Returns:
the java.awt.Point of the center of this shape

react

public void react()
Override to do something useful. Called when the mouse is clicked within this Shape

Specified by:
react in interface Reactor

drag

public void drag(MouseEvent e)
Override to do something useful.


mouseClicked

public void mouseClicked(MouseEvent e)
Called when the Panel detects that the mouse was clicked. If the click is within this Shape, react will be called.

Specified by:
mouseClicked in interface MouseListener

mouseDragged

public void mouseDragged(MouseEvent e)
Called when the Panel detects that the mouse was dragged. If this shape is active, then drag will be called.

Specified by:
mouseDragged in interface MouseMotionListener

mouseEntered

public void mouseEntered(MouseEvent e)
Called when the Panel detects that the mouse entered. Does nothing.

Specified by:
mouseEntered in interface MouseListener

mouseExited

public void mouseExited(MouseEvent e)
Called when the Panel detects that the mouse exited. Does nothing.

Specified by:
mouseExited in interface MouseListener

mousePressed

public void mousePressed(MouseEvent e)
Called when the Panel detects that the mouse was pressed. Sets this Shape to be active. This means that if the mouse is subsequently dragged, the drag(MouseEvent e) method of the Shape will be called.

Specified by:
mousePressed in interface MouseListener

mouseReleased

public void mouseReleased(MouseEvent e)
Called when the Panel detects that the mouse was released. Makes this shape inactive if it is active. Ensures that mouse drags that originate outside of this shape will not invoke the drag(MouseEvent e) method.

Specified by:
mouseReleased in interface MouseListener

mouseMoved

public void mouseMoved(MouseEvent e)
Called when the Panel detects that the mouse was moved. Does nothing.

Specified by:
mouseMoved in interface MouseMotionListener