Fix issue with MapViewDrawing and generalize ClickListener for all Drawings.
This commit is contained in:
		
							parent
							
								
									24eeab7971
								
							
						
					
					
						commit
						9db2ee36dd
					
				
					 11 changed files with 1592 additions and 1689 deletions
				
			
		
							
								
								
									
										132
									
								
								.classpath
									
									
									
									
									
								
							
							
						
						
									
										132
									
								
								.classpath
									
									
									
									
									
								
							|  | @ -1,69 +1,63 @@ | ||||||
| <?xml version="1.0" encoding="UTF-8"?> | <?xml version="1.0" encoding="UTF-8"?> | ||||||
| <classpath> | <classpath> | ||||||
| 	<classpathentry kind="src" output="target/classes" path="src/main"> | 	<classpathentry kind="src" output="target/classes" path="src/main"> | ||||||
| 		<attributes> | 		<attributes> | ||||||
| 			<attribute name="optional" value="true"/> | 			<attribute name="optional" value="true"/> | ||||||
| 			<attribute name="maven.pomderived" value="true"/> | 			<attribute name="maven.pomderived" value="true"/> | ||||||
| 		</attributes> | 		</attributes> | ||||||
| 	</classpathentry> | 	</classpathentry> | ||||||
| 	<classpathentry kind="src" output="target/test-classes" path="src/test"> | 	<classpathentry kind="src" output="target/test-classes" path="src/test"> | ||||||
| 		<attributes> | 		<attributes> | ||||||
| 			<attribute name="optional" value="true"/> | 			<attribute name="optional" value="true"/> | ||||||
| 			<attribute name="maven.pomderived" value="true"/> | 			<attribute name="maven.pomderived" value="true"/> | ||||||
| 		</attributes> | 		</attributes> | ||||||
| 	</classpathentry> | 	</classpathentry> | ||||||
| 	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"> | 	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"> | ||||||
| 		<attributes> | 		<attributes> | ||||||
| 			<attribute name="maven.pomderived" value="true"/> | 			<attribute name="maven.pomderived" value="true"/> | ||||||
| 		</attributes> | 		</attributes> | ||||||
| 	</classpathentry> | 	</classpathentry> | ||||||
| 	<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/> | 	<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/> | ||||||
| 	<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> | 	<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> | ||||||
| 		<attributes> | 		<attributes> | ||||||
| 			<attribute name="maven.pomderived" value="true"/> | 			<attribute name="maven.pomderived" value="true"/> | ||||||
| 		</attributes> | 		</attributes> | ||||||
| 	</classpathentry> | 	</classpathentry> | ||||||
| 	<classpathentry kind="lib" path="libs/mapsforge-core-0.9.1.jar"> | 	<classpathentry kind="lib" path="libs/mapsforge-core-0.9.1.jar"> | ||||||
| 		<attributes> | 		<attributes> | ||||||
| 			<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-base/libs/mapsforge-core-0.9.1-javadoc.jar!/"/> | 			<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-base/libs/mapsforge-core-0.9.1-javadoc.jar!/"/> | ||||||
| 		</attributes> | 		</attributes> | ||||||
| 	</classpathentry> | 	</classpathentry> | ||||||
| 	<classpathentry kind="lib" path="libs/mapsforge-map-0.9.1.jar"> | 	<classpathentry kind="lib" path="libs/mapsforge-map-0.9.1.jar"> | ||||||
| 		<attributes> | 		<attributes> | ||||||
| 			<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-base/libs/mapsforge-map-0.9.1-javadoc.jar!/"/> | 			<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-base/libs/mapsforge-map-0.9.1-javadoc.jar!/"/> | ||||||
| 		</attributes> | 		</attributes> | ||||||
| 	</classpathentry> | 	</classpathentry> | ||||||
| 	<classpathentry kind="lib" path="libs/mapsforge-map-awt-0.9.1.jar"> | 	<classpathentry kind="lib" path="libs/mapsforge-map-awt-0.9.1.jar"> | ||||||
| 		<attributes> | 		<attributes> | ||||||
| 			<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-base/libs/mapsforge-map-awt-0.9.1-javadoc.jar!/"/> | 			<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-base/libs/mapsforge-map-awt-0.9.1-javadoc.jar!/"/> | ||||||
| 		</attributes> | 		</attributes> | ||||||
| 	</classpathentry> | 	</classpathentry> | ||||||
| 	<classpathentry kind="lib" path="libs/mapsforge-map-reader-0.9.1.jar"> | 	<classpathentry kind="lib" path="libs/mapsforge-map-reader-0.9.1.jar"> | ||||||
| 		<attributes> | 		<attributes> | ||||||
| 			<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-base/libs/mapsforge-map-reader-0.9.1-javadoc.jar!/"/> | 			<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-base/libs/mapsforge-map-reader-0.9.1-javadoc.jar!/"/> | ||||||
| 		</attributes> | 		</attributes> | ||||||
| 	</classpathentry> | 	</classpathentry> | ||||||
| 	<classpathentry kind="lib" path="libs/mapsforge-poi-0.9.1.jar"> | 	<classpathentry kind="lib" path="libs/mapsforge-poi-0.9.1.jar"> | ||||||
| 		<attributes> | 		<attributes> | ||||||
| 			<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-base/libs/mapsforge-poi-0.9.1-javadoc.jar!/"/> | 			<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-base/libs/mapsforge-poi-0.9.1-javadoc.jar!/"/> | ||||||
| 		</attributes> | 		</attributes> | ||||||
| 	</classpathentry> | 	</classpathentry> | ||||||
| 	<classpathentry kind="lib" path="libs/mapsforge-poi-awt-0.9.1.jar"> | 	<classpathentry kind="lib" path="libs/mapsforge-poi-awt-0.9.1.jar"> | ||||||
| 		<attributes> | 		<attributes> | ||||||
| 			<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-base/libs/mapsforge-poi-awt-0.9.1-javadoc.jar!/"/> | 			<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-base/libs/mapsforge-poi-awt-0.9.1-javadoc.jar!/"/> | ||||||
| 		</attributes> | 		</attributes> | ||||||
| 	</classpathentry> | 	</classpathentry> | ||||||
| 	<classpathentry kind="lib" path="libs/mapsforge-themes-0.9.1.jar"> | 	<classpathentry kind="lib" path="libs/kxml2-2.3.0.jar"/> | ||||||
| 		<attributes> | 	<classpathentry kind="lib" path="libs/svg-salamander-1.0.jar"> | ||||||
| 			<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-base/libs/mapsforge-themes-0.9.1-javadoc.jar!/"/> | 		<attributes> | ||||||
| 		</attributes> | 			<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-base/libs/svg-salamander-1.0-javadoc.jar!/"/> | ||||||
| 	</classpathentry> | 		</attributes> | ||||||
| 	<classpathentry kind="lib" path="libs/kxml2-2.3.0.jar"/> | 	</classpathentry> | ||||||
| 	<classpathentry kind="lib" path="libs/svg-salamander-1.0.jar"> | 	<classpathentry kind="output" path="target/classes"/> | ||||||
| 		<attributes> | </classpath> | ||||||
| 			<attribute name="javadoc_location" value="jar:platform:/resource/be-graphes-base/libs/svg-salamander-1.0-javadoc.jar!/"/> |  | ||||||
| 		</attributes> |  | ||||||
| 	</classpathentry> |  | ||||||
| 	<classpathentry kind="lib" path="libs/mapsforge-map-writer-0.9.1-jar-with-dependencies.jar"/> |  | ||||||
| 	<classpathentry kind="output" path="target/classes"/> |  | ||||||
| </classpath> |  | ||||||
|  |  | ||||||
							
								
								
									
										5
									
								
								.settings/org.eclipse.jdt.core.prefs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								.settings/org.eclipse.jdt.core.prefs
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,5 @@ | ||||||
|  | eclipse.preferences.version=1 | ||||||
|  | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 | ||||||
|  | org.eclipse.jdt.core.compiler.compliance=1.8 | ||||||
|  | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning | ||||||
|  | org.eclipse.jdt.core.compiler.source=1.8 | ||||||
							
								
								
									
										4
									
								
								.settings/org.eclipse.m2e.core.prefs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								.settings/org.eclipse.m2e.core.prefs
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,4 @@ | ||||||
|  | activeProfiles= | ||||||
|  | eclipse.preferences.version=1 | ||||||
|  | resolveWorkspaceProjects=true | ||||||
|  | version=1 | ||||||
|  | @ -7,33 +7,31 @@ import org.insa.drawing.Drawing; | ||||||
| import org.insa.graph.Node; | import org.insa.graph.Node; | ||||||
| 
 | 
 | ||||||
| public class WeaklyConnectedComponentGraphicObserver implements WeaklyConnectedComponentObserver { | public class WeaklyConnectedComponentGraphicObserver implements WeaklyConnectedComponentObserver { | ||||||
| 	 |  | ||||||
| 	private static final Color[] COLORS = { |  | ||||||
| 		Color.BLUE, Color.ORANGE, Color.GREEN, Color.YELLOW, Color.RED |  | ||||||
| 	}; |  | ||||||
| 
 | 
 | ||||||
| 	// Drawing + Graph drawing |     private static final Color[] COLORS = { Color.BLUE, Color.ORANGE, Color.GREEN, Color.YELLOW, Color.RED }; | ||||||
| 	private Drawing drawing; |  | ||||||
| 	 |  | ||||||
| 	// Current index color |  | ||||||
| 	private int cindex = -1; |  | ||||||
| 	 |  | ||||||
| 	public WeaklyConnectedComponentGraphicObserver(Drawing drawing) { |  | ||||||
| 		this.drawing = drawing; |  | ||||||
| 	} |  | ||||||
| 
 | 
 | ||||||
| 	@Override |     // Drawing + Graph drawing | ||||||
| 	public void notifyStartComponent(Node curNode) { |     private Drawing drawing; | ||||||
| 		cindex = (cindex + 1) % COLORS.length; |  | ||||||
| 	} |  | ||||||
| 
 | 
 | ||||||
| 	@Override |     // Current index color | ||||||
| 	public void notifyNewNodeInComponent(Node node) { |     private int cindex = -1; | ||||||
| 		this.drawing.drawMarker(node.getPoint(), COLORS[cindex]); |  | ||||||
| 	} |  | ||||||
| 
 | 
 | ||||||
| 	@Override |     public WeaklyConnectedComponentGraphicObserver(Drawing drawing) { | ||||||
| 	public void notifyEndComponent(ArrayList<Node> nodes) { |         this.drawing = drawing; | ||||||
| 	} |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void notifyStartComponent(Node curNode) { | ||||||
|  |         cindex = (cindex + 1) % COLORS.length; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void notifyNewNodeInComponent(Node node) { | ||||||
|  |         this.drawing.drawPoint(node.getPoint(), 1, COLORS[cindex]); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void notifyEndComponent(ArrayList<Node> nodes) { | ||||||
|  |     } | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  |  | ||||||
										
											
												File diff suppressed because it is too large
												Load diff
											
										
									
								
							|  | @ -1,302 +0,0 @@ | ||||||
| /* |  | ||||||
|  * Copyright 2010, 2011, 2012, 2013 mapsforge.org |  | ||||||
|  * Copyright 2014 Christian Pesch |  | ||||||
|  * Copyright 2014 Ludwig M Brinckmann |  | ||||||
|  * Copyright 2014-2018 devemux86 |  | ||||||
|  * Copyright 2017 usrusr |  | ||||||
|  * |  | ||||||
|  * This program is free software: you can redistribute it and/or modify it under the |  | ||||||
|  * terms of the GNU Lesser General Public License as published by the Free Software |  | ||||||
|  * Foundation, either version 3 of the License, or (at your option) any later version. |  | ||||||
|  * |  | ||||||
|  * This program 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 Lesser General Public License for more details. |  | ||||||
|  * |  | ||||||
|  * You should have received a copy of the GNU Lesser General Public License along with |  | ||||||
|  * this program. If not, see <http://www.gnu.org/licenses/>. |  | ||||||
|  */ |  | ||||||
| package org.insa.base; |  | ||||||
| 
 |  | ||||||
| import java.awt.Dimension; |  | ||||||
| import java.awt.event.WindowAdapter; |  | ||||||
| import java.awt.event.WindowEvent; |  | ||||||
| import java.io.File; |  | ||||||
| import java.util.ArrayList; |  | ||||||
| import java.util.Arrays; |  | ||||||
| import java.util.List; |  | ||||||
| import java.util.UUID; |  | ||||||
| import java.util.prefs.Preferences; |  | ||||||
| 
 |  | ||||||
| import javax.swing.JFrame; |  | ||||||
| import javax.swing.JOptionPane; |  | ||||||
| import javax.swing.WindowConstants; |  | ||||||
| 
 |  | ||||||
| import org.insa.graph.Arc; |  | ||||||
| import org.insa.graph.Graph; |  | ||||||
| import org.insa.graph.Path; |  | ||||||
| import org.insa.graph.io.BinaryGraphReader; |  | ||||||
| import org.insa.graph.io.BinaryPathReader; |  | ||||||
| import org.insa.graph.io.Openfile; |  | ||||||
| import org.mapsforge.core.graphics.Color; |  | ||||||
| import org.mapsforge.core.graphics.GraphicFactory; |  | ||||||
| import org.mapsforge.core.graphics.Paint; |  | ||||||
| import org.mapsforge.core.graphics.Style; |  | ||||||
| import org.mapsforge.core.model.BoundingBox; |  | ||||||
| import org.mapsforge.core.model.LatLong; |  | ||||||
| import org.mapsforge.core.model.MapPosition; |  | ||||||
| import org.mapsforge.core.model.Point; |  | ||||||
| import org.mapsforge.core.util.LatLongUtils; |  | ||||||
| import org.mapsforge.core.util.Parameters; |  | ||||||
| import org.mapsforge.map.awt.graphics.AwtGraphicFactory; |  | ||||||
| import org.mapsforge.map.awt.util.AwtUtil; |  | ||||||
| import org.mapsforge.map.awt.util.JavaPreferences; |  | ||||||
| import org.mapsforge.map.awt.view.MapView; |  | ||||||
| import org.mapsforge.map.datastore.MapDataStore; |  | ||||||
| import org.mapsforge.map.datastore.MultiMapDataStore; |  | ||||||
| import org.mapsforge.map.layer.Layers; |  | ||||||
| import org.mapsforge.map.layer.cache.TileCache; |  | ||||||
| import org.mapsforge.map.layer.debug.TileCoordinatesLayer; |  | ||||||
| import org.mapsforge.map.layer.debug.TileGridLayer; |  | ||||||
| import org.mapsforge.map.layer.download.TileDownloadLayer; |  | ||||||
| import org.mapsforge.map.layer.download.tilesource.OpenStreetMapMapnik; |  | ||||||
| import org.mapsforge.map.layer.download.tilesource.TileSource; |  | ||||||
| import org.mapsforge.map.layer.hills.DiffuseLightShadingAlgorithm; |  | ||||||
| import org.mapsforge.map.layer.hills.HillsRenderConfig; |  | ||||||
| import org.mapsforge.map.layer.hills.MemoryCachingHgtReaderTileSource; |  | ||||||
| import org.mapsforge.map.layer.overlay.Polyline; |  | ||||||
| import org.mapsforge.map.layer.renderer.TileRendererLayer; |  | ||||||
| import org.mapsforge.map.model.MapViewPosition; |  | ||||||
| import org.mapsforge.map.model.Model; |  | ||||||
| import org.mapsforge.map.model.common.PreferencesFacade; |  | ||||||
| import org.mapsforge.map.reader.MapFile; |  | ||||||
| import org.mapsforge.map.rendertheme.InternalRenderTheme; |  | ||||||
| 
 |  | ||||||
| public final class Samples { |  | ||||||
| 	private static final GraphicFactory GRAPHIC_FACTORY = AwtGraphicFactory.INSTANCE; |  | ||||||
| 	private static final boolean SHOW_DEBUG_LAYERS = false; |  | ||||||
| 	private static final boolean SHOW_RASTER_MAP = false; |  | ||||||
| 
 |  | ||||||
| 	private static final String MESSAGE = "Are you sure you want to exit the application?"; |  | ||||||
| 	private static final String TITLE = "Confirm close"; |  | ||||||
| 
 |  | ||||||
| 	/** |  | ||||||
| 	 * Starts the {@code Samples}. |  | ||||||
| 	 * |  | ||||||
| 	 * @param args |  | ||||||
| 	 *            command line args: expects the map files as multiple parameters |  | ||||||
| 	 *            with possible SRTM hgt folder as 1st argument. |  | ||||||
| 	 * @throws Exception |  | ||||||
| 	 */ |  | ||||||
| 	public static void main(String[] args) throws Exception { |  | ||||||
| 
 |  | ||||||
| 		// Multithreaded map rendering |  | ||||||
| 		Parameters.NUMBER_OF_THREADS = 2; |  | ||||||
| 
 |  | ||||||
| 		// Square frame buffer |  | ||||||
| 		Parameters.SQUARE_FRAME_BUFFER = false; |  | ||||||
| 
 |  | ||||||
| 		HillsRenderConfig hillsCfg = null; |  | ||||||
| 		File demFolder = getDemFolder(args); |  | ||||||
| 		if (demFolder != null) { |  | ||||||
| 			MemoryCachingHgtReaderTileSource tileSource = new MemoryCachingHgtReaderTileSource( |  | ||||||
| 					demFolder, new DiffuseLightShadingAlgorithm(), AwtGraphicFactory.INSTANCE); |  | ||||||
| 			tileSource.setEnableInterpolationOverlap(true); |  | ||||||
| 			hillsCfg = new HillsRenderConfig(tileSource); |  | ||||||
| 			hillsCfg.indexOnThread(); |  | ||||||
| 			args = Arrays.copyOfRange(args, 1, args.length); |  | ||||||
| 		} |  | ||||||
| 
 |  | ||||||
| 		List<File> mapFiles = getMapFiles(args); |  | ||||||
| 		final MapView mapView = createMapView(); |  | ||||||
| 		final BoundingBox boundingBox = addLayers(mapView, mapFiles, hillsCfg); |  | ||||||
| 
 |  | ||||||
| 		// addAPath(mapView); |  | ||||||
| 
 |  | ||||||
| 		final PreferencesFacade preferencesFacade = new JavaPreferences( |  | ||||||
| 				Preferences.userNodeForPackage(Samples.class)); |  | ||||||
| 
 |  | ||||||
| 		final JFrame frame = new JFrame(); |  | ||||||
| 		frame.setTitle("Mapsforge Samples"); |  | ||||||
| 		frame.add(mapView); |  | ||||||
| 		frame.pack(); |  | ||||||
| 		frame.setSize(new Dimension(800, 600)); |  | ||||||
| 		frame.setLocationRelativeTo(null); |  | ||||||
| 		frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); |  | ||||||
| 		frame.addWindowListener(new WindowAdapter() { |  | ||||||
| 			@Override |  | ||||||
| 			public void windowClosing(WindowEvent e) { |  | ||||||
| 				int result = JOptionPane.showConfirmDialog(frame, MESSAGE, TITLE, |  | ||||||
| 						JOptionPane.YES_NO_OPTION); |  | ||||||
| 				if (result == JOptionPane.YES_OPTION) { |  | ||||||
| 					mapView.getModel().save(preferencesFacade); |  | ||||||
| 					mapView.destroyAll(); |  | ||||||
| 					AwtGraphicFactory.clearResourceMemoryCache(); |  | ||||||
| 					frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); |  | ||||||
| 				} |  | ||||||
| 			} |  | ||||||
| 
 |  | ||||||
| 			@Override |  | ||||||
| 			public void windowOpened(WindowEvent e) { |  | ||||||
| 				final Model model = mapView.getModel(); |  | ||||||
| 				model.init(preferencesFacade); |  | ||||||
| 				if (model.mapViewPosition.getZoomLevel() == 0 |  | ||||||
| 						|| !boundingBox.contains(model.mapViewPosition.getCenter())) { |  | ||||||
| 					byte zoomLevel = LatLongUtils.zoomForBounds( |  | ||||||
| 							model.mapViewDimension.getDimension(), boundingBox, |  | ||||||
| 							model.displayModel.getTileSize()); |  | ||||||
| 					model.mapViewPosition.setMapPosition( |  | ||||||
| 							new MapPosition(boundingBox.getCenterPoint(), zoomLevel)); |  | ||||||
| 				} |  | ||||||
| 			} |  | ||||||
| 		}); |  | ||||||
| 		frame.setVisible(true); |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	private static void addAPath(MapView mapView) throws Exception { |  | ||||||
| 
 |  | ||||||
| 		Graph gr = (new BinaryGraphReader(Openfile.open("Maps/midip.map"))).read(); |  | ||||||
| 		Path path = (new BinaryPathReader(Openfile.open("Paths/chemin_0x400_119963_96676.path"))) |  | ||||||
| 				.readPath(gr); |  | ||||||
| 
 |  | ||||||
| 		Paint paintStroke = AwtGraphicFactory.INSTANCE.createPaint(); |  | ||||||
| 		paintStroke.setColor(Color.GREEN); |  | ||||||
| 		paintStroke.setStrokeWidth(3); |  | ||||||
| 		paintStroke.setStyle(Style.STROKE); |  | ||||||
| 
 |  | ||||||
| 		Polyline line = new Polyline(paintStroke, AwtGraphicFactory.INSTANCE); |  | ||||||
| 
 |  | ||||||
| 		for (Arc arc : path.getArcs()) { |  | ||||||
| 			ArrayList<org.insa.graph.Point> points = arc.getPoints(); |  | ||||||
| 			for (int i = 0; i < points.size(); ++i) { |  | ||||||
| 				line.getLatLongs().add( |  | ||||||
| 						new LatLong(points.get(i).getLatitude(), points.get(i).getLongitude())); |  | ||||||
| 			} |  | ||||||
| 		} |  | ||||||
| 
 |  | ||||||
| 		mapView.getLayerManager().getLayers().add(line); |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	private static BoundingBox addLayers(MapView mapView, List<File> mapFiles, |  | ||||||
| 			HillsRenderConfig hillsRenderConfig) { |  | ||||||
| 		Layers layers = mapView.getLayerManager().getLayers(); |  | ||||||
| 
 |  | ||||||
| 		int tileSize = SHOW_RASTER_MAP ? 256 : 512; |  | ||||||
| 
 |  | ||||||
| 		// Tile cache |  | ||||||
| 		TileCache tileCache = AwtUtil.createTileCache(tileSize, |  | ||||||
| 				mapView.getModel().frameBufferModel.getOverdrawFactor(), 1024, |  | ||||||
| 				new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString())); |  | ||||||
| 
 |  | ||||||
| 		final BoundingBox boundingBox; |  | ||||||
| 		if (SHOW_RASTER_MAP) { |  | ||||||
| 			// Raster |  | ||||||
| 			mapView.getModel().displayModel.setFixedTileSize(tileSize); |  | ||||||
| 			TileSource tileSource = OpenStreetMapMapnik.INSTANCE; |  | ||||||
| 			TileDownloadLayer tileDownloadLayer = createTileDownloadLayer(tileCache, |  | ||||||
| 					mapView.getModel().mapViewPosition, tileSource); |  | ||||||
| 			layers.add(tileDownloadLayer); |  | ||||||
| 			tileDownloadLayer.start(); |  | ||||||
| 			mapView.setZoomLevelMin(tileSource.getZoomLevelMin()); |  | ||||||
| 			mapView.setZoomLevelMax(tileSource.getZoomLevelMax()); |  | ||||||
| 			boundingBox = new BoundingBox(LatLongUtils.LATITUDE_MIN, LatLongUtils.LONGITUDE_MIN, |  | ||||||
| 					LatLongUtils.LATITUDE_MAX, LatLongUtils.LONGITUDE_MAX); |  | ||||||
| 		} |  | ||||||
| 		else { |  | ||||||
| 			// Vector |  | ||||||
| 			mapView.getModel().displayModel.setFixedTileSize(tileSize); |  | ||||||
| 			MultiMapDataStore mapDataStore = new MultiMapDataStore( |  | ||||||
| 					MultiMapDataStore.DataPolicy.RETURN_ALL); |  | ||||||
| 			for (File file : mapFiles) { |  | ||||||
| 				mapDataStore.addMapDataStore(new MapFile(file), false, false); |  | ||||||
| 			} |  | ||||||
| 			TileRendererLayer tileRendererLayer = createTileRendererLayer(tileCache, mapDataStore, |  | ||||||
| 					mapView.getModel().mapViewPosition, hillsRenderConfig); |  | ||||||
| 			layers.add(tileRendererLayer); |  | ||||||
| 			boundingBox = mapDataStore.boundingBox(); |  | ||||||
| 		} |  | ||||||
| 
 |  | ||||||
| 		// Debug |  | ||||||
| 		if (SHOW_DEBUG_LAYERS) { |  | ||||||
| 			layers.add(new TileGridLayer(GRAPHIC_FACTORY, mapView.getModel().displayModel)); |  | ||||||
| 			layers.add(new TileCoordinatesLayer(GRAPHIC_FACTORY, mapView.getModel().displayModel)); |  | ||||||
| 		} |  | ||||||
| 
 |  | ||||||
| 		return boundingBox; |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	private static MapView createMapView() { |  | ||||||
| 		MapView mapView = new MapView(); |  | ||||||
| 		mapView.getMapScaleBar().setVisible(true); |  | ||||||
| 		if (SHOW_DEBUG_LAYERS) { |  | ||||||
| 			mapView.getFpsCounter().setVisible(true); |  | ||||||
| 		} |  | ||||||
| 
 |  | ||||||
| 		return mapView; |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	@SuppressWarnings("unused") |  | ||||||
| 	private static TileDownloadLayer createTileDownloadLayer(TileCache tileCache, |  | ||||||
| 			MapViewPosition mapViewPosition, TileSource tileSource) { |  | ||||||
| 		return new TileDownloadLayer(tileCache, mapViewPosition, tileSource, GRAPHIC_FACTORY) { |  | ||||||
| 			@Override |  | ||||||
| 			public boolean onTap(LatLong tapLatLong, Point layerXY, Point tapXY) { |  | ||||||
| 				System.out.println("Tap on: " + tapLatLong); |  | ||||||
| 				return true; |  | ||||||
| 			} |  | ||||||
| 		}; |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	private static TileRendererLayer createTileRendererLayer(TileCache tileCache, |  | ||||||
| 			MapDataStore mapDataStore, MapViewPosition mapViewPosition, |  | ||||||
| 			HillsRenderConfig hillsRenderConfig) { |  | ||||||
| 		TileRendererLayer tileRendererLayer = new TileRendererLayer(tileCache, mapDataStore, |  | ||||||
| 				mapViewPosition, false, true, false, GRAPHIC_FACTORY, hillsRenderConfig) { |  | ||||||
| 			@Override |  | ||||||
| 			public boolean onTap(LatLong tapLatLong, Point layerXY, Point tapXY) { |  | ||||||
| 				System.out.println("Tap on: " + tapLatLong); |  | ||||||
| 				return true; |  | ||||||
| 			} |  | ||||||
| 		}; |  | ||||||
| 		tileRendererLayer.setXmlRenderTheme(InternalRenderTheme.DEFAULT); |  | ||||||
| 		return tileRendererLayer; |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	private static File getDemFolder(String[] args) { |  | ||||||
| 		if (args.length == 0) { |  | ||||||
| 			throw new IllegalArgumentException("missing argument: <mapFile>"); |  | ||||||
| 		} |  | ||||||
| 
 |  | ||||||
| 		File demFolder = new File(args[0]); |  | ||||||
| 		if (demFolder.exists() && demFolder.isDirectory() && demFolder.canRead()) { |  | ||||||
| 			return demFolder; |  | ||||||
| 		} |  | ||||||
| 		return null; |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	private static List<File> getMapFiles(String[] args) { |  | ||||||
| 		if (args.length == 0) { |  | ||||||
| 			throw new IllegalArgumentException("missing argument: <mapFile>"); |  | ||||||
| 		} |  | ||||||
| 
 |  | ||||||
| 		List<File> result = new ArrayList<>(); |  | ||||||
| 		for (String arg : args) { |  | ||||||
| 			File mapFile = new File(arg); |  | ||||||
| 			if (!mapFile.exists()) { |  | ||||||
| 				throw new IllegalArgumentException("file does not exist: " + mapFile); |  | ||||||
| 			} |  | ||||||
| 			else if (!mapFile.isFile()) { |  | ||||||
| 				throw new IllegalArgumentException("not a file: " + mapFile); |  | ||||||
| 			} |  | ||||||
| 			else if (!mapFile.canRead()) { |  | ||||||
| 				throw new IllegalArgumentException("cannot read file: " + mapFile); |  | ||||||
| 			} |  | ||||||
| 			result.add(mapFile); |  | ||||||
| 		} |  | ||||||
| 		return result; |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	private Samples() { |  | ||||||
| 		throw new IllegalStateException(); |  | ||||||
| 	} |  | ||||||
| } |  | ||||||
|  | @ -5,12 +5,16 @@ import java.awt.Color; | ||||||
| import java.awt.Graphics; | import java.awt.Graphics; | ||||||
| import java.awt.Graphics2D; | import java.awt.Graphics2D; | ||||||
| import java.awt.Image; | import java.awt.Image; | ||||||
|  | import java.awt.event.MouseAdapter; | ||||||
| import java.awt.event.MouseEvent; | import java.awt.event.MouseEvent; | ||||||
|  | import java.awt.event.MouseListener; | ||||||
| import java.awt.geom.NoninvertibleTransformException; | import java.awt.geom.NoninvertibleTransformException; | ||||||
| import java.awt.geom.Point2D; | import java.awt.geom.Point2D; | ||||||
| import java.awt.image.*; | import java.awt.image.BufferedImage; | ||||||
| import java.util.ArrayList; | import java.util.ArrayList; | ||||||
|  | import java.util.IdentityHashMap; | ||||||
| import java.util.Iterator; | import java.util.Iterator; | ||||||
|  | import java.util.Map; | ||||||
| 
 | 
 | ||||||
| import javax.swing.JPanel; | import javax.swing.JPanel; | ||||||
| 
 | 
 | ||||||
|  | @ -21,294 +25,321 @@ import org.insa.graph.Path; | ||||||
| import org.insa.graph.Point; | import org.insa.graph.Point; | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  *   Cette implementation de la classe Dessin produit vraiment un affichage |  * Cette implementation de la classe Dessin produit vraiment un affichage (au | ||||||
|  *   (au contraire de la classe DessinInvisible). |  * contraire de la classe DessinInvisible). | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| public class BasicDrawing extends JPanel implements Drawing { | public class BasicDrawing extends JPanel implements Drawing { | ||||||
| 
 | 
 | ||||||
| 	/** |     /** | ||||||
| 	 *  |      *  | ||||||
| 	 */ |      */ | ||||||
| 	private static final long serialVersionUID = 96779785877771827L; |     private static final long serialVersionUID = 96779785877771827L; | ||||||
| 	 |  | ||||||
| 	// Default path color. |  | ||||||
| 	public static final Color DEFAULT_PATH_COLOR = new Color(255, 0, 255); |  | ||||||
| 
 | 
 | ||||||
| 	// Default palette. |     // Default path color. | ||||||
| 	public static final GraphPalette DEFAULT_PALETTE = new BasicGraphPalette(); |     public static final Color DEFAULT_PATH_COLOR = new Color(255, 0, 255); | ||||||
| 	 |  | ||||||
| 	// Default marker width |  | ||||||
| 	private static final int DEFAULT_MARKER_WIDTH = 10; |  | ||||||
| 	 |  | ||||||
| 	//  |  | ||||||
| 	private final Graphics2D gr; |  | ||||||
| 
 | 
 | ||||||
| 	private double long1, long2, lat1, lat2; |     // Default palette. | ||||||
| 	 |     public static final GraphPalette DEFAULT_PALETTE = new BasicGraphPalette(); | ||||||
| 	// Width and height of the image |  | ||||||
| 	private final int width, height; |  | ||||||
| 	 |  | ||||||
| 	private Image image; |  | ||||||
| 	private ZoomAndPanListener zoomAndPanListener; |  | ||||||
| 	 |  | ||||||
| 	/** |  | ||||||
| 	 * Create a new BasicDrawing. |  | ||||||
| 	 *  |  | ||||||
| 	 */ |  | ||||||
| 	public BasicDrawing() { |  | ||||||
| 		 |  | ||||||
| 		this.zoomAndPanListener = new ZoomAndPanListener(this, ZoomAndPanListener.DEFAULT_MIN_ZOOM_LEVEL, 20, 1.2); |  | ||||||
| 		this.addMouseListener(zoomAndPanListener); |  | ||||||
| 		this.addMouseMotionListener(zoomAndPanListener); |  | ||||||
| 		this.addMouseWheelListener(zoomAndPanListener); |  | ||||||
| 		 |  | ||||||
| 		this.width = 2000; |  | ||||||
| 		this.height = 1600; |  | ||||||
| 		 |  | ||||||
| 		BufferedImage img = new BufferedImage(this.width, this.height, BufferedImage.TYPE_3BYTE_BGR); |  | ||||||
| 		 |  | ||||||
| 		this.image = img; |  | ||||||
| 		this.gr = img.createGraphics(); |  | ||||||
| 		 |  | ||||||
| 		this.zoomAndPanListener.setCoordTransform(this.gr.getTransform()); |  | ||||||
| 
 | 
 | ||||||
| 		this.long1 = -180; |     // Default marker width | ||||||
| 		this.long2 = 180; |     private static final int DEFAULT_MARKER_WIDTH = 10; | ||||||
| 		this.lat1  = -90; |  | ||||||
| 		this.lat2  = 90; |  | ||||||
| 
 | 
 | ||||||
| 		this.clear(); |     // | ||||||
| 		this.repaint(); |     private final Graphics2D gr; | ||||||
| 
 | 
 | ||||||
| 	} |     private double long1, long2, lat1, lat2; | ||||||
| 
 | 
 | ||||||
| 	@Override |     // Width and height of the image | ||||||
| 	public void paintComponent(Graphics g1) { |     private final int width, height; | ||||||
| 		Graphics2D g = (Graphics2D)g1; |  | ||||||
| 		g.clearRect(0, 0, getWidth(), getHeight()); |  | ||||||
| 		g.setTransform(zoomAndPanListener.getCoordTransform()); |  | ||||||
| 		g.drawImage(image, 0, 0, this); |  | ||||||
| 	} |  | ||||||
| 
 | 
 | ||||||
| 	protected void setBB(double long1, double long2, double lat1, double lat2) {	 |     // | ||||||
|  |     private Image image; | ||||||
|  |     private ZoomAndPanListener zoomAndPanListener; | ||||||
| 
 | 
 | ||||||
| 		if (long1 > long2 || lat1 > lat2) { |     // Mapping DrawingClickListener -> MouseEventListener | ||||||
| 			throw new Error("DessinVisible.setBB : mauvaises coordonnees."); |     private Map<DrawingClickListener, MouseListener> listenerMapping = new IdentityHashMap<>(); | ||||||
| 		} |  | ||||||
| 				 |  | ||||||
| 		this.long1 = long1; |  | ||||||
| 		this.long2 = long2; |  | ||||||
| 		this.lat1= lat1; |  | ||||||
| 		this.lat2 = lat2; |  | ||||||
| 				 |  | ||||||
| 		double scale = 1 / Math.max(this.width / (double)this.getWidth(),  this.height / (double)this.getHeight()); |  | ||||||
| 		 |  | ||||||
| 		this.zoomAndPanListener.getCoordTransform().setToIdentity(); |  | ||||||
| 		this.zoomAndPanListener.getCoordTransform().translate((this.getWidth() - this.width * scale) / 2,  |  | ||||||
| 				(this.getHeight() - this.height * scale) / 2); |  | ||||||
| 		this.zoomAndPanListener.getCoordTransform().scale(scale, scale); |  | ||||||
| 		this.zoomAndPanListener.setZoomLevel(0); |  | ||||||
| 		this.repaint(); |  | ||||||
| 		 |  | ||||||
| 	} |  | ||||||
| 
 | 
 | ||||||
| 	private int projx(double lon) { |     /** | ||||||
| 		return (int)(width * (lon - this.long1) / (this.long2 - this.long1)) ; |      * Create a new BasicDrawing. | ||||||
| 	} |      *  | ||||||
|  |      */ | ||||||
|  |     public BasicDrawing() { | ||||||
| 
 | 
 | ||||||
| 	private int projy(double lat) { |         this.zoomAndPanListener = new ZoomAndPanListener(this, ZoomAndPanListener.DEFAULT_MIN_ZOOM_LEVEL, 20, 1.2); | ||||||
| 		return (int)(height * (1 - (lat - this.lat1) / (this.lat2 - this.lat1))) ; |         this.addMouseListener(zoomAndPanListener); | ||||||
| 	} |         this.addMouseMotionListener(zoomAndPanListener); | ||||||
| 	 |         this.addMouseWheelListener(zoomAndPanListener); | ||||||
| 	/** |  | ||||||
| 	 * Return the longitude and latitude corresponding to the given |  | ||||||
| 	 * position of the MouseEvent. |  | ||||||
| 	 *  |  | ||||||
| 	 * @param event |  | ||||||
| 	 *  |  | ||||||
| 	 * @return |  | ||||||
| 	 */ |  | ||||||
| 	public Point getLongitudeLatitude(MouseEvent event) throws NoninvertibleTransformException { |  | ||||||
| 		// Get the point using the inverse transform of the Zoom/Pan object, this gives us |  | ||||||
| 		// a point within the drawing box (between [0, 0] and [width, height]). |  | ||||||
| 		Point2D ptDst = this.zoomAndPanListener.getCoordTransform().inverseTransform(event.getPoint(), null); |  | ||||||
| 		 |  | ||||||
| 		// Inverse the "projection" on x/y to get longitude and latitude. |  | ||||||
| 		double lon = ptDst.getX(); |  | ||||||
| 		double lat = ptDst.getY(); |  | ||||||
| 		lon = (lon / this.width) * (this.long2 - this.long1) + this.long1; |  | ||||||
| 		lat = (1 - lat / this.height) * (this.lat2 - this.lat1) + this.lat1; |  | ||||||
| 		 |  | ||||||
| 		// Return a new point. |  | ||||||
| 		return new Point(lon, lat);			 |  | ||||||
| 	} |  | ||||||
| 	 |  | ||||||
| 	protected void setWidth(int width) { |  | ||||||
| 		this.gr.setStroke(new BasicStroke(width)); |  | ||||||
| 	} |  | ||||||
| 
 | 
 | ||||||
| 	protected void setColor(Color col) { |         this.width = 2000; | ||||||
| 		this.gr.setColor(col); |         this.height = 1600; | ||||||
| 	} |  | ||||||
| 	 |  | ||||||
| 	@Override |  | ||||||
| 	public void clear() { |  | ||||||
| 		this.gr.setColor(Color.WHITE); |  | ||||||
| 		this.gr.fillRect(0, 0, this.width, this.height); |  | ||||||
| 	} |  | ||||||
| 
 | 
 | ||||||
| 	@Override |         BufferedImage img = new BufferedImage(this.width, this.height, BufferedImage.TYPE_3BYTE_BGR); | ||||||
| 	public void drawLine(Point from, Point to) { |  | ||||||
| 		int x1 = this.projx(from.getLongitude()) ; |  | ||||||
| 		int x2 = this.projx(to.getLongitude()) ; |  | ||||||
| 		int y1 = this.projy(from.getLatitude()) ; |  | ||||||
| 		int y2 = this.projy(to.getLatitude()) ; |  | ||||||
| 
 | 
 | ||||||
| 		gr.drawLine(x1, y1, x2, y2) ; |         this.image = img; | ||||||
| 		this.repaint();	 |         this.gr = img.createGraphics(); | ||||||
| 	} |  | ||||||
| 
 | 
 | ||||||
| 	@Override |         this.zoomAndPanListener.setCoordTransform(this.gr.getTransform()); | ||||||
| 	public void drawLine(Point from, Point to, int width) { |  | ||||||
| 		setWidth(width); |  | ||||||
| 		drawLine(from, to);		 |  | ||||||
| 	} |  | ||||||
| 
 | 
 | ||||||
| 	@Override |         this.long1 = -180; | ||||||
| 	public void drawLine(Point from, Point to, int width, Color color) { |         this.long2 = 180; | ||||||
| 		setWidth(width); |         this.lat1 = -90; | ||||||
| 		setColor(color);	 |         this.lat2 = 90; | ||||||
| 		drawLine(from, to); |  | ||||||
| 	} |  | ||||||
| 	 |  | ||||||
| 	@Override |  | ||||||
| 	public void drawMarker(Point point) { |  | ||||||
| 		drawPoint(point, DEFAULT_MARKER_WIDTH, this.gr.getColor()); |  | ||||||
| 	} |  | ||||||
| 
 | 
 | ||||||
|  |         this.clear(); | ||||||
|  |         this.repaint(); | ||||||
| 
 | 
 | ||||||
| 	@Override |     } | ||||||
| 	public void drawMarker(Point point, Color color) { |  | ||||||
| 		setColor(color); |  | ||||||
| 		drawMarker(point); |  | ||||||
| 	} |  | ||||||
| 	 |  | ||||||
| 	@Override |  | ||||||
| 	public void drawPoint(Point point, int width, Color color) { |  | ||||||
| 		setWidth(width); |  | ||||||
| 		setColor(color); |  | ||||||
| 		int x = this.projx(point.getLongitude()) - DEFAULT_MARKER_WIDTH / 2; |  | ||||||
| 		int y = this.projy(point.getLatitude()) - DEFAULT_MARKER_WIDTH / 2; |  | ||||||
| 		gr.fillOval(x, y, DEFAULT_MARKER_WIDTH, DEFAULT_MARKER_WIDTH); |  | ||||||
| 		this.repaint(); |  | ||||||
| 	} |  | ||||||
| 	 |  | ||||||
| 	/** |  | ||||||
| 	 * Draw the given arc. |  | ||||||
| 	 *  |  | ||||||
| 	 * @param arc Arc to draw. |  | ||||||
| 	 * @param palette Palette to use to retrieve color and width for arc, |  | ||||||
| 	 * or null to use current settings. |  | ||||||
| 	 */ |  | ||||||
| 	public void drawArc(Arc arc, GraphPalette palette) { |  | ||||||
| 		ArrayList<Point> pts = arc.getPoints(); |  | ||||||
| 		if (!pts.isEmpty()) { |  | ||||||
| 			if (palette != null) { |  | ||||||
| 				setColor(palette.getColorForType(arc.getInfo().getType())); |  | ||||||
| 				setWidth(palette.getWidthForType(arc.getInfo().getType())); |  | ||||||
| 			} |  | ||||||
| 			Iterator<Point> it1 = pts.iterator(); |  | ||||||
| 			Point prev = it1.next(); |  | ||||||
| 			while (it1.hasNext()) { |  | ||||||
| 				Point curr = it1.next(); |  | ||||||
| 				drawLine(prev, curr); |  | ||||||
| 				prev = curr; |  | ||||||
| 			} |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
| 	 |  | ||||||
| 	/** |  | ||||||
| 	 * Initialize the drawing for the given graph. |  | ||||||
| 	 *  |  | ||||||
| 	 * @param graph |  | ||||||
| 	 */ |  | ||||||
| 	public void initialize(Graph graph) { |  | ||||||
| 		double minLon = Double.POSITIVE_INFINITY, minLat = Double.POSITIVE_INFINITY,  |  | ||||||
| 				maxLon = Double.NEGATIVE_INFINITY, maxLat = Double.NEGATIVE_INFINITY; |  | ||||||
| 		for (Node node: graph.getNodes()) { |  | ||||||
| 			Point pt = node.getPoint(); |  | ||||||
| 			if (pt.getLatitude() < minLat) { |  | ||||||
| 				minLat = pt.getLatitude(); |  | ||||||
| 			} |  | ||||||
| 			if (pt.getLatitude() > maxLat) { |  | ||||||
| 				maxLat = pt.getLatitude(); |  | ||||||
| 			} |  | ||||||
| 			if (pt.getLongitude() < minLon) { |  | ||||||
| 				minLon = pt.getLongitude(); |  | ||||||
| 			} |  | ||||||
| 			if (pt.getLongitude() > maxLon) { |  | ||||||
| 				maxLon = pt.getLongitude(); |  | ||||||
| 			} |  | ||||||
| 		} |  | ||||||
| 		 |  | ||||||
| 		double deltaLon = 0.02 * (maxLon - minLon), |  | ||||||
| 				deltaLat = 0.02 * (maxLat - minLat); |  | ||||||
| 
 | 
 | ||||||
| 		setBB(minLon - deltaLon, maxLon + deltaLon,  |     @Override | ||||||
| 			minLat - deltaLat, maxLat + deltaLat); |     public void paintComponent(Graphics g1) { | ||||||
| 	} |         Graphics2D g = (Graphics2D) g1; | ||||||
|  |         g.clearRect(0, 0, getWidth(), getHeight()); | ||||||
|  |         g.setTransform(zoomAndPanListener.getCoordTransform()); | ||||||
|  |         g.drawImage(image, 0, 0, this); | ||||||
|  |     } | ||||||
| 
 | 
 | ||||||
| 	@Override |     protected void setBB(double long1, double long2, double lat1, double lat2) { | ||||||
| 	public void drawGraph(Graph graph, GraphPalette palette) { |  | ||||||
| 		clear(); |  | ||||||
| 		initialize(graph); |  | ||||||
| 		for (Node node: graph.getNodes()) { |  | ||||||
| 			for (Arc arc: node.getSuccessors()) { |  | ||||||
| 				drawArc(arc, palette); |  | ||||||
| 			} |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
| 
 | 
 | ||||||
| 	@Override |         if (long1 > long2 || lat1 > lat2) { | ||||||
| 	public void drawGraph(Graph graph) { |             throw new Error("DessinVisible.setBB : mauvaises coordonnees."); | ||||||
| 		drawGraph(graph, DEFAULT_PALETTE);		 |         } | ||||||
| 	} |  | ||||||
| 
 | 
 | ||||||
| 	@Override |         this.long1 = long1; | ||||||
| 	public void drawPath(Path path, Color color, boolean markers) { |         this.long2 = long2; | ||||||
| 		setColor(color); |         this.lat1 = lat1; | ||||||
| 		setWidth(2); |         this.lat2 = lat2; | ||||||
| 		for (Arc arc: path.getArcs()) { |  | ||||||
| 			drawArc(arc, null); |  | ||||||
| 		} |  | ||||||
| 		if (markers) { |  | ||||||
| 			drawMarker(path.getOrigin().getPoint(), color); |  | ||||||
| 			drawMarker(path.getDestination().getPoint(), color); |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
| 	 |  | ||||||
| 	@Override |  | ||||||
| 	public void drawPath(Path path, Color color) { |  | ||||||
| 		drawPath(path, color, true); |  | ||||||
| 	} |  | ||||||
| 
 | 
 | ||||||
| 	@Override |         double scale = 1 / Math.max(this.width / (double) this.getWidth(), this.height / (double) this.getHeight()); | ||||||
| 	public void drawPath(Path path) { | 
 | ||||||
| 		drawPath(path, DEFAULT_PATH_COLOR); |         this.zoomAndPanListener.getCoordTransform().setToIdentity(); | ||||||
| 	} |         this.zoomAndPanListener.getCoordTransform().translate((this.getWidth() - this.width * scale) / 2, | ||||||
| 	 |                 (this.getHeight() - this.height * scale) / 2); | ||||||
| 	@Override |         this.zoomAndPanListener.getCoordTransform().scale(scale, scale); | ||||||
| 	public void drawPath(Path path, boolean markers) { |         this.zoomAndPanListener.setZoomLevel(0); | ||||||
| 		drawPath(path, DEFAULT_PATH_COLOR, markers); |         this.repaint(); | ||||||
| 	} | 
 | ||||||
| 	 |     } | ||||||
| 	@SuppressWarnings("unused") | 
 | ||||||
| 	private void putText(Point point, String txt) { |     private int projx(double lon) { | ||||||
| 		int x = this.projx(point.getLongitude()); |         return (int) (width * (lon - this.long1) / (this.long2 - this.long1)); | ||||||
| 		int y = this.projy(point.getLatitude()); |     } | ||||||
| 		gr.drawString(txt, x, y); | 
 | ||||||
| 		this.repaint();	 |     private int projy(double lat) { | ||||||
| 	} |         return (int) (height * (1 - (lat - this.lat1) / (this.lat2 - this.lat1))); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * Return the longitude and latitude corresponding to the given position of the | ||||||
|  |      * MouseEvent. | ||||||
|  |      *  | ||||||
|  |      * @param event | ||||||
|  |      *  | ||||||
|  |      * @return | ||||||
|  |      */ | ||||||
|  |     public Point getLongitudeLatitude(MouseEvent event) throws NoninvertibleTransformException { | ||||||
|  |         // Get the point using the inverse transform of the Zoom/Pan object, this gives | ||||||
|  |         // us | ||||||
|  |         // a point within the drawing box (between [0, 0] and [width, height]). | ||||||
|  |         Point2D ptDst = this.zoomAndPanListener.getCoordTransform().inverseTransform(event.getPoint(), null); | ||||||
|  | 
 | ||||||
|  |         // Inverse the "projection" on x/y to get longitude and latitude. | ||||||
|  |         double lon = ptDst.getX(); | ||||||
|  |         double lat = ptDst.getY(); | ||||||
|  |         lon = (lon / this.width) * (this.long2 - this.long1) + this.long1; | ||||||
|  |         lat = (1 - lat / this.height) * (this.lat2 - this.lat1) + this.lat1; | ||||||
|  | 
 | ||||||
|  |         // Return a new point. | ||||||
|  |         return new Point(lon, lat); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void addDrawingClickListener(DrawingClickListener listener) { | ||||||
|  |         MouseListener mListener = new MouseAdapter() { | ||||||
|  |             @Override | ||||||
|  |             public void mouseClicked(MouseEvent evt) { | ||||||
|  |                 System.out.println(evt); | ||||||
|  |                 try { | ||||||
|  |                     listener.mouseClicked(getLongitudeLatitude(evt)); | ||||||
|  |                 } | ||||||
|  |                 catch (NoninvertibleTransformException e) { | ||||||
|  |                     e.printStackTrace(); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         }; | ||||||
|  |         this.addMouseListener(mListener); | ||||||
|  |         this.listenerMapping.put(listener, mListener); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void removeDrawingClickListener(DrawingClickListener listener) { | ||||||
|  |         this.removeMouseListener(this.listenerMapping.get(listener)); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     protected void setWidth(int width) { | ||||||
|  |         this.gr.setStroke(new BasicStroke(width)); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     protected void setColor(Color col) { | ||||||
|  |         this.gr.setColor(col); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void clear() { | ||||||
|  |         this.gr.setColor(Color.WHITE); | ||||||
|  |         this.gr.fillRect(0, 0, this.width, this.height); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void drawLine(Point from, Point to) { | ||||||
|  |         int x1 = this.projx(from.getLongitude()); | ||||||
|  |         int x2 = this.projx(to.getLongitude()); | ||||||
|  |         int y1 = this.projy(from.getLatitude()); | ||||||
|  |         int y2 = this.projy(to.getLatitude()); | ||||||
|  | 
 | ||||||
|  |         gr.drawLine(x1, y1, x2, y2); | ||||||
|  |         this.repaint(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void drawLine(Point from, Point to, int width) { | ||||||
|  |         setWidth(width); | ||||||
|  |         drawLine(from, to); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void drawLine(Point from, Point to, int width, Color color) { | ||||||
|  |         setWidth(width); | ||||||
|  |         setColor(color); | ||||||
|  |         drawLine(from, to); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void drawMarker(Point point) { | ||||||
|  |         drawPoint(point, DEFAULT_MARKER_WIDTH, this.gr.getColor()); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void drawMarker(Point point, Color color) { | ||||||
|  |         setColor(color); | ||||||
|  |         drawMarker(point); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void drawPoint(Point point, int width, Color color) { | ||||||
|  |         setWidth(width); | ||||||
|  |         setColor(color); | ||||||
|  |         int x = this.projx(point.getLongitude()) - DEFAULT_MARKER_WIDTH / 2; | ||||||
|  |         int y = this.projy(point.getLatitude()) - DEFAULT_MARKER_WIDTH / 2; | ||||||
|  |         gr.fillOval(x, y, DEFAULT_MARKER_WIDTH, DEFAULT_MARKER_WIDTH); | ||||||
|  |         this.repaint(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * Draw the given arc. | ||||||
|  |      *  | ||||||
|  |      * @param arc | ||||||
|  |      *            Arc to draw. | ||||||
|  |      * @param palette | ||||||
|  |      *            Palette to use to retrieve color and width for arc, or null to use | ||||||
|  |      *            current settings. | ||||||
|  |      */ | ||||||
|  |     public void drawArc(Arc arc, GraphPalette palette) { | ||||||
|  |         ArrayList<Point> pts = arc.getPoints(); | ||||||
|  |         if (!pts.isEmpty()) { | ||||||
|  |             if (palette != null) { | ||||||
|  |                 setColor(palette.getColorForType(arc.getInfo().getType())); | ||||||
|  |                 setWidth(palette.getWidthForType(arc.getInfo().getType())); | ||||||
|  |             } | ||||||
|  |             Iterator<Point> it1 = pts.iterator(); | ||||||
|  |             Point prev = it1.next(); | ||||||
|  |             while (it1.hasNext()) { | ||||||
|  |                 Point curr = it1.next(); | ||||||
|  |                 drawLine(prev, curr); | ||||||
|  |                 prev = curr; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * Initialize the drawing for the given graph. | ||||||
|  |      *  | ||||||
|  |      * @param graph | ||||||
|  |      */ | ||||||
|  |     public void initialize(Graph graph) { | ||||||
|  |         double minLon = Double.POSITIVE_INFINITY, minLat = Double.POSITIVE_INFINITY, maxLon = Double.NEGATIVE_INFINITY, | ||||||
|  |                 maxLat = Double.NEGATIVE_INFINITY; | ||||||
|  |         for (Node node: graph.getNodes()) { | ||||||
|  |             Point pt = node.getPoint(); | ||||||
|  |             if (pt.getLatitude() < minLat) { | ||||||
|  |                 minLat = pt.getLatitude(); | ||||||
|  |             } | ||||||
|  |             if (pt.getLatitude() > maxLat) { | ||||||
|  |                 maxLat = pt.getLatitude(); | ||||||
|  |             } | ||||||
|  |             if (pt.getLongitude() < minLon) { | ||||||
|  |                 minLon = pt.getLongitude(); | ||||||
|  |             } | ||||||
|  |             if (pt.getLongitude() > maxLon) { | ||||||
|  |                 maxLon = pt.getLongitude(); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         double deltaLon = 0.02 * (maxLon - minLon), deltaLat = 0.02 * (maxLat - minLat); | ||||||
|  | 
 | ||||||
|  |         setBB(minLon - deltaLon, maxLon + deltaLon, minLat - deltaLat, maxLat + deltaLat); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void drawGraph(Graph graph, GraphPalette palette) { | ||||||
|  |         clear(); | ||||||
|  |         initialize(graph); | ||||||
|  |         for (Node node: graph.getNodes()) { | ||||||
|  |             for (Arc arc: node.getSuccessors()) { | ||||||
|  |                 drawArc(arc, palette); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void drawGraph(Graph graph) { | ||||||
|  |         drawGraph(graph, DEFAULT_PALETTE); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void drawPath(Path path, Color color, boolean markers) { | ||||||
|  |         setColor(color); | ||||||
|  |         setWidth(2); | ||||||
|  |         for (Arc arc: path.getArcs()) { | ||||||
|  |             drawArc(arc, null); | ||||||
|  |         } | ||||||
|  |         if (markers) { | ||||||
|  |             drawMarker(path.getOrigin().getPoint(), color); | ||||||
|  |             drawMarker(path.getDestination().getPoint(), color); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void drawPath(Path path, Color color) { | ||||||
|  |         drawPath(path, color, true); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void drawPath(Path path) { | ||||||
|  |         drawPath(path, DEFAULT_PATH_COLOR); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void drawPath(Path path, boolean markers) { | ||||||
|  |         drawPath(path, DEFAULT_PATH_COLOR, markers); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @SuppressWarnings("unused") | ||||||
|  |     private void putText(Point point, String txt) { | ||||||
|  |         int x = this.projx(point.getLongitude()); | ||||||
|  |         int y = this.projy(point.getLatitude()); | ||||||
|  |         gr.drawString(txt, x, y); | ||||||
|  |         this.repaint(); | ||||||
|  |     } | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -7,113 +7,128 @@ import org.insa.graph.Path; | ||||||
| import org.insa.graph.Point; | import org.insa.graph.Point; | ||||||
| 
 | 
 | ||||||
| public interface Drawing { | public interface Drawing { | ||||||
| 	 |  | ||||||
| 	/** |  | ||||||
| 	 * Clear the drawing. |  | ||||||
| 	 */ |  | ||||||
| 	public void clear(); |  | ||||||
| 
 | 
 | ||||||
| 	/** |     /** | ||||||
| 	 * Draw a line between the two given points with the default color |      * Add a listener to click to this drawing. | ||||||
| 	 * and width. |      *  | ||||||
| 	 *  |      * @param listener | ||||||
| 	 * @param from |      */ | ||||||
| 	 * @param to |     public void addDrawingClickListener(DrawingClickListener listener); | ||||||
| 	 */ | 
 | ||||||
| 	public void drawLine(Point from, Point to); |     /** | ||||||
| 	 |      * Remove the given listener from the drawing. | ||||||
| 	/** |      *  | ||||||
| 	 * Draw a line between the two given points with the default color |      * @param listener | ||||||
| 	 * and the given width. |      */ | ||||||
| 	 *  |     public void removeDrawingClickListener(DrawingClickListener listener); | ||||||
| 	 * @param from | 
 | ||||||
| 	 * @param to |     /** | ||||||
| 	 * @param width |      * Clear the drawing. | ||||||
| 	 */ |      */ | ||||||
| 	public void drawLine(Point from, Point to, int width); |     public void clear(); | ||||||
| 	 | 
 | ||||||
| 	/** |     /** | ||||||
| 	 * Draw a line between the two given points with the given color |      * Draw a line between the two given points with the default color and width. | ||||||
| 	 * and the given width. |      *  | ||||||
| 	 *  |      * @param from | ||||||
| 	 * @param from |      * @param to | ||||||
| 	 * @param to |      */ | ||||||
| 	 * @param width |     public void drawLine(Point from, Point to); | ||||||
| 	 * @param color | 
 | ||||||
| 	 */ |     /** | ||||||
| 	public void drawLine(Point from, Point to, int width, Color color); |      * Draw a line between the two given points with the default color and the given | ||||||
| 	 |      * width. | ||||||
| 	/** |      *  | ||||||
| 	 * Draw a marker at the given point with the default color. |      * @param from | ||||||
| 	 *  |      * @param to | ||||||
| 	 * @param point |      * @param width | ||||||
| 	 */ |      */ | ||||||
| 	public void drawMarker(Point point); |     public void drawLine(Point from, Point to, int width); | ||||||
| 	 | 
 | ||||||
| 	/** |     /** | ||||||
| 	 * Draw the given point with the given color. |      * Draw a line between the two given points with the given color and the given | ||||||
| 	 *  |      * width. | ||||||
| 	 * @param point |      *  | ||||||
| 	 */ |      * @param from | ||||||
| 	public void drawMarker(Point point, Color color); |      * @param to | ||||||
| 	 |      * @param width | ||||||
| 	/** |      * @param color | ||||||
| 	 * Draw a point width the given width and color. Do not use this to mark location, |      */ | ||||||
| 	 * use drawMarker. |     public void drawLine(Point from, Point to, int width, Color color); | ||||||
| 	 *  | 
 | ||||||
| 	 * @param point |     /** | ||||||
| 	 * @param width |      * Draw a marker at the given point with the default color. | ||||||
| 	 * @param color |      *  | ||||||
| 	 */ |      * @param point | ||||||
| 	public void drawPoint(Point point, int width, Color color); |      */ | ||||||
|  |     public void drawMarker(Point point); | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * Draw the given point with the given color. | ||||||
|  |      *  | ||||||
|  |      * @param point | ||||||
|  |      */ | ||||||
|  |     public void drawMarker(Point point, Color color); | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * Draw a point width the given width and color. Do not use this to mark | ||||||
|  |      * location, use drawMarker. | ||||||
|  |      *  | ||||||
|  |      * @param point | ||||||
|  |      * @param width | ||||||
|  |      * @param color | ||||||
|  |      */ | ||||||
|  |     public void drawPoint(Point point, int width, Color color); | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * Draw the given graph using the given palette. | ||||||
|  |      *  | ||||||
|  |      * @param graph | ||||||
|  |      * @param palette | ||||||
|  |      */ | ||||||
|  |     public void drawGraph(Graph graph, GraphPalette palette); | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * Draw the given graph using a default palette specific to the implementation. | ||||||
|  |      *  | ||||||
|  |      * @param graph | ||||||
|  |      */ | ||||||
|  |     public void drawGraph(Graph graph); | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * Draw a path using the given color. | ||||||
|  |      *  | ||||||
|  |      * @param path | ||||||
|  |      * @param color | ||||||
|  |      * @param markers | ||||||
|  |      *            Show origin and destination markers. | ||||||
|  |      */ | ||||||
|  |     public void drawPath(Path path, Color color, boolean markers); | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * Draw a path using the given color with markers. | ||||||
|  |      *  | ||||||
|  |      * @param path | ||||||
|  |      * @param color | ||||||
|  |      */ | ||||||
|  |     public void drawPath(Path path, Color color); | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * Draw a path using a default color specific to the implementation | ||||||
|  |      *  | ||||||
|  |      *  | ||||||
|  |      * @param path | ||||||
|  |      * @param markers | ||||||
|  |      *            Show origin and destination markers. | ||||||
|  |      */ | ||||||
|  |     public void drawPath(Path path, boolean markers); | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * Draw a path using a default color specific to the implementation | ||||||
|  |      *  | ||||||
|  |      *  | ||||||
|  |      * @param path | ||||||
|  |      */ | ||||||
|  |     public void drawPath(Path path); | ||||||
| 
 | 
 | ||||||
| 	/** |  | ||||||
| 	 * Draw the given graph using the given palette. |  | ||||||
| 	 *  |  | ||||||
| 	 * @param graph |  | ||||||
| 	 * @param palette |  | ||||||
| 	 */ |  | ||||||
| 	public void drawGraph(Graph graph, GraphPalette palette); |  | ||||||
| 	 |  | ||||||
| 	/** |  | ||||||
| 	 * Draw the given graph using a default palette specific to the implementation. |  | ||||||
| 	 *  |  | ||||||
| 	 * @param graph |  | ||||||
| 	 */ |  | ||||||
| 	public void drawGraph(Graph graph); |  | ||||||
| 	 |  | ||||||
| 	/** |  | ||||||
| 	 * Draw a path using the given color. |  | ||||||
| 	 *  |  | ||||||
| 	 * @param path |  | ||||||
| 	 * @param color |  | ||||||
| 	 * @param markers Show origin and destination markers. |  | ||||||
| 	 */ |  | ||||||
| 	public void drawPath(Path path, Color color, boolean markers); |  | ||||||
| 	 |  | ||||||
| 	/** |  | ||||||
| 	 * Draw a path using the given color with markers. |  | ||||||
| 	 *  |  | ||||||
| 	 * @param path |  | ||||||
| 	 * @param color |  | ||||||
| 	 */ |  | ||||||
| 	public void drawPath(Path path, Color color); |  | ||||||
| 	 |  | ||||||
| 	/** |  | ||||||
| 	 * Draw a path using a default color specific to the implementation |  | ||||||
| 	 *  |  | ||||||
| 	 *  |  | ||||||
| 	 * @param path |  | ||||||
| 	 * @param markers Show origin and destination markers. |  | ||||||
| 	 */ |  | ||||||
| 	public void drawPath(Path path, boolean markers); |  | ||||||
| 	 |  | ||||||
| 	/** |  | ||||||
| 	 * Draw a path using a default color specific to the implementation |  | ||||||
| 	 *  |  | ||||||
| 	 *  |  | ||||||
| 	 * @param path |  | ||||||
| 	 */ |  | ||||||
| 	public void drawPath(Path path); |  | ||||||
| 	 |  | ||||||
| } | } | ||||||
|  |  | ||||||
							
								
								
									
										14
									
								
								src/main/org/insa/drawing/DrawingClickListener.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								src/main/org/insa/drawing/DrawingClickListener.java
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,14 @@ | ||||||
|  | package org.insa.drawing; | ||||||
|  | 
 | ||||||
|  | import org.insa.graph.Point; | ||||||
|  | 
 | ||||||
|  | public interface DrawingClickListener { | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * Event triggered when a click is made on the map. | ||||||
|  |      *  | ||||||
|  |      * @param point | ||||||
|  |      */ | ||||||
|  |     public void mouseClicked(Point point); | ||||||
|  | 
 | ||||||
|  | } | ||||||
|  | @ -1,272 +1,324 @@ | ||||||
| package org.insa.drawing; | package org.insa.drawing; | ||||||
| 
 | 
 | ||||||
| import java.awt.Color; | import java.awt.Color; | ||||||
| import java.awt.event.MouseAdapter; | import java.awt.event.MouseAdapter; | ||||||
| import java.awt.event.MouseWheelEvent; | import java.awt.event.MouseWheelEvent; | ||||||
| import java.io.File; | import java.io.File; | ||||||
| import java.util.ArrayList; | import java.io.FileNotFoundException; | ||||||
| import java.util.HashMap; | import java.util.ArrayList; | ||||||
| import java.util.Map; | import java.util.HashMap; | ||||||
| import java.util.UUID; | import java.util.Map; | ||||||
| 
 | import java.util.UUID; | ||||||
| import javax.swing.JFileChooser; | 
 | ||||||
| import javax.swing.filechooser.FileNameExtensionFilter; | import javax.swing.JFileChooser; | ||||||
| 
 | import javax.swing.filechooser.FileNameExtensionFilter; | ||||||
| import org.insa.graph.Arc; | 
 | ||||||
| import org.insa.graph.Graph; | import org.insa.drawing.utils.MarkerUtils; | ||||||
| import org.insa.graph.Path; | import org.insa.graph.Arc; | ||||||
| import org.insa.graph.Point; | import org.insa.graph.Graph; | ||||||
| import org.mapsforge.core.graphics.GraphicFactory; | import org.insa.graph.Path; | ||||||
| import org.mapsforge.core.graphics.Paint; | import org.insa.graph.Point; | ||||||
| import org.mapsforge.core.graphics.Style; | import org.mapsforge.core.graphics.Bitmap; | ||||||
| import org.mapsforge.core.model.BoundingBox; | import org.mapsforge.core.graphics.GraphicFactory; | ||||||
| import org.mapsforge.core.model.LatLong; | import org.mapsforge.core.graphics.Paint; | ||||||
| import org.mapsforge.core.model.MapPosition; | import org.mapsforge.core.graphics.Style; | ||||||
| import org.mapsforge.core.util.LatLongUtils; | import org.mapsforge.core.model.BoundingBox; | ||||||
| import org.mapsforge.map.awt.graphics.AwtGraphicFactory; | import org.mapsforge.core.model.LatLong; | ||||||
| import org.mapsforge.map.awt.util.AwtUtil; | import org.mapsforge.core.model.MapPosition; | ||||||
| import org.mapsforge.map.awt.view.MapView; | import org.mapsforge.core.util.LatLongUtils; | ||||||
| import org.mapsforge.map.datastore.MapDataStore; | import org.mapsforge.map.awt.graphics.AwtGraphicFactory; | ||||||
| import org.mapsforge.map.layer.Layers; | import org.mapsforge.map.awt.util.AwtUtil; | ||||||
| import org.mapsforge.map.layer.cache.TileCache; | import org.mapsforge.map.awt.view.MapView; | ||||||
| import org.mapsforge.map.layer.hills.HillsRenderConfig; | import org.mapsforge.map.datastore.MapDataStore; | ||||||
| import org.mapsforge.map.layer.overlay.Marker; | import org.mapsforge.map.layer.Layers; | ||||||
| import org.mapsforge.map.layer.overlay.Polyline; | import org.mapsforge.map.layer.cache.TileCache; | ||||||
| import org.mapsforge.map.layer.renderer.TileRendererLayer; | import org.mapsforge.map.layer.hills.HillsRenderConfig; | ||||||
| import org.mapsforge.map.model.DisplayModel; | import org.mapsforge.map.layer.overlay.Marker; | ||||||
| import org.mapsforge.map.model.MapViewPosition; | import org.mapsforge.map.layer.overlay.Polyline; | ||||||
| import org.mapsforge.map.model.Model; | import org.mapsforge.map.layer.renderer.TileRendererLayer; | ||||||
| import org.mapsforge.map.reader.MapFile; | import org.mapsforge.map.model.DisplayModel; | ||||||
| import org.mapsforge.map.rendertheme.InternalRenderTheme; | import org.mapsforge.map.model.MapViewPosition; | ||||||
| import org.mapsforge.map.rendertheme.XmlRenderTheme; | import org.mapsforge.map.model.Model; | ||||||
| 
 | import org.mapsforge.map.reader.MapFile; | ||||||
| public class MapViewDrawing extends MapView implements Drawing { | import org.mapsforge.map.rendertheme.ExternalRenderTheme; | ||||||
| 
 | import org.mapsforge.map.rendertheme.XmlRenderTheme; | ||||||
|     /** | 
 | ||||||
|      *  | public class MapViewDrawing extends MapView implements Drawing { | ||||||
|      */ | 
 | ||||||
|     private static final long serialVersionUID = 8606967833704938092L; |     /** | ||||||
| 
 |      *  | ||||||
|     // Default path color. |      */ | ||||||
|     public static final Color DEFAULT_PATH_COLOR = new Color(66, 134, 244); |     private static final long serialVersionUID = 8606967833704938092L; | ||||||
| 
 | 
 | ||||||
|     // Graphic factory. |     // Default path color. | ||||||
|     private static final GraphicFactory GRAPHIC_FACTORY = AwtGraphicFactory.INSTANCE; |     public static final Color DEFAULT_PATH_COLOR = new Color(66, 134, 244); | ||||||
| 
 | 
 | ||||||
|     // Default tile size. |     // Graphic factory. | ||||||
|     private static final int DEFAULT_TILE_SIZE = 512; |     private static final GraphicFactory GRAPHIC_FACTORY = AwtGraphicFactory.INSTANCE; | ||||||
| 
 | 
 | ||||||
|     // Tile size. |     // Default tile size. | ||||||
|     int tileSize; |     private static final int DEFAULT_TILE_SIZE = 512; | ||||||
| 
 | 
 | ||||||
|     ArrayList<Paint> extraLayers; |     // List of listeners. | ||||||
| 
 |     private ArrayList<DrawingClickListener> drawingClickListeners = new ArrayList<>(); | ||||||
|     public MapViewDrawing() { | 
 | ||||||
|         getMapScaleBar().setVisible(true); |     // Tile size. | ||||||
|         this.tileSize = DEFAULT_TILE_SIZE; |     int tileSize; | ||||||
|         DisplayModel model = getModel().displayModel; | 
 | ||||||
|         model.setFixedTileSize(tileSize); |     // Extra layers... | ||||||
|         // model.setBackgroundColor(convertColor(Color.WHITE)); |     private static class FixedStrokeWidthLayer { | ||||||
| 
 |         public Paint paint; | ||||||
|         extraLayers = new ArrayList<Paint>(); |         public int width; | ||||||
|         addMouseWheelListener(new MouseAdapter() { | 
 | ||||||
|             @Override |         public FixedStrokeWidthLayer(Paint paint, int width) { | ||||||
|             public void mouseWheelMoved(MouseWheelEvent e) { |             this.paint = paint; | ||||||
|                 byte zoomLevelDiff = (byte) -e.getWheelRotation(); |             this.width = width; | ||||||
|                 for (Paint p: extraLayers) { |         } | ||||||
|                     p.setStrokeWidth(p.getStrokeWidth() + zoomLevelDiff); | 
 | ||||||
|                 } |     }; | ||||||
|             } | 
 | ||||||
|         }); |     ArrayList<FixedStrokeWidthLayer> extraLayers = new ArrayList<>(); | ||||||
|     } | 
 | ||||||
| 
 |     public MapViewDrawing() { | ||||||
|     protected int convertColor(Color color) { |         getMapScaleBar().setVisible(true); | ||||||
|         return GRAPHIC_FACTORY.createColor(color.getAlpha(), color.getRed(), color.getGreen(), |         this.tileSize = DEFAULT_TILE_SIZE; | ||||||
|                 color.getBlue()); |         DisplayModel model = getModel().displayModel; | ||||||
|     } |         model.setFixedTileSize(tileSize); | ||||||
| 
 | 
 | ||||||
|     private int getStrokeWidth(int width) { |         addMouseWheelListener(new MouseAdapter() { | ||||||
|         byte zoomLevel = getModel().mapViewPosition.getZoomLevel(); | 
 | ||||||
|         return width * (2 - (8 - zoomLevel)); |             @Override | ||||||
|     } |             public void mouseWheelMoved(MouseWheelEvent e) { | ||||||
| 
 |                 for (FixedStrokeWidthLayer f: extraLayers) { | ||||||
|     private Paint createPaintStroke(int width, Color color) { |                     f.paint.setStrokeWidth(getStrokeWidth(f.width)); | ||||||
|         Paint paintStroke = AwtGraphicFactory.INSTANCE.createPaint(); |                 } | ||||||
|         paintStroke.setStyle(Style.STROKE); |             } | ||||||
|         if (width != 0) { |         }); | ||||||
|             paintStroke.setStrokeWidth(getStrokeWidth(width)); | 
 | ||||||
|         } |     } | ||||||
|         if (color != null) { | 
 | ||||||
|             paintStroke.setColor(convertColor(color)); |     /** | ||||||
|         } |      * @param color | ||||||
|         return paintStroke; |      * @return | ||||||
|     } |      */ | ||||||
| 
 |     protected int convertColor(Color color) { | ||||||
|     /** |         return GRAPHIC_FACTORY.createColor(color.getAlpha(), color.getRed(), color.getGreen(), color.getBlue()); | ||||||
|      *  |     } | ||||||
|      * @param color | 
 | ||||||
|      * @return |     /** | ||||||
|      */ |      * @param width | ||||||
|     private File getMapsforgeFileFromGraph(Graph graph) { |      * @return | ||||||
|         // TODO: Find a way to change this... |      */ | ||||||
|         Map<Integer, String> idToNames = new HashMap<Integer, String>(); |     private int getStrokeWidth(int width) { | ||||||
|         idToNames.put(0x100, "insa"); |         byte zoomLevel = getModel().mapViewPosition.getZoomLevel(); | ||||||
|         idToNames.put(0x110, "paris"); |         int mul = 2; | ||||||
|         idToNames.put(0x200, "mayotte"); |         if (zoomLevel < 8) { | ||||||
|         idToNames.put(0x250, "newzealand"); |             mul = 1; | ||||||
|         idToNames.put(0x300, "reunion"); |         } | ||||||
|         idToNames.put(0x400, "midip"); |         else { | ||||||
|         idToNames.put(0x410, "morbihan"); |             mul += 2 * (zoomLevel - 8) / 3; | ||||||
| 
 |         } | ||||||
|         File file = null; |         return width * mul; | ||||||
|         if (idToNames.containsKey(graph.getMapId())) { |     } | ||||||
|             file = new File("Maps/" + idToNames.get(graph.getMapId()) + ".mapfg"); | 
 | ||||||
|             if (!file.exists()) { |     /** | ||||||
|                 file = new File("Maps/new/" + idToNames.get(graph.getMapId()) + ".mapfg"); |      * @param width | ||||||
|             } |      * @param color | ||||||
|         } |      * @return | ||||||
| 
 |      */ | ||||||
|         if (file == null || !file.exists()) { |     private Paint createPaintStroke(int width, Color color) { | ||||||
|             JFileChooser fileChooser = new JFileChooser("Maps/"); |         Paint paintStroke = AwtGraphicFactory.INSTANCE.createPaint(); | ||||||
|             fileChooser.setFileFilter(new FileNameExtensionFilter("mapsforge files", "" + "mapfg")); |         paintStroke.setStyle(Style.STROKE); | ||||||
|             if (fileChooser.showOpenDialog(this.getParent()) == JFileChooser.APPROVE_OPTION) { |         if (width != 0) { | ||||||
|                 file = fileChooser.getSelectedFile(); |             paintStroke.setStrokeWidth(getStrokeWidth(width)); | ||||||
|             } |         } | ||||||
|         } |         if (color != null) { | ||||||
| 
 |             paintStroke.setColor(convertColor(color)); | ||||||
|         return file; |         } | ||||||
|     } |         return paintStroke; | ||||||
| 
 |     } | ||||||
|     protected LatLong convertPoint(Point point) { | 
 | ||||||
|         return new LatLong(point.getLatitude(), point.getLongitude()); |     /** | ||||||
|     } |      *  | ||||||
| 
 |      * @param color | ||||||
|     private static TileRendererLayer createTileRendererLayer(TileCache tileCache, |      * @return | ||||||
|             MapDataStore mapDataStore, MapViewPosition mapViewPosition, |      */ | ||||||
|             HillsRenderConfig hillsRenderConfig) { |     private File getMapsforgeFileFromGraph(Graph graph) { | ||||||
|         TileRendererLayer tileRendererLayer = new TileRendererLayer(tileCache, mapDataStore, |         // TODO: Find a way to change this... | ||||||
|                 mapViewPosition, false, true, false, GRAPHIC_FACTORY, hillsRenderConfig) { |         Map<Integer, String> idToNames = new HashMap<Integer, String>(); | ||||||
|             @Override |         idToNames.put(0x100, "insa"); | ||||||
|             public boolean onTap(LatLong tapLatLong, org.mapsforge.core.model.Point layerXY, |         idToNames.put(0x110, "paris"); | ||||||
|                     org.mapsforge.core.model.Point tapXY) { |         idToNames.put(0x200, "mayotte"); | ||||||
|                 System.out.println("Tap on: " + tapLatLong); |         idToNames.put(0x250, "newzealand"); | ||||||
|                 return true; |         idToNames.put(0x300, "reunion"); | ||||||
|             } |         idToNames.put(0x400, "midip"); | ||||||
|         }; |         idToNames.put(0x410, "morbihan"); | ||||||
|         XmlRenderTheme renderTheme = InternalRenderTheme.DEFAULT; | 
 | ||||||
|         tileRendererLayer.setXmlRenderTheme(renderTheme); |         File file = null; | ||||||
|         return tileRendererLayer; |         if (idToNames.containsKey(graph.getMapId())) { | ||||||
|     } |             file = new File("Maps/" + idToNames.get(graph.getMapId()) + ".mapfg"); | ||||||
| 
 |             if (!file.exists()) { | ||||||
|     @Override |                 file = new File("Maps/new/" + idToNames.get(graph.getMapId()) + ".mapfg"); | ||||||
|     public void clear() { |             } | ||||||
|         getLayerManager().getLayers().clear(); |         } | ||||||
|         extraLayers.clear(); | 
 | ||||||
|         repaint(); |         if (file == null || !file.exists()) { | ||||||
|     } |             JFileChooser fileChooser = new JFileChooser("Maps/"); | ||||||
| 
 |             fileChooser.setFileFilter(new FileNameExtensionFilter("mapsforge files", "" + "mapfg")); | ||||||
|     @Override |             if (fileChooser.showOpenDialog(this.getParent()) == JFileChooser.APPROVE_OPTION) { | ||||||
|     public void drawLine(Point from, Point to) { |                 file = fileChooser.getSelectedFile(); | ||||||
|         drawLine(from, to, 0, null); |             } | ||||||
|     } |         } | ||||||
| 
 | 
 | ||||||
|     @Override |         return file; | ||||||
|     public void drawLine(Point from, Point to, int width) { |     } | ||||||
|         drawLine(from, to, width, null); | 
 | ||||||
|     } |     protected LatLong convertPoint(Point point) { | ||||||
| 
 |         return new LatLong(point.getLatitude(), point.getLongitude()); | ||||||
|     @Override |     } | ||||||
|     public void drawLine(Point from, Point to, int width, Color color) { | 
 | ||||||
|         Paint paintStroke = createPaintStroke(width, color); |     private TileRendererLayer createTileRendererLayer(TileCache tileCache, MapDataStore mapDataStore, | ||||||
|         Polyline line = new Polyline(paintStroke, AwtGraphicFactory.INSTANCE); |             MapViewPosition mapViewPosition, HillsRenderConfig hillsRenderConfig) { | ||||||
|         line.getLatLongs().add(convertPoint(from)); |         TileRendererLayer tileRendererLayer = new TileRendererLayer(tileCache, mapDataStore, mapViewPosition, false, | ||||||
|         line.getLatLongs().add(convertPoint(to)); |                 true, false, GRAPHIC_FACTORY, hillsRenderConfig) { | ||||||
|         getLayerManager().getLayers().add(line); |             @Override | ||||||
|     } |             public boolean onTap(LatLong tapLatLong, org.mapsforge.core.model.Point layerXY, | ||||||
| 
 |                     org.mapsforge.core.model.Point tapXY) { | ||||||
|     @Override |                 System.out.println("Tap on: " + tapLatLong); | ||||||
|     public void drawMarker(Point point) { |                 Point pt = new Point(tapLatLong.getLongitude(), tapLatLong.getLatitude()); | ||||||
|         drawMarker(point, null); |                 for (DrawingClickListener listener: MapViewDrawing.this.drawingClickListeners) { | ||||||
|     } |                     listener.mouseClicked(pt); | ||||||
| 
 |                 } | ||||||
|     @Override |                 return true; | ||||||
|     public void drawMarker(Point point, Color color) { |             } | ||||||
|         Marker marker = new Marker(convertPoint(point), GRAPHIC_FACTORY.createBitmap(10, 20), 1, 2); |         }; | ||||||
|         getLayerManager().getLayers().add(marker); |         XmlRenderTheme renderTheme = null; | ||||||
|     } |         try { | ||||||
| 
 |             renderTheme = new ExternalRenderTheme("resources/assets/custom-theme.xml"); | ||||||
|     @Override |         } | ||||||
|     public void drawPoint(Point point, int width, Color color) { |         catch (FileNotFoundException e) { | ||||||
|         // TODO: Maybe do something? |             e.printStackTrace(); | ||||||
|     } |         } | ||||||
| 
 |         tileRendererLayer.setXmlRenderTheme(renderTheme); | ||||||
|     @Override |         return tileRendererLayer; | ||||||
|     public void drawGraph(Graph graph, GraphPalette palette) { |     } | ||||||
| 
 | 
 | ||||||
|         File graphFile = getMapsforgeFileFromGraph(graph); |     @Override | ||||||
| 
 |     public void addDrawingClickListener(DrawingClickListener listener) { | ||||||
|         // Tile cache |         this.drawingClickListeners.add(listener); | ||||||
|         TileCache tileCache = AwtUtil.createTileCache(tileSize, |     } | ||||||
|                 getModel().frameBufferModel.getOverdrawFactor(), 1024, | 
 | ||||||
|                 new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString())); |     @Override | ||||||
| 
 |     public void removeDrawingClickListener(DrawingClickListener listener) { | ||||||
|         // Layers |         this.drawingClickListeners.remove(listener); | ||||||
|         Layers layers = getLayerManager().getLayers(); |     } | ||||||
| 
 | 
 | ||||||
|         MapDataStore mapDataStore = new MapFile(graphFile); |     @Override | ||||||
|         TileRendererLayer tileRendererLayer = createTileRendererLayer(tileCache, mapDataStore, |     public void clear() { | ||||||
|                 getModel().mapViewPosition, null); |         getLayerManager().getLayers().clear(); | ||||||
|         layers.add(tileRendererLayer); |         extraLayers.clear(); | ||||||
|         BoundingBox boundingBox = mapDataStore.boundingBox(); |         repaint(); | ||||||
| 
 |     } | ||||||
|         final Model model = getModel(); | 
 | ||||||
|         if (model.mapViewPosition.getZoomLevel() == 0 |     @Override | ||||||
|                 || !boundingBox.contains(model.mapViewPosition.getCenter())) { |     public void drawLine(Point from, Point to) { | ||||||
|             byte zoomLevel = LatLongUtils.zoomForBounds(model.mapViewDimension.getDimension(), |         drawLine(from, to, 0, null); | ||||||
|                     boundingBox, model.displayModel.getTileSize()); |     } | ||||||
|             model.mapViewPosition | 
 | ||||||
|                     .setMapPosition(new MapPosition(boundingBox.getCenterPoint(), zoomLevel)); |     @Override | ||||||
|             model.mapViewPosition.setZoomLevelMin(zoomLevel); |     public void drawLine(Point from, Point to, int width) { | ||||||
|         } |         drawLine(from, to, width, null); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
|     public void drawGraph(Graph graph) { |     public void drawLine(Point from, Point to, int width, Color color) { | ||||||
|         drawGraph(graph, null); |         Paint paintStroke = createPaintStroke(width, color); | ||||||
|     } |         Polyline line = new Polyline(paintStroke, AwtGraphicFactory.INSTANCE); | ||||||
| 
 |         line.getLatLongs().add(convertPoint(from)); | ||||||
|     @Override |         line.getLatLongs().add(convertPoint(to)); | ||||||
|     public void drawPath(Path path, Color color, boolean markers) { |         getLayerManager().getLayers().add(line); | ||||||
|         Paint paintStroke = createPaintStroke(1, DEFAULT_PATH_COLOR); |     } | ||||||
|         Polyline line = new Polyline(paintStroke, AwtGraphicFactory.INSTANCE); | 
 | ||||||
|         for (Arc arc: path.getArcs()) { |     @Override | ||||||
|             ArrayList<Point> points = arc.getPoints(); |     public void drawMarker(Point point) { | ||||||
|             for (int i = 0; i < points.size(); ++i) { |         drawMarker(point, Color.GREEN); | ||||||
|                 line.getLatLongs().add( |     } | ||||||
|                         new LatLong(points.get(i).getLatitude(), points.get(i).getLongitude())); | 
 | ||||||
|             } |     @Override | ||||||
|         } |     public void drawMarker(Point point, Color color) { | ||||||
|         getLayerManager().getLayers().add(line); |         Bitmap bitmap = MarkerUtils.getMarkerForColor(color); | ||||||
|         extraLayers.add(paintStroke); |         Marker marker = new Marker(convertPoint(point), bitmap, 0, -bitmap.getHeight() / 2); | ||||||
|         if (markers) { |         getLayerManager().getLayers().add(marker); | ||||||
|             drawMarker(path.getOrigin().getPoint()); |     } | ||||||
|             drawMarker(path.getDestination().getPoint()); | 
 | ||||||
|         } |     @Override | ||||||
|     } |     public void drawPoint(Point point, int width, Color color) { | ||||||
| 
 |         // TODO: | ||||||
|     @Override |     } | ||||||
|     public void drawPath(Path path, Color color) { | 
 | ||||||
|         drawPath(path, color, true); |     @Override | ||||||
|     } |     public void drawGraph(Graph graph, GraphPalette palette) { | ||||||
| 
 | 
 | ||||||
|     @Override |         File graphFile = getMapsforgeFileFromGraph(graph); | ||||||
|     public void drawPath(Path path) { | 
 | ||||||
|         drawPath(path, DEFAULT_PATH_COLOR, true); |         // Tile cache | ||||||
|     } |         TileCache tileCache = AwtUtil.createTileCache(tileSize, getModel().frameBufferModel.getOverdrawFactor(), 1024, | ||||||
| 
 |                 new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString())); | ||||||
|     @Override | 
 | ||||||
|     public void drawPath(Path path, boolean markers) { |         // Layers | ||||||
|         drawPath(path, DEFAULT_PATH_COLOR, markers); |         Layers layers = getLayerManager().getLayers(); | ||||||
|     } | 
 | ||||||
| 
 |         MapDataStore mapDataStore = new MapFile(graphFile); | ||||||
| } |         TileRendererLayer tileRendererLayer = createTileRendererLayer(tileCache, mapDataStore, | ||||||
|  |                 getModel().mapViewPosition, null); | ||||||
|  |         layers.add(tileRendererLayer); | ||||||
|  |         BoundingBox boundingBox = mapDataStore.boundingBox(); | ||||||
|  | 
 | ||||||
|  |         final Model model = getModel(); | ||||||
|  |         if (model.mapViewPosition.getZoomLevel() == 0 || !boundingBox.contains(model.mapViewPosition.getCenter())) { | ||||||
|  |             byte zoomLevel = LatLongUtils.zoomForBounds(model.mapViewDimension.getDimension(), boundingBox, | ||||||
|  |                     model.displayModel.getTileSize()); | ||||||
|  |             model.mapViewPosition.setMapPosition(new MapPosition(boundingBox.getCenterPoint(), zoomLevel)); | ||||||
|  |             model.mapViewPosition.setZoomLevelMin(zoomLevel); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void drawGraph(Graph graph) { | ||||||
|  |         drawGraph(graph, null); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void drawPath(Path path, Color color, boolean markers) { | ||||||
|  |         Paint paintStroke = createPaintStroke(1, DEFAULT_PATH_COLOR); | ||||||
|  |         Polyline line = new Polyline(paintStroke, AwtGraphicFactory.INSTANCE); | ||||||
|  |         for (Arc arc: path.getArcs()) { | ||||||
|  |             ArrayList<Point> points = arc.getPoints(); | ||||||
|  |             for (int i = 0; i < points.size(); ++i) { | ||||||
|  |                 line.getLatLongs().add(new LatLong(points.get(i).getLatitude(), points.get(i).getLongitude())); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |         getLayerManager().getLayers().add(line); | ||||||
|  |         extraLayers.add(new FixedStrokeWidthLayer(paintStroke, 1)); | ||||||
|  |         if (markers) { | ||||||
|  |             drawMarker(path.getOrigin().getPoint(), DEFAULT_PATH_COLOR); | ||||||
|  |             drawMarker(path.getDestination().getPoint(), DEFAULT_PATH_COLOR); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void drawPath(Path path, Color color) { | ||||||
|  |         drawPath(path, color, true); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void drawPath(Path path) { | ||||||
|  |         drawPath(path, DEFAULT_PATH_COLOR, true); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void drawPath(Path path, boolean markers) { | ||||||
|  |         drawPath(path, DEFAULT_PATH_COLOR, markers); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
|  |  | ||||||
							
								
								
									
										118
									
								
								src/main/org/insa/drawing/utils/MarkerUtils.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										118
									
								
								src/main/org/insa/drawing/utils/MarkerUtils.java
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,118 @@ | ||||||
|  | package org.insa.drawing.utils; | ||||||
|  | 
 | ||||||
|  | import java.awt.Color; | ||||||
|  | import java.awt.image.BufferedImage; | ||||||
|  | import java.util.HashMap; | ||||||
|  | import java.util.Map; | ||||||
|  | 
 | ||||||
|  | import org.mapsforge.core.graphics.Bitmap; | ||||||
|  | import org.mapsforge.map.awt.graphics.AwtBitmap; | ||||||
|  | 
 | ||||||
|  | public class MarkerUtils { | ||||||
|  | 
 | ||||||
|  |     // Marker | ||||||
|  |     private static Map<Color, Bitmap> MARKER_BITMAPS = new HashMap<Color, Bitmap>(); | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * Return a color mapping with the given color as the main color. | ||||||
|  |      *  | ||||||
|  |      * @param color | ||||||
|  |      * @return | ||||||
|  |      */ | ||||||
|  |     protected static Color[] getColorMapping(Color color) { | ||||||
|  |         return new Color[] { new Color(0, 0, 0, 0), color, Color.BLACK }; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * Create a Bitmap representing a marker of the given color. | ||||||
|  |      *  | ||||||
|  |      * @param color | ||||||
|  |      * @return | ||||||
|  |      */ | ||||||
|  |     public static Bitmap getMarkerForColor(Color color) { | ||||||
|  |         if (MARKER_BITMAPS.containsKey(color)) { | ||||||
|  |             return MARKER_BITMAPS.get(color); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         // create image | ||||||
|  |         BufferedImage image = new BufferedImage(MARKER_MASK[0].length, MARKER_MASK.length, BufferedImage.TYPE_INT_ARGB); | ||||||
|  | 
 | ||||||
|  |         Color[] map = getColorMapping(color); | ||||||
|  |         for (int i = 0; i < image.getHeight(); ++i) { | ||||||
|  |             for (int j = 0; j < image.getWidth(); ++j) { | ||||||
|  |                 image.setRGB(j, i, map[MARKER_MASK[i][j]].getRGB()); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         // Create Bitmap, add it to map and return it. | ||||||
|  |         Bitmap bitmap = new AwtBitmap(image); | ||||||
|  |         MARKER_BITMAPS.put(color, bitmap); | ||||||
|  |         return bitmap; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     // Mask | ||||||
|  |     private static byte[][] MARKER_MASK = new byte[][] { | ||||||
|  |         // @formatter:off | ||||||
|  | 		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, | ||||||
|  | 		{0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0}, | ||||||
|  | 		{0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0}, | ||||||
|  | 		{0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0}, | ||||||
|  | 		{0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0}, | ||||||
|  | 		{0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0}, | ||||||
|  | 		{0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0}, | ||||||
|  | 		{0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0}, | ||||||
|  | 		{0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0}, | ||||||
|  | 		{0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0}, | ||||||
|  | 		{0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0}, | ||||||
|  | 		{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0}, | ||||||
|  | 		{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0}, | ||||||
|  | 		{0,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,0}, | ||||||
|  | 		{1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1}, | ||||||
|  | 		{1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1}, | ||||||
|  | 		{1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1}, | ||||||
|  | 		{1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1}, | ||||||
|  | 		{1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1}, | ||||||
|  | 		{1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1}, | ||||||
|  | 		{1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1}, | ||||||
|  | 		{0,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,0}, | ||||||
|  | 		{0,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,0}, | ||||||
|  | 		{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0}, | ||||||
|  | 		{0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0}, | ||||||
|  | 		{0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0}, | ||||||
|  | 		{0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0}, | ||||||
|  | 		{0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0}, | ||||||
|  | 		{0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0}, | ||||||
|  | 		{0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0}, | ||||||
|  | 		{0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0}, | ||||||
|  | 		{0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0}, | ||||||
|  | 		{0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0}, | ||||||
|  | 		{0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0}, | ||||||
|  | 		{0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0}, | ||||||
|  | 		{0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0}, | ||||||
|  | 		{0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0}, | ||||||
|  | 		{0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0}, | ||||||
|  | 		{0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0}, | ||||||
|  | 		{0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0}, | ||||||
|  | 		{0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0}, | ||||||
|  | 		{0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0}, | ||||||
|  | 		{0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0}, | ||||||
|  | 		{0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0}, | ||||||
|  | 		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, | ||||||
|  | 		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, | ||||||
|  | 		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, | ||||||
|  | 		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, | ||||||
|  | 		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, | ||||||
|  | 		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, | ||||||
|  | 		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, | ||||||
|  | 		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, | ||||||
|  | 		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, | ||||||
|  | 		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, | ||||||
|  | 		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, | ||||||
|  | 		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, | ||||||
|  | 		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, | ||||||
|  | 		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, | ||||||
|  | 		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, | ||||||
|  | 		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, | ||||||
|  | 		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} | ||||||
|  | 	};	 | ||||||
|  | } | ||||||
		Loading…
	
		Reference in a new issue