KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcckit > graphic > Polygon


1 /*
2  * Copyright 2003-2004, Franz-Josef Elmer, All rights reserved
3  *
4  * This library is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation; either version 2.1 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details
13  * (http://www.gnu.org/copyleft/lesser.html).
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package jcckit.graphic;
20
21 import java.util.Vector JavaDoc;
22
23 /**
24  * A polygon or polyline.
25  *
26  * @author Franz-Josef Elmer
27  */

28 public class Polygon extends BasicGraphicalElement {
29   private final Vector JavaDoc _points = new Vector JavaDoc();
30   private final boolean _closed;
31
32   /**
33    * Creates an instance of the specified graphic attributes.
34    * @param closed <tt>true</tt> if this polygon is closed.
35    */

36   public Polygon(GraphicAttributes attributes, boolean closed) {
37     super(attributes);
38     _closed = closed;
39   }
40
41   /** Returns <tt>true</tt> if this polygon is closed. */
42   public boolean isClosed() {
43     return _closed;
44   }
45
46   /** Returns the number points. */
47   public int getNumberOfPoints() {
48     return _points.size();
49   }
50
51   /** Returns the point for the specified index. */
52   public GraphPoint getPoint(int index) {
53     return (GraphPoint) _points.elementAt(index);
54   }
55
56   /** Adds a new point to the end of the list of points. */
57   public void addPoint(GraphPoint point) {
58     _points.addElement(point);
59   }
60
61   /** Removes all points. */
62   public void removeAllPoints() {
63     _points.removeAllElements();
64   }
65
66   /** Replaces the point at the specified index by a new one. */
67   public void replacePointAt(int index, GraphPoint point) {
68     _points.setElementAt(point, index);
69   }
70
71   /**
72    * Renders this line with the specified {@link Renderer}.
73    * @param renderer An instance of {@link PolygonRenderer}.
74    * @throws IllegalArgumentException if <tt>renderer</tt> is not
75    * an instance of <tt>PolygonRenderer</tt>.
76    */

77   public void renderWith(Renderer renderer) {
78     if (renderer instanceof PolygonRenderer) {
79       ((PolygonRenderer) renderer).render(this);
80     } else {
81       throw new IllegalArgumentException JavaDoc(renderer
82                             + " does not implements PolygonRenderer.");
83     }
84   }
85 }
86
Popular Tags