WeakWaypointComponentVis.java 7 KB
Newer Older
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
/*
 * Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
 *
 * This file is part of PeerfactSim.KOM.
 * 
 * PeerfactSim.KOM is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 * 
 * PeerfactSim.KOM is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with PeerfactSim.KOM.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package de.tud.kom.p2psim.impl.topology.views.visualization.world;

import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Random;
import java.util.Set;

import javax.swing.JComponent;

import de.tud.kom.p2psim.api.topology.waypoints.WaypointModel;
import de.tud.kom.p2psim.api.topology.waypoints.WaypointModelListener;
40
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
41
import de.tud.kom.p2psim.impl.topology.views.VisualizationTopologyView.VisualizationInjector;
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
import de.tud.kom.p2psim.impl.topology.waypoints.graph.DefaultWeightedEdgeRetrievableGraph;
import de.tud.kom.p2psim.impl.topology.waypoints.graph.Path;
import de.tud.kom.p2psim.impl.topology.waypoints.graph.Waypoint;
import de.tud.kom.p2psim.impl.topology.waypoints.graph.WeakWaypoint;
import de.tudarmstadt.maki.simonstrator.api.Randoms;

/**
 * 
 * @author Fabio Zöllner
 * @version 1.0, 27.03.2012
 */
public class WeakWaypointComponentVis extends JComponent implements WaypointModelListener {

	protected final int WAYPOINT_STRONG_RADIUS = 2;
	protected final int WAYPOINT_WEAK_RADIUS = 1;
	
	protected Color COLOR_PATH;
	
	protected Color COLOR_STRONG_WAYPOINT;
	protected Color COLOR_WEAK_WAYPOINT;
	
	protected Random rnd = Randoms
			.getRandom(WeakWaypointComponentVis.class);
	
	protected BufferedImage image;
	
	protected volatile boolean needsRedraw = true;

	private WaypointModel model;
	
72
	public WeakWaypointComponentVis(WaypointModel model) {
73
		this.model = model;
74
75
		setBounds(0, 0, VisualizationInjector.getWorldX(),
				VisualizationInjector.getWorldY());
76
77
78
79
80
81
		setOpaque(false);
		setVisible(true);
		COLOR_STRONG_WAYPOINT = new Color(30, 144, 255, 255);
		COLOR_WEAK_WAYPOINT = new Color(30, 144, 255, 200);
		COLOR_PATH = new Color(49, 79, 79, 130);
		
82
83
84
		image = new BufferedImage(VisualizationInjector.getWorldX(),
				VisualizationInjector.getWorldY(),
				BufferedImage.TYPE_INT_ARGB);
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
	}

	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		if (needsRedraw) 
			redraw();
		
		Graphics2D g2 = (Graphics2D) g;

		g2.drawImage(image, 0, 0, null);
	}
	
	protected void redraw() {
		needsRedraw = false;
		
		Graphics2D g2 = (Graphics2D) image.getGraphics();

		Composite c = g2.getComposite();
		g2.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
106
107
108
		Rectangle2D.Double rect = new Rectangle2D.Double(0, 0,
				VisualizationInjector.getWorldX(),
				VisualizationInjector.getWorldY());
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
		g2.fill(rect);
		g2.setComposite(c);
		
		// TODO Change to non-colored version
		try {
			//drawColoredGraph(g2);
			drawPaths(g2);
		} catch (RuntimeException e) {
			needsRedraw = true;
		}
		
		try {
			drawWaypoints(g2);
		} catch (RuntimeException e) {
			needsRedraw = true;
		}
	}
	
	protected void drawColoredGraph(Graphics2D g2) {
		ArrayList<Path> usedWaypoints = new ArrayList<Path>();
		ArrayList<Waypoint> starts = new ArrayList<Waypoint>();
		Collection<Waypoint> waypoints = model.getWaypoints();
		
		DefaultWeightedEdgeRetrievableGraph<Waypoint, Path> graph = model.getGraph();

		if (waypoints == null || waypoints.isEmpty() || graph == null)  {
			throw new RuntimeException() {
				@Override public synchronized Throwable fillInStackTrace() { return null; }
			};
		}
		
		for (Waypoint wp : waypoints) {
			if (starts.contains(wp)) continue;
			starts.add(wp);
			
			ArrayList<Path> workList = new ArrayList<Path>();
			workList.addAll(graph.edgesOf(wp));
			
			Color pathColor = getRandomColor();
			
			while (!workList.isEmpty()) {
				Path p = workList.remove(0);
				
				Waypoint target = p.getTarget();
				starts.add(target);
				
				if (containsPath(usedWaypoints, p))
					continue;
				
				workList.addAll(graph.edgesOf(target));
				
				PositionVector sourcePos = p.getSource().getPosition();
				PositionVector targetPos = p.getTarget().getPosition();
				
				g2.setColor(pathColor);
				g2.setStroke(new BasicStroke(0.8f));
				
166
167
168
169
170
				g2.drawLine(VisualizationInjector.scaleValue(sourcePos.getX()),
						VisualizationInjector.scaleValue(sourcePos.getY()),
						VisualizationInjector.scaleValue(targetPos.getX()),
						VisualizationInjector.scaleValue(targetPos.getY()));

171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
				usedWaypoints.add(p);
			}
		}
	}

	private boolean containsPath(ArrayList<Path> workList, Path p1) {
		for (Path p : workList) {
			if (p.equals(p1)) {
				return true;
			}
		}
		return false;
	}
	
	private Color getRandomColor() {
		return new Color(rnd.nextFloat(), rnd.nextFloat(), rnd.nextFloat());
	}
	
	protected void drawPaths(Graphics2D g2) {
		Set<Path> paths = model.getPaths();
		
		if (paths.isEmpty()) {
			throw new RuntimeException() {
				@Override public synchronized Throwable fillInStackTrace() { return null; }
			};
		}
		
		for (Path p : paths) {
			PositionVector sourcePos = p.getSource().getPosition();
			PositionVector targetPos = p.getTarget().getPosition();

			g2.setColor(COLOR_PATH);
			g2.setStroke(new BasicStroke(0.8f));
204
205
206
207
			g2.drawLine(VisualizationInjector.scaleValue(sourcePos.getX()),
					VisualizationInjector.scaleValue(sourcePos.getY()),
					VisualizationInjector.scaleValue(targetPos.getX()),
					VisualizationInjector.scaleValue(targetPos.getY()));
208
209
210
211
212
213
214
215
216
217
218
		}
	}
	
	protected void drawWaypoints(Graphics2D g2) {
		Collection<Waypoint> waypoints = model.getWaypoints();
		
		for (Waypoint wp : waypoints) {
			PositionVector pos = wp.getPosition();
			
			if (wp instanceof WeakWaypoint) {
				g2.setColor(COLOR_WEAK_WAYPOINT);
219
220
221
222
223
224
				g2.fillOval(
						VisualizationInjector.scaleValue(pos.getX())
								- WAYPOINT_WEAK_RADIUS,
						VisualizationInjector.scaleValue(pos.getY())
								- WAYPOINT_WEAK_RADIUS,
						WAYPOINT_WEAK_RADIUS * 2, WAYPOINT_WEAK_RADIUS * 2);
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
			}
		}
	}

	@Override
	public void addedPath(Path path) {
		needsRedraw = true;
	}

	@Override
	public void addedWaypoint(Waypoint waypoint) {
		needsRedraw = true;
	}
	
	@Override
	public void modifiedWaypoints() {
		needsRedraw = true;
	}

}