import edu.princeton.cs.algs4.StdDraw; import javax.swing.*; import java.awt.*; import java.io.File; import java.util.HashMap; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.Scanner; import static edu.princeton.cs.algs4.StdDraw.*; public class CampusMap { public static final int MINIMUM_DISTANCE_TO_SHOW = 6; public static final Color BG_COLOR = new Color(0x333338); public static final Color PW_COLOR = new Color(0x3498DB); public static final Color POI_COLOR = new Color(0xE74C3C); public static final Color LABEL_COLOR = new Color(0xECF0F1); public static final Color FEET_COLOR = new Color(0xBBBBBB); public static final Color INTERSECTION_LABEL_COLOR = new Color(0x99F0F9); private static boolean onScreen = false; public boolean fileParsingOk = true; try{ map.dijkstra(src); String shortest = map.shortestPath(dst); shortest = "This is the shortest path:\n" + shortest; JOptionPane.showMessageDialog(null, shortest); } catch(RuntimeException e){ } // The campus map is made up by an array of interest points public HashMap pointsOfInterest; public CampusMap(String fileWithPoints) { pointsOfInterest = new HashMap(); try { Scanner file = new Scanner(new File(fileWithPoints)); System.out.println("Loading points of interest and intersections..."); while(file.hasNextLine()) { String line = file.nextLine(); if (line.equalsIgnoreCase("[passageways]")) break; PointOfInterest p = parsePoint(line); pointsOfInterest.put(p.getName(), p); } System.out.println("Connecting passageways..."); while (file.hasNextLine()) { String line = file.nextLine(); String[] parts = line.split("--"); String sourceName = parts[0]; String destName = parts[1]; int distance = Integer.parseInt(parts[2]); String direction = parts[3].toUpperCase(); connect(sourceName, destName, distance, Direction.valueOf(direction)); } } catch (Exception e) { fileParsingOk = false; } } public void dijkstras(String source) { PointOfInterest s = pointsOfInterest.get(source); if (s == null) { throw new RuntimeException("USER: SOURCE POINT NOT EXIST."); } PriorityQueue q = new PriorityQueue(); s.setDistance(0); for (PointOfInterest v : s.getPointsOfInterest()) { q.add(v); while (!q.isEmpty()) { PointOfInterest x = q.poll(); for (Passageway p : s.getPassageways()) { PointOfInterest w = p.getDestination(); if (w.getDistance() > x.getDistance() + p.getDistance()) { w.setDistance(x.getDistance() + p.getDistance()); w.setPrevious(x); } } } } } static int currentLine = 0; public static void println(String s) { setPenColor(RED); textLeft(10,680-(10*currentLine++), s); } public void connect(String sourcePointName, String destPointName, int distance, Direction direction) { System.out.println("Connecting " + sourcePointName + "<=>" + destPointName); PointOfInterest sourcePoint = pointsOfInterest.get(sourcePointName); PointOfInterest destPoint = pointsOfInterest.get(destPointName); sourcePoint.addPassageway(destPoint, distance, direction); destPoint.addPassageway(sourcePoint, distance, direction.opposite()); } public static PointOfInterest parsePoint(String fileLine) { try { String[] parts = fileLine.split(" ", 4); String name = parts[3].trim(); double x = Double.parseDouble(parts[1].trim()); double y = Double.parseDouble(parts[2].trim()); boolean isIntersection = parts[0].trim().equals("0"); return new PointOfInterest(name, x, y, isIntersection); } catch (Exception e) { JOptionPane.showMessageDialog(null,"Interest point not in correct format:\n" + fileLine); } return null; } public Passageway parsePassageway(String fileLine) { try { String[] parts = fileLine.split("-", 4); String sourceName = parts[0].trim(); String destName = parts[1].trim(); int distance = Integer.parseInt(parts[2].trim()); String direction = parts[3].trim(); return new Passageway( getPointNamed(sourceName), getPointNamed(destName), distance, Direction.valueOf(direction) ); } catch (Exception e) { JOptionPane.showMessageDialog(null,"Passageway not in correct format:\n" + fileLine); return null; } } public PointOfInterest getPointNamed(String name) { return pointsOfInterest.get(name); } public void show() { if(!onScreen) { setCanvasSize(1000, 700); setXscale(0, 1000); setYscale(0, 700); onScreen = true; } clear(BG_COLOR); setPenColor(WHITE); setFont(new Font("Arial", Font.PLAIN, 10)); for (PointOfInterest p : pointsOfInterest.values()) { showPointOfInterest(p); for (Passageway pw : p.getPassageways()) { showPassageway(pw); } } onScreen = true; } private void showPointOfInterest(PointOfInterest p) { if (p.isIntersection()) { setPenColor(PW_COLOR); filledCircle(p.getScreenX(), p.getScreenY(), 3); text(p.getScreenX(), p.getScreenY() + 10, p.getName()); } else { if (p.hasName()) { setPenColor(POI_COLOR); filledSquare(p.getScreenX(), p.getScreenY(), 3); setPenColor(LABEL_COLOR); text(p.getScreenX(), p.getScreenY() - 10, p.getName()); } } } private void showPassageway(Passageway pw) { setPenColor(PW_COLOR); StdDraw.line( pw.getSource().getScreenX(), pw.getSource().getScreenY(), pw.getDestination().getScreenX(), pw.getDestination().getScreenY() ); if (pw.getDistance() >= MINIMUM_DISTANCE_TO_SHOW) { double midpointX = (pw.getSource().getScreenX() + pw.getDestination().getScreenX()) / 2; double midpointY = (pw.getSource().getScreenY() + pw.getDestination().getScreenY()) / 2; setPenColor(INTERSECTION_LABEL_COLOR); text(midpointX, midpointY, pw.getDistance() + " ft"); } } }