Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.60 KB | None | 0 0
  1.  
  2. /**
  3.  * California.java
  4.  *
  5.  * Using OpenFile, this class draws an outline of California from data
  6.  * input from californiaborder.txt with StdDraw helping us draw the state.
  7.  * Then takes in input from california.txt to plot out cities, and plots
  8.  * the cities from california.txt as dots.
  9.  *
  10.  * @author Stephanie Wu
  11.  * @version 1.0
  12.  * @since 9/9/2019
  13.  */
  14.  
  15. import java.awt.Color;
  16. import java.awt.Font;
  17. import java.util.Scanner;
  18.  
  19. public class California
  20. {
  21.     /** Creates a California object.
  22.      */
  23.     public California ( )
  24.     { }
  25.  
  26.     /**
  27.      *  The main method.
  28.      */
  29.     public static void main(String[] args)
  30.     {
  31.         California run = new California();
  32.         run.setUpCanvas();
  33.         run.drawBorder();
  34.         run.drawCaliforniaCities();
  35.     }
  36.  
  37.     /**
  38.      *  Sets up the canvas, using methods from StdDraw.  This includes
  39.      *  setting up the canvas size, the horizontal scale (Xscale), and
  40.      *  the vertical scale (Yscale).  We will enable double buffering,
  41.      *  in anticipation of running an animation.
  42.      */
  43.     public void setUpCanvas ( )               //  This method is complete.
  44.     {
  45.         StdDraw.setCanvasSize(620, 700);
  46.         StdDraw.setXscale(-125.0, -114.0);    //  Related to the longitude
  47.         StdDraw.setYscale(32.0, 42.5);        //  Related to the latitude
  48.  
  49.         StdDraw.enableDoubleBuffering();
  50.         StdDraw.setPenColor(StdDraw.DARK_GRAY);
  51.     }
  52.  
  53.     /**
  54.      *  Using OpenFile, this method opens the text file californiaborder.txt.
  55.      *  Each line in the text file is an ordered pair (doubles) that represents
  56.      *  a point on the California border.  These points are read in, using a loop,
  57.      *  and the points are connected to form the border of California.  Don't
  58.      *  forget to close the text file!
  59.      */
  60.     public void drawBorder ( )
  61.     {
  62.         StdDraw.setPenRadius(0.0025);
  63.         Scanner infile = OpenFile.openToRead("californiaborder.txt");
  64.         String temp = null;
  65.  
  66.         temp = infile.nextLine();
  67.         int split = temp.indexOf(",");
  68.         double long1 = Double.parseDouble(temp.substring(0, split));
  69.         double lat1 = Double.parseDouble(temp.substring(split+2));
  70.  
  71.         while(infile.hasNext())
  72.         {
  73.             temp = infile.nextLine();
  74.             split = temp.indexOf(",");
  75.             double long2 = Double.parseDouble(temp.substring(0,split));
  76.             double lat2 = Double.parseDouble(temp.substring(split+2, temp.length()));
  77.  
  78.             StdDraw.line(long1,lat1,long2,lat2);
  79.             StdDraw.show();
  80.             long1 = long2;
  81.             lat1 = lat2;
  82.             StdDraw.pause(50);
  83.  
  84.         }
  85.         infile.close(); //important
  86.     }
  87.  
  88.     /**
  89.      *  Using OpenFile, this method opens the text file california.txt.  Each line
  90.      *  in the text file provides information about a California city.  After the
  91.      *  sixth comma the latitude for the city is provided (a double), and after the
  92.      *  seventh comma the longitude for the city is provided (a double).  These
  93.      *  values should be parsed as doubles, and the city should be represented as a
  94.      *  dot on the map.  Don't forget to close the text file.
  95.      *
  96.      */
  97.     public void drawCaliforniaCities ( )
  98.     {
  99.         StdDraw.setPenColor(StdDraw.DARK_GRAY);
  100.         StdDraw.setPenRadius(0.01);
  101.         Scanner infile = OpenFile.openToRead("california.txt");
  102.         String temp = null;
  103.         int split = 0;
  104.         int ind = 0;
  105.         double x = 0.0;
  106.         double y = 0.0;
  107.         while(infile.hasNext()){
  108.             ind = 0;
  109.             temp = infile.nextLine();
  110.             while(split<6){
  111.                 if(temp.charAt(ind).equals(",")) split++;
  112.                 ind++;
  113.             }
  114.             temp = temp.substring(ind+1, temp.length());
  115.             x = Double.parseDouble(temp.substring(0, temp.indexOf(",")));
  116.             temp = temp.substring(temp.indexOf(",")+1, temp.length());
  117.             y = Double.parseDouble(temp.substring(0, temp.indexOf(",")));
  118.  
  119.             StdDraw.point(x,y);
  120.             StdDraw.show();
  121.         }
  122.  
  123.         StdDraw.pause(30);
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement