Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.29 KB | None | 0 0
  1. package FIT_8202_Fedotov_Lines;
  2.  
  3. import java.awt.Color;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.PrintStream;
  8. import java.util.HashMap;
  9. import java.util.LinkedList;
  10. import java.util.Scanner;
  11.  
  12. /**
  13.  * Utilities class for writing and reading Lines format files:
  14.  *
  15.  * <pre>
  16.  * n - number of polylines
  17.  * m<sub>1</sub> - points number in first line
  18.  * t<sub>1</sub> - type of first line (1 - solid, 2 - dashed, 3 - dash-dotted)
  19.  * r<sub>1</sub> g<sub>1</sub> b<sub>1</sub> - RGB color of first line
  20.  * x<sup>1</sup><sub>1</sub> y<sup>1</sup><sub>1</sub>
  21.  * ...       X an Y coordinates of points
  22.  * x<sup>1</sup><sub>m<sub>1</sub></sub> y<sup>1</sup><sub>m<sub>1</sub></sub>
  23.  * </pre>
  24.  *
  25.  * @author Victor P. Fedotov
  26.  */
  27. public class Formatter
  28. {
  29.     private static final HashMap<Integer, String> myTypes = new HashMap<Integer, String>();
  30.     static
  31.     {
  32.         for (String name : Lines.TYPES.keySet())
  33.         {
  34.             myTypes.put(Lines.TYPES.get(name).number, name);
  35.         }
  36.     }
  37.    
  38.     /**
  39.      * Writes polylines data in file with Lines format
  40.      * @param polylines List of polylines
  41.      * @param file Output file
  42.      */
  43.     static public void write(LinkedList<Polyline> polylines, File file)
  44.     {
  45.         PrintStream out;
  46.         try
  47.         {
  48.             out = new PrintStream(new FileOutputStream(file));
  49.         }
  50.         catch (FileNotFoundException e)
  51.         {
  52.             e.printStackTrace();
  53.             return;
  54.         }
  55.         out.println(polylines.size());
  56.         for (Polyline polyline : polylines)
  57.         {
  58.             out.println(polyline.getPoints().size());
  59.             out.println(Lines.TYPES.get(polyline.getType()).number);
  60.             out.println(polyline.getThickness());
  61.             Color color = polyline.getColor();
  62.             out.println(color.getRed() + " " + color.getGreen() + " " + color.getBlue());
  63.             for (Point point : polyline.getPoints())
  64.             {
  65.                 out.println(point.x + " " + point.y);
  66.             }
  67.         }
  68.     }
  69.    
  70.     /**
  71.      * Reads and return polylines from file in Lines format
  72.      * @param file
  73.      * @return
  74.      */
  75.     static public LinkedList<Polyline> read(File file)
  76.     {
  77.         LinkedList<Polyline> polylines = new LinkedList<Polyline>();
  78.         Scanner in;
  79.         try
  80.         {
  81.             in = new Scanner(file);
  82.         }
  83.         catch (FileNotFoundException e)
  84.         {
  85.             e.printStackTrace();
  86.             return polylines;
  87.         }
  88.         int p = next(in);
  89.         for (int i = 0; i < p; i++)
  90.         {
  91.             int points    = next(in);
  92.             int _type     = next(in);
  93.             String type   = myTypes.get(_type);
  94.             int thickness = next(in);
  95.             int red       = next(in);
  96.             int green     = next(in);
  97.             int blue      = next(in);
  98.             Polyline polyline = new Polyline(type, thickness, new Color(red, green, blue));
  99.             for (int j = 0; j < points; j++)
  100.             {
  101.                 int x = next(in);
  102.                 int y = next(in);
  103.                 polyline.addPoint(new Point(x, y));
  104.             }
  105.             polylines.add(polyline);
  106.         }
  107.         return polylines;
  108.     }
  109.    
  110.     /**
  111.      * Reads and returns next integer value with scanner
  112.      * If the next token is not integer, then everything after it in the same line ignores and returns next integer
  113.      * @param s
  114.      * @return
  115.      */
  116.     static private int next(Scanner s)
  117.     {
  118.         for (;;)
  119.         {
  120.             String a = s.next();
  121.             if (a.indexOf("//") != -1)
  122.             {
  123.                 s.nextLine();
  124.                 a = a.substring(0, a.indexOf("//"));
  125.             }
  126.             int x;
  127.             try
  128.             {
  129.                 x = Integer.parseInt(a);
  130.             }
  131.             catch (NumberFormatException e)
  132.             {
  133.                 continue;
  134.             }
  135.             return x;
  136.         }
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement