1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
public class VectorObject {
private Paint background = new Color(128, 128, 128, 128);
private Paint foreground = Color.BLACK;
private Stroke stroke = new BasicStroke(1.0f);
private Font font = new Font("Dialog", Font.PLAIN, 12);
private Shape delegated;
private String name;
/** Creates a new instance with the given shape.
* @param shape The delegated shape.
* @exception IllegalArgumentException If the given shape is <code>null</code>.
*/
public VectorObject(Shape shape) {
if (shape == null) {
throw new IllegalArgumentException("The given shape is null");
}
delegated = shape;
}
/** Gets the bounding box of this object in double precision.
* @return A <code>Rectange2D</code> or never <code>null</code>.
*/
public Rectangle2D getBounds2D() {
return delegated.getBounds2D();
}
/** Draw this object.
* @param graphics The graphics context in which to paint.
*/
public void draw(Graphics2D graphics) {
Shape shape = getShape();
if (isFilled()) {
graphics.setPaint(getBackground());
graphics.fill(shape);
}
if (isStroked()) {
graphics.setPaint(getForeground());
graphics.setStroke(getStroke());
graphics.draw(shape);
}
...
}
}
public class VectorGroup {
private List<VectorObject> objectList = new LinkedList();
private String name;
...
/** Creates a new group with given shapes.
* @param shapes The initial list of shapes.
* @exception IllegalArgumentException If one of the given shape is <code>null</code>.
*/
public VectorGroup(Shape... shapes) throws IllegalArgumentException {
for (Shape shape : shapes) {
VectorObject object = new VectorObject(shape);
objectList.add(object);
}
}
/** Creates a new group with given vector objects.
* @param objects The initial list of objects.
*/
public VectorGroup(VectorObject... objects) {
for (VectorObject : objects) {
objectList.add(object);
}
}
/** Gets the size of this group.
*/
public int size() {
return objectList.size();
}
/** Get object at index.
* @param index The value index.
* @return The value at index.
* @exception IndexOutOfBoundsException If the given index is out of bounds.
*/
public VectorObject get(int index) throws IndexOutOfBoundsException {
return objectList.get(index);
}
/** Add an object to the group. The new value is placed on top of the rendering stack.
* <br>If the given object is already in the group, it is moved to the top of the rendering stack as well.
* @param Object The new object.
* @return The value at index.
* @exception IllegalArgumentException If given object is <code>null</code>.
*/
public void add(VectorObject object) throws IllegalArgumentException {
if (object == null) {
throw new IllegalArgumentException("The given vector object is null.");
}
if (objectList.contains(object)) {
objectList.remove(object);
}
objectList.add(object);
}
/** Remove object at index.
* @param index The value index.
* @exception IndexOutOfBoundsException If the given index is out of bounds.
*/
public void remove(int index) throws IndexOutOfBoundsException {
objectList.remove(index);
}
/** Gets an object iterator.
* @return A <code>Vector Object</code> iterator.
*/
Iterator<VectorObject> iterator() {
return objectList.iterator();
}
/** Gets the bounding box of this object in double precision.
* @return A <code>Rectange2D</code> or <code>null</code> if this group is empty.
*/
public Rectangle2D getBounds2D() {
Rectangle2D result = null;
// Compute the union of all objects' bounding boxes.
for (VectorObject object : objectList) {
Rectangle2D objectBounds = object.getBounds2D();
if (result == null) {
result = objectBounds;
}
else {
result = result.createUnion(objectBounds);
}
}
return result;
}
/** Draw this group.
* @param graphics The graphics context in which to paint.
*/
public void draw(Graphics2D graphics) {
// Render objects from first to last.
for (VectorObject object : objectList) {
object.draw(graphics);
}
}
} |
Partager