Game-of-Life-WSU-CEG4180  v0.1
RefactoringtheGameofLife
Shape.java
Go to the documentation of this file.
1 /* This page is part of the Game of Life source code */
2 
3 /**
4  * Copyright 1996-2004 Edwin Martin <edwin@bitstorm.nl>
5  * @author Edwin Martin
6  */
7 
8 package org.bitstorm.gameoflife;
9 
10 import java.awt.Dimension;
11 import java.util.Enumeration;
12 
13 /**
14  * Shape contains data of one (predefined) shape.
15  *
16  * @author Edwin Martin
17  */
18 public class Shape {
19  private final String name;
20  private final int[][] shape;
21 
22  /**
23  * Constructa a Shape.
24  * @param name name of shape
25  * @param shape shape data
26  */
27  public Shape( String name, int[][] shape ) {
28  this.name = name;
29  this.shape = shape;
30  }
31 
32  /**
33  * Get dimension of shape.
34  * @return dimension of the shape in cells
35  */
36  public Dimension getDimension() {
37  int shapeWidth = 0;
38  int shapeHeight = 0;
39  for (int cell = 0; cell < shape.length; cell++) {
40  if (shape[cell][0] > shapeWidth)
41  shapeWidth = shape[cell][0];
42  if (shape[cell][1] > shapeHeight)
43  shapeHeight = shape[cell][1];
44  }
45  shapeWidth++;
46  shapeHeight++;
47  return new Dimension( shapeWidth, shapeHeight );
48  }
49 
50  /**
51  * Get name of shape.
52  * @return name of shape
53  */
54  public String getName() {
55  return name;
56  }
57 
58  /**
59  * Get shape data.
60  * Hide the shape implementation. Returns a anonymous Enumerator object.
61  * @return enumerated shape data
62  */
63  public Enumeration getCells() {
64  return new Enumeration() {
65  private int index=0;
66  public boolean hasMoreElements() {
67  return index < shape.length;
68  }
69  public Object nextElement() {
70  return shape[index++];
71  }
72  };
73  }
74 
75  /**
76  * @see java.lang.Object#toString()
77  */
78  public String toString() {
79  return name+" ("+shape.length+" cell"+(shape.length==1?"":"s")+")";
80  }
81 }
Shape(String name, int[][] shape)
Definition: Shape.java:27
Enumeration getCells()
Definition: Shape.java:63