Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 5.72 KB | None | 0 0
  1. import java.awt.Dimension;
  2. import java.awt.Graphics;
  3. import java.awt.Point;
  4. import java.awt.event.MouseEvent;
  5. import java.io.*;
  6. import java.util.*;
  7.  
  8. //import GUI.Move;
  9.  
  10. public class Mapper extends GUI {
  11.    
  12.     List<Segment> outSegs = new ArrayList<Segment>();
  13.     List<Segment> inSegs = new ArrayList<Segment>();
  14.     List<Node> nodes = new ArrayList<Node>();
  15.     List<Segment> segments = new ArrayList<Segment>();
  16.     Map<Integer, Road> roads = new HashMap<>();
  17.  
  18.     Location origin = Location.newFromLatLon(Location.CENTRE_LAT, Location.CENTRE_LON-1);
  19.     double lat_shift = 5;
  20.     double long_shift = 5;
  21.     double scale = 5;
  22.    
  23.     public Mapper() {
  24.         // TODO Auto-generated constructor stub
  25.     }
  26.  
  27.     @Override
  28.     protected void redraw(Graphics g) {
  29.         // draw nodes
  30.         for (Node n : nodes) {
  31.             Location loc = Location.newFromLatLon(n.latitude, n.longitude);
  32.             Point point = loc.asPoint(origin, scale);
  33.             g.fillOval(point.x, point.y, 1, 1);
  34.         }
  35.     }
  36.  
  37.     @Override
  38.     protected void onClick(MouseEvent e) {
  39.         System.out.println(e.getX());
  40.         System.out.println(e.getY());
  41.         System.out.println("onClick");
  42.     }
  43.  
  44.     @Override
  45.     protected void onSearch() {
  46.         String search = getSearchBox().getText();
  47.         System.out.print(search);
  48.     }
  49.  
  50.     @Override
  51.     protected void onMove(Move m) {
  52.         if ( m == Move.EAST ) {
  53.             Location neworigin = origin.moveBy(lat_shift, 0);
  54.             origin = neworigin;
  55.             getTextOutputArea().setText("MOVE EAST");
  56.         } else if ( m == Move.WEST ) {
  57.             Location neworigin = origin.moveBy(-lat_shift, 0);
  58.             origin = neworigin;
  59.             getTextOutputArea().setText("MOVE WEST");
  60.         } else if ( m == Move.NORTH ) {
  61.             Location neworigin = origin.moveBy(0, -long_shift);
  62.             origin = neworigin;
  63.             getTextOutputArea().setText("MOVE NORTH");
  64.         } else if ( m == Move.SOUTH ) {
  65.             Location neworigin = origin.moveBy(0, long_shift);
  66.             origin = neworigin;
  67.             getTextOutputArea().setText("MOVE SOUTH");
  68.         } else if ( m == Move.ZOOM_IN ) {
  69.             Dimension area = getDrawingAreaDimension();
  70.             double oldscale = scale;
  71.             scale += 0.5;
  72. //          double midx = area.getWidth() / 2;
  73. //          double midy = area.getHeight() / 2;
  74. //          Location midpoint = new Location(midx, midy);
  75. //          double distance = origin.distance(midpoint);
  76.             double dx = ( area.getWidth() - ( area.getWidth() * ( oldscale / scale ) ) ) / 2;
  77.             double dy = ( area.getHeight() - ( area.getHeight() * ( oldscale / scale ) ) ) / 2;
  78.             System.out.println(dx);
  79.             System.out.println(dy);
  80. //          Point point = new Point(area.height, area.width);
  81.             Location neworigin = origin.moveBy(origin.x + dx, origin.y + dy);
  82.             origin = neworigin;
  83.             getTextOutputArea().setText("ZOOM IN");
  84.         } else if ( m == Move.ZOOM_OUT ) {
  85.             scale--;           
  86.             Dimension area = getDrawingAreaDimension();
  87.             double dx = ( area.width + ( area.width * scale ) ) / 2;
  88.             double dy = ( area.height + ( area.height * scale ) ) / 2;
  89.             Location neworigin = origin.moveBy(origin.x - dx, origin.y - dy);
  90.             origin = neworigin;
  91.             getTextOutputArea().setText("ZOOM OUT");
  92.         }
  93.     }
  94.  
  95.     @Override
  96.     protected void onLoad(File nodesfile, File roadsfile, File segmentsfile, File polygonsfile) {
  97.         BufferedReader nodesIn = null;
  98.         //BufferedReader roadsIn = null;
  99.         //BufferedReader segmentsIn = null;
  100.        
  101.         // Get nodes (intersections)
  102.         try {
  103.             nodesIn = new BufferedReader(new FileReader(nodesfile));
  104.             String ln = nodesIn.readLine();
  105.             int nodeID;
  106.             double latitude, longitude;
  107.             while (ln != null) {
  108.                 String[] splitln = ln.split("\\t"); // split line at tabs
  109.                 nodeID = Integer.parseInt(splitln[0]);
  110.                 latitude = Double.parseDouble(splitln[1]);
  111.                 longitude = Double.parseDouble(splitln[2]);
  112.                 Node n = new Node(nodeID, latitude, longitude);
  113.                 nodes.add(n);
  114.                 ln = nodesIn.readLine();
  115.             }
  116.         } catch (IOException e) {
  117.             e.printStackTrace();
  118.         } finally {
  119.             try {
  120.                 if ( nodesIn != null) {
  121.                     nodesIn.close();
  122.                 }
  123.             } catch (IOException s) {
  124.                 System.out.print(s);
  125.             }
  126.         }
  127.        
  128. //      // Get roads
  129. //      try {
  130. //          roadsIn = new BufferedReader(new FileReader(roadsfile));
  131. //          String ln = roadsIn.readLine();
  132. //          int nodeID;
  133. //          double latitude, longitude;
  134. //          while (ln != null) {
  135. //              String[] splitln = ln.split("\\t"); // split line at tabs
  136. //              nodeID = Integer.parseInt(splitln[0]);
  137. //              latitude = Double.parseDouble(splitln[1]);
  138. //              longitude = Double.parseDouble(splitln[2]);
  139. //              Node n = new Node(nodeID, latitude, longitude);
  140. //              nodes.add(n);
  141. //              ln = roadsIn.readLine();
  142. //          }
  143. //      } catch (IOException e) {
  144. //          e.printStackTrace();
  145. //      } finally {
  146. //          try {
  147. //              if ( roadsIn != null) {
  148. //                  roadsIn.close();
  149. //              }
  150. //          } catch (IOException s) {
  151. //              System.out.print(s);
  152. //          }
  153. //      }
  154. //     
  155.         // Get segments
  156. //      try {
  157. //          segmentsIn = new BufferedReader(new FileReader(segmentsfile));
  158. //          segmentsIn.readLine(); // headings line
  159. //          String ln = segmentsIn.readLine();
  160. //          int roadID, nodeID1, nodeID2;
  161. //          double length;
  162. //          double[][] coords;
  163. //          while (ln != null) {
  164. //              String[] splitln = ln.split("\\t"); // split line at tabs
  165. //              roadID = Integer.parseInt(splitln[0]);
  166. //              length = Double.parseDouble(splitln[1]);
  167. //              nodeID1 = Integer.parseInt(splitln[2]);
  168. //              nodeID2 = Integer.parseInt(splitln[3]);
  169. //              for (int i=4; i<splitln.length; i+2){
  170. //                  double latitude = Double.parseDouble(splitln[4]);
  171. //                  double long
  172. //              }
  173. //              Node n = new Node(nodeID, latitude, longitude);
  174. //              nodes.add(n);
  175. //              ln = segmentsIn.readLine();
  176. //          }
  177. //      } catch (IOException e) {
  178. //          e.printStackTrace();
  179. //      } finally {
  180. //          try {
  181. //              if ( segmentsIn != null) {
  182. //                  segmentsIn.close();
  183. //              }
  184. //          } catch (IOException s) {
  185. //              System.out.print(s);
  186. //          }
  187. //      }
  188.     }
  189.    
  190.     public static void main(String[] args) {
  191.         new Mapper();
  192.     }
  193.  
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement