001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.graph.util.delaunay;
017:
018: import com.vividsolutions.jts.geom.Coordinate;
019: import java.awt.Color;
020: import java.awt.Graphics;
021: import java.lang.RuntimeException;
022: import java.util.Collection;
023: import java.util.Iterator;
024: import java.util.Vector;
025: import javax.swing.JPanel;
026: import org.geotools.graph.structure.Edge;
027: import org.geotools.graph.structure.Graph;
028: import org.geotools.graph.structure.line.XYNode;
029:
030: /**
031: *
032: * @author jfc173
033: */
034: public class GraphViewer extends JPanel {
035:
036: Graph graph;
037: Collection nodes;
038: double minX, minY;
039: int xScaling = 4;
040: int yScaling = 4;
041: int xOffset = 0;
042: int yOffset = 0;
043: boolean colorEdges = false;
044: Color[] nodeColors = new Color[] { Color.RED, Color.ORANGE,
045: Color.YELLOW, Color.GREEN, Color.BLUE, Color.MAGENTA,
046: Color.LIGHT_GRAY, Color.GRAY, Color.DARK_GRAY, Color.BLACK };
047: Vector shortEdges, longEdges, otherEdges;
048:
049: /** Creates a new instance of GraphViewer */
050: public GraphViewer() {
051: }
052:
053: public void setXScaling(int i) {
054: xScaling = i;
055: }
056:
057: public int getXScaling() {
058: return xScaling;
059: }
060:
061: public void setYScaling(int i) {
062: yScaling = i;
063: }
064:
065: public int getYScaling() {
066: return yScaling;
067: }
068:
069: public void setGraph(Graph gr) {
070: graph = gr;
071: nodes = graph.getNodes();
072: Iterator it = nodes.iterator();
073: minX = Double.MAX_VALUE;
074: minY = Double.MAX_VALUE;
075: while (it.hasNext()) {
076: Object next = it.next();
077: if (!(next instanceof XYNode)) {
078: throw new RuntimeException(
079: "I can't draw a node that doesn't have a coordinate.");
080: }
081: Coordinate coord = ((XYNode) next).getCoordinate();
082: if (coord.x < minX) {
083: minX = coord.x;
084: }
085: if (coord.y < minY) {
086: minY = coord.y;
087: }
088: }
089: }
090:
091: public void setColorEdges(boolean b) {
092: colorEdges = b;
093: }
094:
095: public void setShortEdges(Vector l) {
096: shortEdges = l;
097: }
098:
099: public void setLongEdges(Vector l) {
100: longEdges = l;
101: }
102:
103: public void setOtherEdges(Vector l) {
104: otherEdges = l;
105: }
106:
107: public void paintComponent(Graphics g) {
108: int i = 0;
109: xOffset = (int) Math.round(xScaling - minX * xScaling);
110: yOffset = (int) Math.round(yScaling - minY * yScaling);
111: System.out.println("xOffset is " + xOffset);
112: System.out.println("yOffset is " + yOffset);
113: Iterator it = nodes.iterator();
114: while (it.hasNext()) {
115: Object next = it.next();
116: if (!(next instanceof XYNode)) {
117: throw new RuntimeException(
118: "I can't draw a node that doesn't have a coordinate.");
119: }
120: Coordinate coord = ((XYNode) next).getCoordinate();
121: // g.setColor(nodeColors[i]);
122: // i++; //this works if there are no more than 10 nodes.
123: g.fillOval((int) Math.round(xOffset + coord.x * xScaling
124: - 2), (int) Math.round(yOffset + coord.y * yScaling
125: - 2), 4, 4);
126: }
127:
128: g.setColor(Color.RED);
129: Collection edges = graph.getEdges();
130: Iterator edgeIt = edges.iterator();
131: while (edgeIt.hasNext()) {
132: Edge next = (Edge) edgeIt.next();
133: if (!((next.getNodeA() instanceof XYNode) && (next
134: .getNodeB() instanceof XYNode))) {
135: throw new RuntimeException(
136: "I can't draw an edge without endpoint coordinates.");
137: }
138: Coordinate coordA = ((XYNode) next.getNodeA())
139: .getCoordinate();
140: Coordinate coordB = ((XYNode) next.getNodeB())
141: .getCoordinate();
142: if (colorEdges) {
143: if (shortEdges.contains(next)) {
144: g.setColor(Color.RED);
145: } else if (longEdges.contains(next)) {
146: g.setColor(Color.GREEN);
147: } else if (otherEdges.contains(next)) {
148: g.setColor(Color.BLACK);
149: } else {
150: g.setColor(Color.YELLOW);
151: }
152: }
153: g.drawLine((int) Math.round(xOffset + coordA.x * xScaling),
154: (int) Math.round(yOffset + coordA.y * yScaling),
155: (int) Math.round(xOffset + coordB.x * xScaling),
156: (int) Math.round(yOffset + coordB.y * yScaling));
157: }
158: }
159:
160: }
|