Advertisement
danikula

SerializablePath

Aug 27th, 2015
1,142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.90 KB | None | 0 0
  1. public class SerializablePath extends Path implements Serializable {
  2.  
  3.     private List<Action> actions = new LinkedList<>();
  4.  
  5.     private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
  6.         in.defaultReadObject();
  7.  
  8.         for (Action action : actions) {
  9.             action.perform(this);
  10.         }
  11.     }
  12.  
  13.     @Override
  14.     public void lineTo(float x, float y) {
  15.         actions.add(new Line(x, y));
  16.         super.lineTo(x, y);
  17.     }
  18.  
  19.     @Override
  20.     public void moveTo(float x, float y) {
  21.         actions.add(new Move(x, y));
  22.         super.moveTo(x, y);
  23.     }
  24.  
  25.     @Override
  26.     public void quadTo(float x1, float y1, float x2, float y2) {
  27.         actions.add(new Quad(x1, y1, x2, y2));
  28.         super.quadTo(x1, y1, x2, y2);
  29.     }
  30.  
  31.     private interface Action extends Serializable {
  32.  
  33.         void perform(Path path);
  34.     }
  35.  
  36.     private static final class Move implements Action {
  37.  
  38.         private final float x, y;
  39.  
  40.         public Move(float x, float y) {
  41.             this.x = x;
  42.             this.y = y;
  43.         }
  44.  
  45.         @Override
  46.         public void perform(Path path) {
  47.             path.moveTo(x, y);
  48.         }
  49.     }
  50.  
  51.     private static final class Line implements Action {
  52.  
  53.         private final float x, y;
  54.  
  55.         public Line(float x, float y) {
  56.             this.x = x;
  57.             this.y = y;
  58.         }
  59.  
  60.         @Override
  61.         public void perform(Path path) {
  62.             path.lineTo(x, y);
  63.         }
  64.     }
  65.  
  66.     private static final class Quad implements Action {
  67.  
  68.         private final float x1, y1, x2, y2;
  69.  
  70.         private Quad(float x1, float y1, float x2, float y2) {
  71.             this.x1 = x1;
  72.             this.y1 = y1;
  73.             this.x2 = x2;
  74.             this.y2 = y2;
  75.         }
  76.  
  77.         @Override
  78.         public void perform(Path path) {
  79.             path.quadTo(x1, y1, x2, y2);
  80.         }
  81.     }
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement