edu.byu.phun2d
Class Group

java.lang.Object
  extended by edu.byu.phun2d.DrawObj
      extended by edu.byu.phun2d.Group

public class Group
extends DrawObj

A Group collects a series of other DrawObj objects to be drawn, including other GroupObjs. The Group allows a whole collection of drawable things to be manipulated as a whole. This simplifies building up complex scenes of many objects.

To build up a Group one first uses the constructor Group() to create the empty object. If this is added to a window or another Group, nothing will be seen because this group has no contents. The easiest way to provide contents is to add other DrawObj objects (line, rectangle, group etc.) using the add(DrawObj) method. These objects will be drawn in the order that they are added to the group. This means that if any of the objects overlap, the last ones in the group will appear on top.

It is also easy to get the objects that are part of a group. Each object in a group has an index (a number to reference that object). The first object in the group is at index zero, the second is at index 1, and so forth. To get an object use the getObjectAt(index) method. It is also possible to change one of those objects to something else. The setObjectAt(index, object) will change the object at the specified index to some new object. You can find out how many objects there are in a group using the nObjs() method.

Transforms

The Group also provides geometric transforms to change the size, orientation and location of the group as a whole. Normally the group's origin point is at (0,0). This can be moved using setTranslate(x,y) which will move the origin of the group to some point (x,y). This is a simple way to move a lot of things around the screen. The setRotate(angle) method will rotate all of the objects in the group around the the point (x,y). The setScale(sx,sy) method will change the size of the group. Scale values larger than one will make it bigger. Values between 0 and 1 will make it smaller. For example setScale(0.5,0.5) will make the group half as large. Please note that if you don't want an object scaled the correct scale factors are (1,1) not (0,0).


Constructor Summary
Group()
           
 
Method Summary
 void add(DrawObj newObj)
          This will add a new object to the list of objects in this group.
 DrawObj getObjectAt(int index)
          This will get an object from this group.
 int nObjs()
          This will return the number of objects in this group.
 void setFillColor(Color c)
          Generates an error message because you cannot change the fill color of a Group.
 void setLineColor(Color c)
          Generates an error message because you cannot change the line color of a Group.
 void setLineWidth(double width)
          Generates an error message because you cannot change the line width of a Group.
 void setObjectAt(int index, DrawObj newObj)
          This will change one of the existing objects in a group.
 
Methods inherited from class edu.byu.phun2d.DrawObj
getFillColor, getLineColor, getLineWidth, getRotate, getScaleX, getScaleY, getTranslate, insideToOutside, outsideToInside, setRotate, setScale, setTranslate
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Group

public Group()
Method Detail

add

public void add(DrawObj newObj)
This will add a new object to the list of objects in this group.

Parameters:
newObj - the new object to be added.

nObjs

public int nObjs()
This will return the number of objects in this group.

Returns:
the number of objects

getObjectAt

public DrawObj getObjectAt(int index)
This will get an object from this group. The object to retrieve is specified by index. The first object is at index zero and so forth. The last object in the group is at nObjs()-1.

Parameters:
index - the index of the object to be retrieved. If this is an invalid index (less than zero or beyond the end of the list) then null is returned.
Returns:
the object specified by index or null (if index is invalid).

setObjectAt

public void setObjectAt(int index,
                        DrawObj newObj)
This will change one of the existing objects in a group.

Parameters:
index - this is the index of the object to be changed. This should be greater than or equal to zero and less than nObjs().
newObj - This is the new object to be placed at the specified index. This will replace the old object.

setFillColor

public void setFillColor(Color c)
Generates an error message because you cannot change the fill color of a Group.

Overrides:
setFillColor in class DrawObj
Parameters:
c - the new fill color.

setLineColor

public void setLineColor(Color c)
Generates an error message because you cannot change the line color of a Group.

Overrides:
setLineColor in class DrawObj
Parameters:
c - the new line/border color.

setLineWidth

public void setLineWidth(double width)
Generates an error message because you cannot change the line width of a Group.

Overrides:
setLineWidth in class DrawObj
Parameters:
width - the new line width.