StrongWaypointComponentVis.java 4.35 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
/*
 * 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.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.Collection;
import java.util.Random;

import javax.swing.JComponent;

import de.tud.kom.p2psim.api.topology.waypoints.WaypointModel;
import de.tud.kom.p2psim.api.topology.waypoints.WaypointModelListener;
37
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
38
import de.tud.kom.p2psim.impl.topology.views.VisualizationTopologyView.VisualizationInjector;
39
40
41
42
43
import de.tud.kom.p2psim.impl.topology.waypoints.graph.Path;
import de.tud.kom.p2psim.impl.topology.waypoints.graph.StrongWaypoint;
import de.tud.kom.p2psim.impl.topology.waypoints.graph.Waypoint;
import de.tudarmstadt.maki.simonstrator.api.Randoms;

44
45
public class StrongWaypointComponentVis extends JComponent
		implements WaypointModelListener {
46
47
48
49
50

	/**
	 * 
	 */
	protected final int WAYPOINT_STRONG_RADIUS = 2;
51

52
	protected final int WAYPOINT_WEAK_RADIUS = 1;
53

54
	protected Color COLOR_PATH;
55

56
	protected Color COLOR_STRONG_WAYPOINT;
57

58
	protected Color COLOR_WEAK_WAYPOINT;
59
60
61

	protected Random rnd = Randoms.getRandom(StrongWaypointComponentVis.class);

62
	protected BufferedImage image;
63

64
65
66
67
	protected volatile boolean needsRedraw = true;

	private WaypointModel model;

68
	public StrongWaypointComponentVis(WaypointModel model) {
69
		this.model = model;
70
71
		setBounds(0, 0, VisualizationInjector.getWorldX(),
				VisualizationInjector.getWorldY());
72
73
74
75
76
		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);
77
78
79

		image = new BufferedImage(VisualizationInjector.getWorldX(),
				VisualizationInjector.getWorldY(), BufferedImage.TYPE_INT_ARGB);
80
81
82
83
84
	}

	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
85
86

		if (needsRedraw)
87
			redraw();
88

89
90
91
92
		Graphics2D g2 = (Graphics2D) g;

		g2.drawImage(image, 0, 0, null);
	}
93

94
95
96
97
98
99
100
	protected void redraw() {
		needsRedraw = false;

		Graphics2D g2 = (Graphics2D) image.getGraphics();

		Composite c = g2.getComposite();
		g2.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
101
102
103
		Rectangle2D.Double rect = new Rectangle2D.Double(0, 0,
				VisualizationInjector.getWorldX(),
				VisualizationInjector.getWorldY());
104
105
		g2.fill(rect);
		g2.setComposite(c);
106

107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
		try {
			drawWaypoints(g2);
		} catch (RuntimeException e) {
			needsRedraw = true;
		}
	}

	protected void drawWaypoints(Graphics2D g2) {
		Collection<Waypoint> waypoints = model.getWaypoints(StrongWaypoint.class);

		if (waypoints.isEmpty())  {
			throw new RuntimeException() {
				@Override public synchronized Throwable fillInStackTrace() { return null; }
			};
		}
		
		for (Waypoint wp : waypoints) {
			PositionVector pos = wp.getPosition();
			
			if (wp instanceof StrongWaypoint) {
				g2.setColor(COLOR_STRONG_WAYPOINT);
128
129
130
131
132
133
134
				g2.fillOval(
						VisualizationInjector.scaleValue(pos.getX())
								- WAYPOINT_STRONG_RADIUS,
						VisualizationInjector.scaleValue(pos.getY())
								- WAYPOINT_STRONG_RADIUS,
						(WAYPOINT_STRONG_RADIUS * 2),
						(WAYPOINT_STRONG_RADIUS * 2));
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
			}
		}
	}

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

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

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