ShowGoogleMapsMapViz.java 4.54 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
27
28
 *
 * 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;
import java.awt.RenderingHints;
import java.awt.Toolkit;
29
30
import java.awt.image.BufferedImage;
import java.io.File;
31
32
33
34
35
36
37
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

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

40
import de.tud.kom.p2psim.impl.topology.movement.modularosm.GPSCalculation;
41
42
43
44
45
46
47
48
49
import de.tud.kom.p2psim.impl.topology.views.VisualizationTopologyView.VisualizationInjector;

public class ShowGoogleMapsMapViz extends JComponent implements IMapVisualization{
	
	private String tempImageFilePath;
	
	private boolean initialized = false;
	
	public ShowGoogleMapsMapViz() {
50
51
		setBounds(0, 0, VisualizationInjector.getWorldX(),
				VisualizationInjector.getWorldY());
52
53
54
55
56
57
		setOpaque(true);
		setVisible(true);	
	}
	
	private void initializeImage() {
		if(!initialized) {
58
59
60
61
62
			tempImageFilePath = tempImageFilePath + 
					"googlemaps" + 
					GPSCalculation.getLatCenter() + 
					GPSCalculation.getLonCenter() + 
					GPSCalculation.getZoom() + ".jpg";
63

64
65
			//Check if the file with same properties (same location) already exists
			File f = new File(tempImageFilePath);
66
			if(!f.exists()) {
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
				try {
		            String imageUrl = "http://maps.google.com/maps/api/staticmap?center=" + 
		            		GPSCalculation.getLatCenter() + "," + 
		            		GPSCalculation.getLonCenter() + "&zoom=" + 
		            		GPSCalculation.getZoom() + "&size=500x500&scale=2";
		            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();
		        }
			}
89
90
91
92
93
94
95
96
97
98
99
100
101
			initialized = true;
		}
	}

	@Override
	public void paint(Graphics g) {
		initializeImage();
		
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f));
102
103
104
105
		Image imageToDraw = resize(
				Toolkit.getDefaultToolkit().getImage(tempImageFilePath),
				VisualizationInjector.getWorldX(),
				VisualizationInjector.getWorldY());
106
		g2.drawImage(imageToDraw , 0, 0, this);		
107
108
109
110
111
112
	}
	
	public void setTempImageFilePath(String tempImageFilePath) {
		this.tempImageFilePath = tempImageFilePath;
	}
	
113
114
115
116
117
118
119
120
121
122
123
	/**
	 * Resizes the given image to the given width and height
	 *
	 * @param originalImage
	 * @param width
	 * @param height
	 */
	private Image resize(Image originalImage, int width, int height) {
	    int type = BufferedImage.TYPE_INT_ARGB;
	    BufferedImage resizedImage = new BufferedImage(width, height, type);
	    Graphics2D g = resizedImage.createGraphics();
124

125
126
127
128
	    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);
129

130
131
132
	    g.drawImage(originalImage, 0, 0, width, height, this);
	    g.dispose();
	    return resizedImage;
133
	}
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153

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

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

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

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