001package armyc2.c5isr.web.render.utilities; 002 003import java.util.ArrayList; 004import java.util.Arrays; 005 006import armyc2.c5isr.graphics2d.Rectangle2D; 007 008public class Path { 009 static class ActionTypes { 010 public static final int ACTION_MOVE_TO = 0; 011 public static final int ACTION_LINE_TO = 1; 012 public static final int ACTION_DASHED_LINE_TO = 6; 013 } 014 015 static class Action { 016 int actionType; 017 double x, y; 018 019 public Action(int actionType, double x, double y) { 020 this.actionType = actionType; 021 this.x = x; 022 this.y = y; 023 } 024 } 025 026 private ArrayList<Action> _actions = new ArrayList<>(); 027 private String _dashArray = null; 028 private Rectangle2D _rectangle = new Rectangle2D.Double(); 029 030 031 public void setLineDash(float[] dashArray) { 032 this._dashArray = Arrays.toString(dashArray) 033 .replace(",", "") 034 .replace("[", "") 035 .replace("]", "") 036 .trim(); 037 } 038 039 040 public Rectangle2D getBounds() { 041 return this._rectangle; 042 } 043 044 045 /** 046 * Adds a point to the path by moving to the specified coordinates specified 047 * 048 * @param x 049 * @param y 050 */ 051 public void moveTo(double x, double y) { 052 053 if (this._actions.size() == 0) { 054 this._rectangle = new Rectangle2D.Double(x, y, 1, 1); 055 } 056 this._rectangle.add(x, y); 057 this._actions.add(new Action(ActionTypes.ACTION_MOVE_TO, x, y)); 058 } 059 060 /** 061 * Adds a point to the path by drawing a straight line from the current 062 * coordinates to the new specified coordinates specified 063 * 064 * @param x 065 * @param y 066 */ 067 public void lineTo(double x, double y) { 068 069 if (this._actions.size() == 0) { 070 this.moveTo(0, 0); 071 } 072 this._actions.add(new Action(ActionTypes.ACTION_LINE_TO, x, y)); 073 this._rectangle.add(x, y); 074 } 075 076 /** 077 * Adds a point to the path by drawing a straight line from the current 078 * coordinates to the new specified coordinates specified 079 * 080 * @param x 081 * @param y 082 */ 083 public void dashedLineTo(double x, double y) { 084 if (this._actions.size() == 0) { 085 this.moveTo(0, 0); 086 } 087 this._actions.add(new Action(ActionTypes.ACTION_DASHED_LINE_TO, x, y)); 088 this._rectangle.add(x, y); 089 } 090 091 092 public String toSVGElement(String stroke, int strokeWidth, String fill, double strokeOpacity, double fillOpacity) { 093 //context.beginPath(); 094 int size = this._actions.size(); 095 Action temp = null; 096 String path = ""; 097 098 for (int i = 0; i < size; i++) { 099 temp = this._actions.get(i); 100 101 if (temp.actionType == ActionTypes.ACTION_LINE_TO) { 102 path += "L" + temp.x + " " + temp.y; 103 } else if (temp.actionType == ActionTypes.ACTION_MOVE_TO) { 104 path += "M" + temp.x + " " + temp.y; 105 } else if (temp.actionType == ActionTypes.ACTION_DASHED_LINE_TO) { 106 path += "L" + temp.x + " " + temp.y; 107 108 } 109 110 } 111 112 String line = "<path d=\"" + path + '"'; 113 114 if (stroke != null && !stroke.isEmpty()) { 115 116 line += " stroke=\"" + stroke + '"'; 117 118 119 if (strokeWidth != 0) 120 line += " stroke-width=\"" + strokeWidth + '"'; 121 else 122 line += " stroke-width=\"2\""; 123 124 if (strokeOpacity != 1.0) { 125 //stroke-opacity="0.4" 126 line += " stroke-opacity=\"" + strokeOpacity + '"'; 127 } 128 129 //line += ' stroke-linejoin="round"'; 130 line += " stroke-linecap=\"round\""; 131 //line += ' stroke-linecap="square"'; 132 } 133 134 if (this._dashArray != null) line += " stroke-dasharray=\"" + this._dashArray + '"'; 135 136 if (fill != null && !fill.isEmpty()) { 137 if (fill.indexOf("url") == 0) { 138 line += " fill=\"url(#fillPattern)\""; 139 //line += ' fill="url(#fillPattern)"'; 140 } else { 141 line += " fill=\"" + fill + '"';//text = text.replace(/\</g,">"); 142 if (fillOpacity != 1.0) { 143 //fill-opacity="0.4" 144 line += " fill-opacity=\"" + fillOpacity + '"'; 145 } 146 } 147 148 } else 149 line += " fill=\"none\""; 150 151 line += " />"; 152 return line; 153 154 } 155}