Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PathComponent {
- private final PathSegment[] segments;
- public HashMap<String, Object> properties = new HashMap<String, Object>();
- public PathComponent (PathSegment[] segments) {
- this.segments = segments;
- }
- public void draw (Graphics k, int aaSampleCount) {
- PathPoint maximum = this.points[0];
- for (int i = 0; i < this.points.length; i++) {
- PathPoint p = this.points[i];
- if (p.getX() > maximum.getX()) maximum.setX(p.getX());
- if (p.getY() > maximum.getY()) maximum.setY(p.getY());
- }
- BufferedImage buffer1 = new BufferedImage((maximum.getX() + this.getInt("stroke")) * aaSampleCount, (maximum.getY() + this.getInt("stroke")) * aaSampleCount, BufferedImage.TYPE_INT_ARGB);
- Graphics g = buffer1.createGraphics();
- ArrayList<Point> polyPoints = new ArrayList<Point>();
- for (PathSegment s : this.segments) {
- long max = 0x0010000000000000L >> getInt("precision");
- for (long l = 0; l < max; l++) {
- double t = Double.longBitsToDouble(l) - 1;
- polyPoints.add(s.getPoint(t));
- drawPoint(g, s, t, this.getInt("stroke"), this.<Color>getObject("strokeColor", Color.class, null), aaSampleCount);
- }
- }
- int[] xpoly = new int[polyPoints.size()];
- int[] ypoly = new int[polyPoints.size()];
- for (int i = 0; i < polyPoints.size(); i++) {
- xpoly[i] = ((int) polyPoints.get(i).getX() + this.getInt("stroke")) * aaSampleCount;
- ypoly[i] = ((int) polyPoints.get(i).getY() + this.getInt("stroke")) * aaSampleCount;
- }
- BufferedImage buffer2 = new BufferedImage(buffer1.getWidth(), buffer1.getHeight(), BufferedImage.TYPE_INT_ARGB);
- g = this.buffer2.createGraphics();
- g.setColor(this.<Color>getObject("fillColor", Color.class, new Color(0, 0, 0, 255)));
- g.fillPolygon(xpoly, ypoly, xpoly.length);
- g.drawImage(buffer1, 0, 0, null);
- k.drawImage(buffer2, this.getInt("x") - this.getInt("stroke"), this.getInt("y") - this.getInt("stroke"), null);
- }
- private int getInt (String s) {
- return this.<Integer>getObject(s, Integer.class, Integer.valueOf(0)).intValue();
- }
- private <T> T getObject (String s, Class<T> type, T fallback) {
- Object o = this.properties.get(s);
- if (o == null) return fallback;
- if (type.isInstance(o)) return type.cast(o);
- return fallback;
- }
- private static void drawPoint (Graphics g, PathSegment s, double t, int stroke, Color color, int aaSampleCount) {
- Point p = s.getPoint(t);
- double hs = (stroke / 2.0) * aaSampleCount;
- g.setColor(color);
- g.fillOval((int) ((p.getX() * aaSampleCount) - hs), (int) ((p.getX() * aaSampleCount) - hs), stroke * aaSampleCount, stroke * aaSampleCount);
- }
- public PathSegment[] getSegments () {
- return this.segments;
- }
- }
- public class PathSegment {
- public final int degree;
- public final Point[] controls;
- public PathSegment (int degree, Point[] controls) {
- this.degree = degree;
- this.controls = controls;
- }
- public Point getPoint (double t) {
- if (this.degree == 0) {
- Point p = new Point();
- p.setX(-1);
- p.setY(-1);
- return p;
- }
- return bezier(this.degree, t, this.controls);
- }
- public static Point bezier (int degree, double time, Point... controls) {
- if (controls.length < degree) throw new IllegalArgumentException("controls.length < degree");
- Point sum = new Point();
- for (int i = 0; i < degree; i++) {
- double x = controls[i].getX();
- double y = controls[i].getY();
- double d = ((int) binomialCoefficient(n, i)) * Math.pow(1 - time, i) * Math.pow(time, i);
- sum.setX(sum.getX() + (d * x));
- sum.setY(sum.getY() + (d * y));
- }
- return sum;
- }
- private static long binomialCoefficient (int n, int k) {
- if (k > n - k)
- k = n - k;
- long b = 1;
- for (int i = 1, m = n; i <= k; i++, m--)
- b = b * m / i;
- return b;
- }
- }
- public class PathSerializer {
- public static PathComponent read (DataInput i) {
- int segmentCount = i.readUnsignedShort();
- PathSegment[] segments = new PathSegment[segmentCount];
- for (int j = 0; j < segmentCount; j++)
- segments[j] = readSegment(i);
- PathComponent comp = new PathComponent(segments);
- comp.properties.put("x", Integer.valueOf(i.readUnsignedShort()));
- comp.properties.put("y", Integer.valueOf(i.readUnsignedShort()));
- int ps = i.readUnsignedShort();
- comp.properties.put("precision", Integer.valueOf((ps & 0xF000) >> 12));
- comp.properties.put("stroke", Integer.valueOf(ps & 0x0FFF));
- comp.properties.put("strokeColor", new Color(i.readInt(), true));
- comp.properties.put("fillColor", new Color(i.readInt(), true));
- return comp;
- }
- public static boolean write (PathComponent comp, DataOutput o) {
- PathSegment[] segments = comp.getSegments();
- if ((segments.length & 0xFFFF) != segments.length) return false;
- o.writeShort(segments.length);
- for (int i = 0; i < segments.length; i++)
- if (writeSegment(segments[i], o)) return false;
- try {
- o.writeShort(getInt("x", comp.properties));
- o.writeShort(getInt("y", comp.properties));
- int p = getInt("precision", comp.properties);
- if ((p & 0x0F) != p) return false;
- int s = getInt("stroke", comp.properties);
- if ((s & 0x0FFF) != s) return false;
- o.writeShort((p << 12) | s);
- } catch (IllegalArgumentException iae) {
- return false;
- }
- Object color1 = comp.properties.get("strokeColor");
- if (color1 == null || !(color1 instanceof Color)) return false;
- o.writeInt(color1.getRGB());
- Object color2 = comp.properties.get("fillColor");
- if (color2 == null || !(color2 instanceof Color)) return false;
- o.writeInt(color2.getRGB());
- }
- private static int getShort (String s, HashMap<String, Object> map) {
- Object o = map.get(s);
- if (o == null || !(o instanceof Integer)) throw new IllegalArgumentException();
- int i = ((Integer) o).intValue();
- if ((i & 0xFFFF) != i) throw new IllegalArgumentException();
- return i;
- }
- private static PathSegment readSegment (DataInput i) {
- int degree = i.readUnsignedByte();
- Point[] controls = new Point[degree + 1];
- for (int j = 0; j <= degree; j++) {
- controls[j] = new Point();
- controls[j].setX(i.readUnsignedShort());
- controls[j].setY(i.readUnsignedShort());
- }
- return new PathSegment(degree, controls);
- }
- private static boolean writeSegment (PathSegment s, DataOutput o) {
- if ((s.degree & 0xFF) != s.degree) return false;
- o.writeByte(s.degree);
- for (Point c : s.controls) {
- o.writeShort((int) c.getX());
- o.writeShort((int) c.getY());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment