sofia.graphics
Class Shape

java.lang.Object
  extended by greenfoot.sofiainternal.Actor
      extended by sofia.graphics.Shape
Direct Known Subclasses:
sofia.graphics.ImageShape

public abstract class Shape
extends greenfoot.sofiainternal.Actor

Write a one-sentence summary of your class here. Follow it with additional details about its purpose, what abstraction it represents, and how to use it.

Version:
$Date: 2012/08/04 16:32 $
Author:
Tony Allevato, Last changed by $Author: edwards $

Constructor Summary
Shape()
          Creates a new shape with default bounds (located at the center of the screen and 100 pixels wide by 100 pixels tall).
Shape(android.graphics.RectF bounds)
          Creates a new shape with the specified bounds.
 
Method Summary
 void addOther(Shape newShape)
          Add another shape to the same view (or parent) containing this shape.
protected  void conditionallyRelayout()
          Called to recalculate the layout of the shape on the screen.
protected  void conditionallyRepaint()
          Called to indicate that the shape needs to be repainted on the screen.
 boolean contains(float x, float y)
           Gets a value indicating whether the specified pixel location is contained in the receiver.
 boolean contains(android.graphics.PointF point)
          Gets a value indicating whether the specified pixel location is contained in the receiver.
 sofia.graphics.ViewEdges extendsOutside(android.graphics.RectF bounds)
          Determine whether any part of this shape extends outside the given rectangle.
 int getAlpha()
          A convenience method that gets the alpha (opacity) component of the shape's color.
 android.graphics.RectF getBounds()
          Gets the bounding rectangle of the shape.
 sofia.graphics.Color getColor()
          Gets the color of the receiver.
 float getHeight()
          Gets the height of the shape, in pixels.
 android.graphics.PointF getPosition()
          Gets the origin (top-left corner) of the receiver.
 android.graphics.PointF getPositionAnchor()
          Get the current position anchor, which is an offset relative to the upper left corner of the shape that is used as the shape's "origin" for the purposes of getting/setting x-y positions.
 float getRotation()
          Gets the current angle of rotation of the shape, in degrees clockwise.
 android.graphics.PointF getRotationPivot()
          Gets the point around which the shape's rotation will pivot.
 sofia.graphics.ShapeParent getShapeParent()
          Gets the parent of the receiver.
 float getWidth()
          Gets the width of the shape, in pixels.
 float getX()
          Gets the x-coordinate of the anchor point of the shape's bounding box (the top-left corner, by default).
 float getY()
          Gets the y-coordinate of the anchor point of the shape's bounding box (the top-left corner, by default).
 int getZIndex()
          Gets the z-index of the receiver.
 boolean intersects(Shape otherShape)
          Determine whether this shape intersects another, based on their bounding boxes.
 boolean isInFrontOf(Shape other)
          Returns true if this shape is drawn in front of (later than) the other shape.
 boolean isVisible()
          Gets a value indicating whether the receiver is visible (drawn on the screen).
 void move(float dx, float dy)
          Moves the receiver by the specified horizontal and vertical distance.
protected  void notifyParentOfPositionChange()
          Called to recalculate the layout of the shape on the screen.
 void onBoundsResolved()
          Called when the bounds of the shape have been resolved.
 void remove()
          Remove this shape from its view (or parent).
 void rotateBy(float angleDelta)
          Increments the shape's rotation by the specified number of degrees, around the same pivot point that was used previously (or the center of the shape if no other pivot has been previously used).
 void setAlpha(int newAlpha)
          A convenience method that sets the alpha (opacity) component of the shape's color without changing the other color components.
 void setBounds(android.graphics.RectF newBounds)
          Sets the bounding rectangle of the shape.
 void setColor(sofia.graphics.Color newColor)
          Sets the color of the receiver.
 void setPosition(sofia.graphics.PointAndAnchor pointAndAnchor)
          Sets the position of the receiver based on the specified point and anchor, leaving its size unchanged.
 void setPosition(android.graphics.PointF position)
          Sets the origin (top-left corner) of the receiver.
 void setPositionAnchor(sofia.graphics.Anchor anchor)
          Set the position anchor, which is an offset relative to the upper left corner of the shape that is used as the shape's "origin" for the purposes of getting/setting x-y positions.
 void setPositionAnchor(android.graphics.PointF anchor)
          Set the position anchor, which is an offset relative to the upper left corner of the shape that is used as the shape's "origin" for the purposes of getting/setting x-y positions.
 void setRotation(float newRotation)
          Sets the angle of rotation of the shape in degrees clockwise, using the center of the shape's bounding box as the pivot point.
 void setRotation(float newRotation, android.graphics.PointF newPivot)
          Sets the angle of rotation of the shape in degrees clockwise, using the specified point as the pivot point.
 void setVisible(boolean newVisible)
          Sets a value indicating whether the receiver is visible (drawn on the screen).
 void setX(float x)
          Sets the x-coordinate of the anchor point of the shape's bounding box (the top-left corner, by default).
 void setY(float y)
          Sets the y-coordinate of the anchor point of the shape's bounding box (the top-left corner, by default).
 void setZIndex(int newZIndex)
          Sets the z-index of the receiver.
 java.lang.String toString()
          Returns a human-readable string representation of the shape.
 
Methods inherited from class greenfoot.sofiainternal.Actor
_gf_addToWorld, _gf_getBoundingXs, _gf_getBoundingYs, _gf_getDelegate, _gf_getImage, _gf_getIntersectingObjects, _gf_getNeighbours, _gf_getObjectsAtOffset, _gf_getObjectsInRange, _gf_getOneIntersectingObject, _gf_getOneObjectAtOffset, _gf_getRotation, _gf_getX, _gf_getY, _gf_setImage, _gf_setImage, _gf_setLocation, _gf_setRotation, _gf_turn, act, addedToWorld, containsPoint, getWorld, intersects, move, turnTowards
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Shape

public Shape()
Creates a new shape with default bounds (located at the center of the screen and 100 pixels wide by 100 pixels tall).


Shape

public Shape(android.graphics.RectF bounds)
Creates a new shape with the specified bounds.

Parameters:
bounds - The bounds of the shape.
Method Detail

addOther

public void addOther(Shape newShape)
Add another shape to the same view (or parent) containing this shape.

Parameters:
newShape - The other shape to add.

remove

public void remove()
Remove this shape from its view (or parent).


getBounds

public android.graphics.RectF getBounds()
Gets the bounding rectangle of the shape. The top-left corner of the bounding rectangle is the shape's origin, and the bottom-right corner is the shape's extent.

Returns:
The bounding rectangle of the shape.

setBounds

public void setBounds(android.graphics.RectF newBounds)
Sets the bounding rectangle of the shape.

Parameters:
newBounds - The new bounding rectangle of the shape.

getPositionAnchor

public android.graphics.PointF getPositionAnchor()
Get the current position anchor, which is an offset relative to the upper left corner of the shape that is used as the shape's "origin" for the purposes of getting/setting x-y positions. The default anchor is (0, 0) unless it has been explicitly set.

Returns:
The current position anchor.

setPositionAnchor

public void setPositionAnchor(android.graphics.PointF anchor)
Set the position anchor, which is an offset relative to the upper left corner of the shape that is used as the shape's "origin" for the purposes of getting/setting x-y positions. This does not change the shape's current position, but will change the behavior of future calls to setX()/setY()/setPosition() and getX()/getY()/getPosition().

Parameters:
anchor - The new position anchor.

setPositionAnchor

public void setPositionAnchor(sofia.graphics.Anchor anchor)
Set the position anchor, which is an offset relative to the upper left corner of the shape that is used as the shape's "origin" for the purposes of getting/setting x-y positions. This does not change the shape's current position, but will change the behavior of future calls to setX()/setY()/setPosition() and getX()/getY()/getPosition().

Parameters:
anchor - The new position anchor.

getRotation

public float getRotation()
Gets the current angle of rotation of the shape, in degrees clockwise.

Returns:
The current angle of rotation of the shape.

getRotationPivot

public android.graphics.PointF getRotationPivot()
Gets the point around which the shape's rotation will pivot. By default, the center of the shape's bounding box is used.

Returns:
The point around which the shape's rotation will pivot.

setRotation

public void setRotation(float newRotation)
Sets the angle of rotation of the shape in degrees clockwise, using the center of the shape's bounding box as the pivot point.

Parameters:
newRotation - The new angle of rotation of the shape.

setRotation

public void setRotation(float newRotation,
                        android.graphics.PointF newPivot)
Sets the angle of rotation of the shape in degrees clockwise, using the specified point as the pivot point.

Parameters:
newRotation - The new angle of rotation of the shape.
newPivot - The point around which the rotation will pivot.

rotateBy

public void rotateBy(float angleDelta)
Increments the shape's rotation by the specified number of degrees, around the same pivot point that was used previously (or the center of the shape if no other pivot has been previously used).

Parameters:
angleDelta - The number of degrees to add to the shape's rotation.

getX

public float getX()
Gets the x-coordinate of the anchor point of the shape's bounding box (the top-left corner, by default).

Returns:
The x-coordinate of the anchor point of the shape's bounding box.
See Also:
setPositionAnchor(Anchor)

setX

public void setX(float x)
Sets the x-coordinate of the anchor point of the shape's bounding box (the top-left corner, by default). This moves the shape, so calling this method also causes the extent of the shape to change, keeping with width the same.

Parameters:
x - The x-coordinate of the anchor point of the shape's bounding box.
See Also:
setPositionAnchor(Anchor)

getY

public float getY()
Gets the y-coordinate of the anchor point of the shape's bounding box (the top-left corner, by default).

Returns:
The y-coordinate of the anchor point of the shape's bounding box.
See Also:
setPositionAnchor(Anchor)

setY

public void setY(float y)
Sets the y-coordinate of the anchor point of the shape's bounding box (the top-left corner, by default). This moves the shape, so calling this method also causes the extent of the shape to change, keeping with height the same.

Parameters:
y - The y-coordinate of the anchor point of the shape's bounding box.
See Also:
setPositionAnchor(Anchor)

getWidth

public float getWidth()
Gets the width of the shape, in pixels.

Returns:
The width of the shape.

getHeight

public float getHeight()
Gets the height of the shape, in pixels.

Returns:
The height of the shape.

getPosition

public android.graphics.PointF getPosition()
Gets the origin (top-left corner) of the receiver. Be aware that the PointF.x and PointF.y fields of the returned point may not be valid if layout of the shapes has not yet occurred.

Returns:
A PointF object describing the origin of the shape.

setPosition

public void setPosition(android.graphics.PointF position)
Sets the origin (top-left corner) of the receiver.

Parameters:
position - A PointF object describing the origin of the shape.

setPosition

public void setPosition(sofia.graphics.PointAndAnchor pointAndAnchor)
Sets the position of the receiver based on the specified point and anchor, leaving its size unchanged.

Parameters:
pointAndAnchor - A PointAndAnchor object describing the position of the shape.

move

public void move(float dx,
                 float dy)
Moves the receiver by the specified horizontal and vertical distance. Positive values move the shape to the right or down, and negative values move it to the left or up.

Parameters:
dx - The number of pixels to move the shape horizontally.
dy - The number of pixels to move the shape vertically.

contains

public boolean contains(float x,
                        float y)

Gets a value indicating whether the specified pixel location is contained in the receiver.

By default, this method checks to see whether the point is located within the bounding rectangle of the shape. Shapes where this would produce incorrect results, such as ovals or lines, override this method accordingly.

This method does take the shape's rotation into account. This means that if you subclass Shape and need to provide logic that is different from the default bounding box behavior, then you may need to undo the rotation of the incoming point before testing it against your shape's bounds.

Parameters:
x - The x-coordinate.
y - The y-coordinate.
Returns:
True if the shape contains the point, otherwise false.

contains

public boolean contains(android.graphics.PointF point)
Gets a value indicating whether the specified pixel location is contained in the receiver.

Parameters:
point - The point.
Returns:
True if the shape contains the point, otherwise false.

onBoundsResolved

public void onBoundsResolved()
Called when the bounds of the shape have been resolved. Subclasses may want to override this if they need to update their state when this occurs.


getZIndex

public int getZIndex()
Gets the z-index of the receiver. A shape with a higher z-index will be drawn on top of a shape with a lower z-index. By default, shapes are created with a z-index of 0.

Returns:
The z-index of the shape.

setZIndex

public void setZIndex(int newZIndex)
Sets the z-index of the receiver.

Parameters:
newZIndex - The new z-index of the shape.

isInFrontOf

public boolean isInFrontOf(Shape other)
Returns true if this shape is drawn in front of (later than) the other shape.

Parameters:
other - The shape to check against.
Returns:
True if this shape is drawn in front of (later than) the other.

getShapeParent

public final sofia.graphics.ShapeParent getShapeParent()
Gets the parent of the receiver.

Returns:
The parent of the receiver.

getColor

public sofia.graphics.Color getColor()
Gets the color of the receiver.

Returns:
The color of the receiver.

setColor

public void setColor(sofia.graphics.Color newColor)
Sets the color of the receiver.

Parameters:
newColor - The new color of the receiver.

getAlpha

public int getAlpha()
A convenience method that gets the alpha (opacity) component of the shape's color.

Returns:
The alpha component of the shape's color, where 0 means that the color is fully transparent and 255 means that it is fully opaque.

setAlpha

public void setAlpha(int newAlpha)
A convenience method that sets the alpha (opacity) component of the shape's color without changing the other color components.

Parameters:
newAlpha - The new alpha component of the shape's color, where 0 means that the color is fully transparent and 255 means that it is fully opaque.

isVisible

public boolean isVisible()
Gets a value indicating whether the receiver is visible (drawn on the screen). Invisible shapes also do not receive touch events.

Returns:
True if the shape is visible, otherwise false.

setVisible

public void setVisible(boolean newVisible)
Sets a value indicating whether the receiver is visible (drawn on the screen).

Parameters:
newVisible - True if the shape should be visible, otherwise false.

conditionallyRepaint

protected void conditionallyRepaint()
Called to indicate that the shape needs to be repainted on the screen. Most users will not need to call this method, because modifying a property of the shape such as its color will repaint it automatically.


conditionallyRelayout

protected void conditionallyRelayout()
Called to recalculate the layout of the shape on the screen. Most users will not need to call this method, because modifying a property of the shape such as its position and size will cause the layout to be recalculated automatically.


notifyParentOfPositionChange

protected void notifyParentOfPositionChange()
Called to recalculate the layout of the shape on the screen. Most users will not need to call this method, because modifying a property of the shape such as its position and size will cause the layout to be recalculated automatically.


toString

public java.lang.String toString()
Returns a human-readable string representation of the shape.

Overrides:
toString in class java.lang.Object
Returns:
A human-readable string representation of the shape.

intersects

public boolean intersects(Shape otherShape)
Determine whether this shape intersects another, based on their bounding boxes.

Parameters:
otherShape - The other shape to check against.
Returns:
True if this shape and the other shape intersect.

extendsOutside

public sofia.graphics.ViewEdges extendsOutside(android.graphics.RectF bounds)
Determine whether any part of this shape extends outside the given rectangle.

Parameters:
bounds - The rectangle to check against.
Returns:
A ViewEdges object indicating whether any part of this shape extends outside the top, bottom, left, or right side of the bounds.


Greenfoot homepage