ShowMapQuestMapViz.java 6.35 KB
Newer Older
1
/*
2
 * Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 *
 * 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.movement.modularosm.mapvisualization;

import java.awt.AlphaComposite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
27
import java.awt.MediaTracker;
28
29
30
31
32
33
34
35
36
37
38
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

import javax.swing.JComponent;
39
import javax.swing.JMenu;
40

Clemens Krug's avatar
Clemens Krug committed
41
import de.tud.kom.p2psim.api.topology.Topology;
42
import de.tud.kom.p2psim.impl.topology.movement.modularosm.GPSCalculation;
43
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
44
import de.tud.kom.p2psim.impl.topology.views.VisualizationTopologyView.VisualizationInjector;
Clemens Krug's avatar
Clemens Krug committed
45
46
import de.tudarmstadt.maki.simonstrator.api.Binder;
import de.tudarmstadt.maki.simonstrator.api.component.ComponentNotAvailableException;
47

48
49
50
public class ShowMapQuestMapViz extends JComponent
		implements IMapVisualization {

51
	private String tempImageFilePath;
52

53
	private String mapType;
54

55
	private String mapQuestKey;
56
57
	
	private BufferedImage background = null;
58

59
60
	private BufferedImage storedImage;

61
	private boolean initialized = false;
62

63

64
	public ShowMapQuestMapViz() {
65
66
		setBounds(0, 0, VisualizationInjector.getWorldX(),
				VisualizationInjector.getWorldY());
67
		setOpaque(true);
68
		setVisible(true);
69
	}
70

71
	private void initializeImage() {
72
		if (!initialized) {
73
74
75
			PositionVector worldDimensions = Binder.getComponentOrNull(Topology.class)
					.getWorldDimensions();

76
			tempImageFilePath = tempImageFilePath + "mapquest"
Clemens Krug's avatar
Clemens Krug committed
77
78
					+ GPSCalculation.getLatCenter() + "_"
					+ GPSCalculation.getLonCenter() + "_" + GPSCalculation.getZoom() + "_"
79
80
					+ worldDimensions.getX() + "_"
					+ worldDimensions.getY() + mapType + ".jpg";
81
82
83

			// Check if the file with same properties (same location) already
			// exists
84
			File f = new File(tempImageFilePath);
85
			if (!f.exists()) {
86
				try {
87
88
89
90
					//Based on the meters per pixel, the needed height and width in pixels can be determined.
					int pxx = (int) (worldDimensions.getX() / GPSCalculation.getMetersPerPixel());
					int pxy = (int) (worldDimensions.getY() / GPSCalculation.getMetersPerPixel());

91
92
93
94
95
					String imageUrl = "http://www.mapquestapi.com/staticmap/v4/getmap?key="
							+ mapQuestKey + "&type=" + mapType
							+ "&imagetype=jpeg&center="
							+ GPSCalculation.getLatCenter() + ","
							+ GPSCalculation.getLonCenter() + "&zoom="
96
97
98
99
							+ ((GPSCalculation.getZoom())) + "&size="
							+ pxx + ","
							+ pxy;

100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
					URL url = new URL(imageUrl);
					InputStream is = url.openStream();
					OutputStream os = new FileOutputStream(tempImageFilePath);

					byte[] b = new byte[2048];
					int length;

					while ((length = is.read(b)) != -1) {
						os.write(b, 0, length);
					}

					is.close();
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
116
117
			}
			initialized = true;
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
			
			// Create a media tracker and add the image to it.  If we had several 
	        // images to load, they could all be added to the same media tracker. 
	        MediaTracker tracker = new MediaTracker(this); 
	        Image image = Toolkit.getDefaultToolkit().getImage(tempImageFilePath);
	        tracker.addImage(image, 0);

	        // Start downloading the image and wait until it finishes loading. 
	        try { 
	            tracker.waitForAll(); 
	        } catch(InterruptedException e) {
	        	// help?
	        }
	        background = resize(
					Toolkit.getDefaultToolkit().getImage(tempImageFilePath),
					VisualizationInjector.getWorldX(),
					VisualizationInjector.getWorldY());
	        
136
137
138
139
140
141
		}
	}

	@Override
	public void paint(Graphics g) {
		initializeImage();
142

143
144
145
146
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
147
		g2.setComposite(
148
				AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.6f));
149
150
151
		if (background != null) {
			g2.drawImage(background, 0, 0, this);
		}
152
	}
153

154
155
156
	public void setTempImageFilePath(String tempImageFilePath) {
		this.tempImageFilePath = tempImageFilePath;
	}
157

158
159
160
	public void setMapType(String mapType) {
		this.mapType = mapType;
	}
161

162
163
164
	public void setMapQuestKey(String mapQuestKey) {
		this.mapQuestKey = mapQuestKey;
	}
165

166
167
168
169
170
171
172
	/**
	 * Resizes the given image to the given width and height
	 *
	 * @param originalImage
	 * @param width
	 * @param height
	 */
173
	private BufferedImage resize(Image originalImage, int width, int height) {
174
175
176
177
178
179
180
181
182
183
184
185
186
		int type = BufferedImage.TYPE_INT_ARGB;
		BufferedImage resizedImage = new BufferedImage(width, height, type);
		Graphics2D g = resizedImage.createGraphics();

		g.setComposite(AlphaComposite.Src);
		g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
				RenderingHints.VALUE_INTERPOLATION_BILINEAR);
		g.setRenderingHint(RenderingHints.KEY_RENDERING,
				RenderingHints.VALUE_RENDER_QUALITY);
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);

		g.drawImage(originalImage, 0, 0, width, height, this);
187

188
		g.dispose();
189
190

		storedImage = resizedImage;
191
		return resizedImage;
192
	}
193
194
195
196
197
198
199
200
201
202
203
204
205

	@Override
	public BufferedImage getMapImage()
	{
		if(storedImage == null)
		{
			initializeImage();
			resize(Toolkit.getDefaultToolkit().getImage(tempImageFilePath),
					VisualizationInjector.getWorldX(),
					VisualizationInjector.getWorldY());
		}
		return storedImage;
	}
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225

	@Override
	public String getDisplayName() {
		return "MapQuest Map";
	}

	@Override
	public JComponent getComponent() {
		return this;
	}

	@Override
	public JMenu getCustomMenu() {
		return null;
	}

	@Override
	public boolean isHidden() {
		return false;
	}
226
}