ShowMapQuestMapViz.java 5.8 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
41
42
43

import de.tud.kom.p2psim.impl.topology.movement.modularosm.GPSCalculation;
import de.tud.kom.p2psim.impl.topology.views.VisualizationTopologyView.VisualizationInjector;

44
45
46
public class ShowMapQuestMapViz extends JComponent
		implements IMapVisualization {

47
	private String tempImageFilePath;
48

49
	private String mapType;
50

51
	private String mapQuestKey;
52
53
	
	private BufferedImage background = null;
54

55
56
	private BufferedImage storedImage;

57
	private boolean initialized = false;
58

59

60
	public ShowMapQuestMapViz() {
61
62
		setBounds(0, 0, VisualizationInjector.getWorldX(),
				VisualizationInjector.getWorldY());
63
		setOpaque(true);
64
		setVisible(true);
65
	}
66

67
	private void initializeImage() {
68
69
70
71
72
73
74
75
76
		if (!initialized) {
			tempImageFilePath = tempImageFilePath + "mapquest"
					+ GPSCalculation.getLatCenter()
					+ GPSCalculation.getLonCenter() + GPSCalculation.getZoom()
					+ VisualizationInjector.getWorldX()
					+ VisualizationInjector.getWorldY() + mapType + ".jpg";

			// Check if the file with same properties (same location) already
			// exists
77
			File f = new File(tempImageFilePath);
78
			if (!f.exists()) {
79
				try {
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
					String imageUrl = "http://www.mapquestapi.com/staticmap/v4/getmap?key="
							+ mapQuestKey + "&type=" + mapType
							+ "&imagetype=jpeg&center="
							+ GPSCalculation.getLatCenter() + ","
							+ GPSCalculation.getLonCenter() + "&zoom="
							+ ((GPSCalculation.getZoom()) + 1) + "&size="
							+ VisualizationInjector.getWorldX() + ","
							+ VisualizationInjector.getWorldY();
					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();
				}
104
105
			}
			initialized = true;
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
			
			// 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());
	        
124
125
126
127
128
129
		}
	}

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

131
132
133
134
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
135
		g2.setComposite(
136
				AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.6f));
137
138
139
		if (background != null) {
			g2.drawImage(background, 0, 0, this);
		}
140
	}
141

142
143
144
	public void setTempImageFilePath(String tempImageFilePath) {
		this.tempImageFilePath = tempImageFilePath;
	}
145

146
147
148
	public void setMapType(String mapType) {
		this.mapType = mapType;
	}
149

150
151
152
	public void setMapQuestKey(String mapQuestKey) {
		this.mapQuestKey = mapQuestKey;
	}
153

154
155
156
157
158
159
160
	/**
	 * Resizes the given image to the given width and height
	 *
	 * @param originalImage
	 * @param width
	 * @param height
	 */
161
	private BufferedImage resize(Image originalImage, int width, int height) {
162
163
164
165
166
167
168
169
170
171
172
173
174
		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);
175

176
		g.dispose();
177
178

		storedImage = resizedImage;
179
		return resizedImage;
180
	}
181
182
183
184
185
186
187
188
189
190
191
192
193

	@Override
	public BufferedImage getMapImage()
	{
		if(storedImage == null)
		{
			initializeImage();
			resize(Toolkit.getDefaultToolkit().getImage(tempImageFilePath),
					VisualizationInjector.getWorldX(),
					VisualizationInjector.getWorldY());
		}
		return storedImage;
	}
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213

	@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;
	}
214
}