Advertisement
Guest User

showpoints

a guest
Dec 1st, 2015
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.86 KB | None | 0 0
  1.  /**
  2.  * A program that displays a set of points on the screen.
  3.  * Begin with an array of Point objects.  Send that array
  4.  * to a PointDisplay object.  The PointDisplay object will
  5.  * handle all the necessary drawing for you.
  6.  *
  7.  * @author Norm Krumpe
  8.  * @version 2.0
  9.  */
  10. import javax.swing.JFrame;
  11. import java.awt.Point;
  12. import java.util.Scanner;
  13. import java.util.ArrayList;
  14. import java.io.PrintWriter;
  15. import java.io.File;
  16. import java.io.FileNotFoundException;
  17.  
  18. public class PointPlotter  {
  19.   public static void main(String[] args) throws Exception{
  20.    
  21.     // For now, how JFrames work is unimportant. Our
  22.     // focus is on using a PointDisplay object,
  23.     // which requires an array of Point objects in order
  24.     // to work.
  25.     JFrame frame = new JFrame("Show Some Points");
  26.     frame.setBounds(300, 300, 615, 650);
  27.     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  28.  
  29.     // PointDisplay objects "consume" arrays of Point objects.
  30.     // In other words, if you have an array of Point objects,
  31.     // and pass that array to PointDisplay, it will draw
  32.     // those points for you.
  33.     boolean connect = false;
  34.     int pointDiameter = 10;
  35.    
  36.     ArrayList<Point> myPoints = makePoints();
  37.     cleanData();
  38.    
  39.    
  40.  
  41.  // DON'T CHANGE THE REST OF THE CODE IN THIS METHOD:
  42.     PointDisplay display = new PointDisplay(myPoints, pointDiameter, connect);
  43.     frame.add(display);
  44.     frame.setVisible(true);
  45.  
  46.   }
  47.  
  48. //  // Returns an array list of 3 points.  Just so you can
  49. //  // see how a method can return array lists:
  50. //  public static Point[] easyArray() {
  51. //
  52. // Point[] result = {
  53. //  new Point(50, 60),
  54. //  new Point(400, 300),
  55. //  new Point(90, 174)
  56. // };
  57. //
  58. // return result;
  59. //  }
  60.  
  61.   public static void cleanData() throws FileNotFoundException{
  62.     PrintWriter cleanData  = new PrintWriter (new File ("cleanData.txt"));
  63.     Scanner input = new Scanner (new File ("messyData.txt"));
  64.    
  65.     String word;
  66.     int newInt;
  67.     int coordinateCount = 0;
  68.     double oldDouble;
  69.    
  70.     while (input.hasNext()) {
  71.     if (input.hasNextDouble()) {
  72.       oldDouble = input.nextDouble();
  73.       newInt = (int)(oldDouble);
  74.      
  75.       if (coordinateCount == 0) {
  76.         cleanData.print(newInt + " ");
  77.         coordinateCount++;
  78.       }
  79.       else {
  80.         cleanData.println(newInt);
  81.         coordinateCount=0;
  82.       }
  83.     }
  84.     else {
  85.        word = input.next();
  86.     }
  87.     }
  88.     cleanData.close();
  89.     input.close();
  90.   }
  91.  
  92.   public static ArrayList<Point> makePoints() throws Exception {
  93.      Scanner in = new Scanner(new File("cleanData.txt"));
  94.      
  95.      ArrayList<Point> result = new ArrayList<Point>();
  96.      
  97.      while (in.hasNextInt() == true) {
  98.         int x = in.nextInt();
  99.         int y = in.nextInt();
  100.        
  101.         result.add(new Point(x, y));
  102.      }
  103.      
  104.      return result;
  105.   }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement