Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package FIT_8202_Fedotov_Lines;
- import java.awt.Color;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.PrintStream;
- import java.util.HashMap;
- import java.util.LinkedList;
- import java.util.Scanner;
- /**
- * Utilities class for writing and reading Lines format files:
- *
- * <pre>
- * n - number of polylines
- * m<sub>1</sub> - points number in first line
- * t<sub>1</sub> - type of first line (1 - solid, 2 - dashed, 3 - dash-dotted)
- * r<sub>1</sub> g<sub>1</sub> b<sub>1</sub> - RGB color of first line
- * x<sup>1</sup><sub>1</sub> y<sup>1</sup><sub>1</sub>
- * ... X an Y coordinates of points
- * x<sup>1</sup><sub>m<sub>1</sub></sub> y<sup>1</sup><sub>m<sub>1</sub></sub>
- * </pre>
- *
- * @author Victor P. Fedotov
- */
- public class Formatter
- {
- private static final HashMap<Integer, String> myTypes = new HashMap<Integer, String>();
- static
- {
- for (String name : Lines.TYPES.keySet())
- {
- myTypes.put(Lines.TYPES.get(name).number, name);
- }
- }
- /**
- * Writes polylines data in file with Lines format
- * @param polylines List of polylines
- * @param file Output file
- */
- static public void write(LinkedList<Polyline> polylines, File file)
- {
- PrintStream out;
- try
- {
- out = new PrintStream(new FileOutputStream(file));
- }
- catch (FileNotFoundException e)
- {
- e.printStackTrace();
- return;
- }
- out.println(polylines.size());
- for (Polyline polyline : polylines)
- {
- out.println(polyline.getPoints().size());
- out.println(Lines.TYPES.get(polyline.getType()).number);
- out.println(polyline.getThickness());
- Color color = polyline.getColor();
- out.println(color.getRed() + " " + color.getGreen() + " " + color.getBlue());
- for (Point point : polyline.getPoints())
- {
- out.println(point.x + " " + point.y);
- }
- }
- }
- /**
- * Reads and return polylines from file in Lines format
- * @param file
- * @return
- */
- static public LinkedList<Polyline> read(File file)
- {
- LinkedList<Polyline> polylines = new LinkedList<Polyline>();
- Scanner in;
- try
- {
- in = new Scanner(file);
- }
- catch (FileNotFoundException e)
- {
- e.printStackTrace();
- return polylines;
- }
- int p = next(in);
- for (int i = 0; i < p; i++)
- {
- int points = next(in);
- int _type = next(in);
- String type = myTypes.get(_type);
- int thickness = next(in);
- int red = next(in);
- int green = next(in);
- int blue = next(in);
- Polyline polyline = new Polyline(type, thickness, new Color(red, green, blue));
- for (int j = 0; j < points; j++)
- {
- int x = next(in);
- int y = next(in);
- polyline.addPoint(new Point(x, y));
- }
- polylines.add(polyline);
- }
- return polylines;
- }
- /**
- * Reads and returns next integer value with scanner
- * If the next token is not integer, then everything after it in the same line ignores and returns next integer
- * @param s
- * @return
- */
- static private int next(Scanner s)
- {
- for (;;)
- {
- String a = s.next();
- if (a.indexOf("//") != -1)
- {
- s.nextLine();
- a = a.substring(0, a.indexOf("//"));
- }
- int x;
- try
- {
- x = Integer.parseInt(a);
- }
- catch (NumberFormatException e)
- {
- continue;
- }
- return x;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement