TemplateUtils.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. package utils;
  2. import config.subConfig.AxisConfig;
  3. import config.subConfig.TemplateConfig;
  4. import constant.Constant;
  5. import model.Room;
  6. import model.template.Template;
  7. import model.template.TemplateShadow;
  8. import java.awt.*;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. /**
  12. * @author duh
  13. * @create 2018/7/17 20:10
  14. * @email duh@elab-plus.com
  15. **/
  16. public class TemplateUtils {
  17. private static Room room;
  18. private static AxisConfig axisPointConfig;
  19. public static void drawTemplate(Template template, Graphics g){
  20. if(null == room){
  21. System.out.println("--------------room is not exist !!");
  22. return;
  23. }
  24. drawTemplateName(template,g);
  25. drawTemplateSelf(template,g);
  26. drawShadow(template,g);
  27. }
  28. private static void drawTemplateName(Template template, Graphics g) {
  29. int x1 = room.getX()+template.getX()+template.getLength()/2;
  30. int y1 = room.getY()+template.getY()+template.getWidth()/2;
  31. g.drawString(template.getName(), x1, y1);
  32. }
  33. private static void drawTemplateSelf(Template template, Graphics g){
  34. int length = template.getLength();
  35. int width = template.getWidth();
  36. int x1 = room.getX()+template.getX();
  37. int y1 = room.getY()+template.getY();
  38. Graphics2D g2d = (Graphics2D) g.create();
  39. g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  40. g2d.setColor(Color.RED);
  41. if(length < 0){
  42. length=-length;
  43. x1 = x1-length;
  44. }
  45. if(width < 0){
  46. width=-width;
  47. y1=y1-width;
  48. }
  49. g2d.drawRect(x1,y1,length,width);
  50. drawAlignWall(template, length, width, x1, y1, g2d);
  51. g2d.dispose();
  52. }
  53. /**
  54. * x1 y1 为四边形左上角顶点
  55. * @param template
  56. * @param length
  57. * @param width
  58. * @param x1
  59. * @param y1
  60. * @param g2d
  61. */
  62. private static void drawAlignWall(Template template, int length, int width, int x1, int y1, Graphics2D g2d) {
  63. //绘制贴墙边
  64. List<Integer> alignWall = template.getAlignWall();
  65. for (int alignWallValue : alignWall){
  66. switch (alignWallValue){
  67. case 1:
  68. g2d.fillRect(x1,y1+width- Constant.CLOSE_WALL_LINE_WIDTH/2,length,Constant.CLOSE_WALL_LINE_WIDTH);
  69. break;
  70. case 2:
  71. g2d.fillRect(x1+length-Constant.CLOSE_WALL_LINE_WIDTH/2,y1,Constant.CLOSE_WALL_LINE_WIDTH,width);
  72. break;
  73. case 3:
  74. g2d.fillRect(x1,y1- Constant.CLOSE_WALL_LINE_WIDTH/2,length,Constant.CLOSE_WALL_LINE_WIDTH);
  75. break;
  76. case 4:
  77. g2d.fillRect(x1-Constant.CLOSE_WALL_LINE_WIDTH/2,y1,Constant.CLOSE_WALL_LINE_WIDTH,width);
  78. break;
  79. }
  80. }
  81. }
  82. /**
  83. * 绕接入点逆时针旋转rotateRange度 目前支持90的倍数 0:0 1:90 2:180 3:270
  84. * @param template
  85. * @param g
  86. * @param rotateRange
  87. */
  88. private static void drawTemplateSelfByRotate(Template template, Graphics g,int rotateRange){
  89. routeTemplate(template,rotateRange);
  90. }
  91. /**
  92. * 旋转
  93. * @param template
  94. * @param rotateRange
  95. */
  96. public static void routeTemplate(Template template, int rotateRange) {
  97. int length = template.getLength();
  98. int width = template.getWidth();
  99. changeTemplateAlignWall(template,rotateRange);
  100. switch (rotateRange){
  101. case 1:
  102. template.setLength(width);
  103. template.setWidth(-length);
  104. break;
  105. case 2:
  106. template.setLength(-length);
  107. template.setWidth(-width);
  108. break;
  109. case 3:
  110. template.setLength(-width);
  111. template.setWidth(length);
  112. break;
  113. }
  114. routeTemplateShadow(template,rotateRange);
  115. }
  116. public static void routeTemplateShadow(Template template, int rotateRange) {
  117. TemplateShadow shadow = template.getShadow();
  118. int routeX = template.getX();
  119. int routeY = template.getY();
  120. int shadowX = shadow.getX();
  121. int shadowY = shadow.getY();
  122. switch (rotateRange){
  123. case 1:
  124. shadow.setX(shadowY);
  125. shadow.setY(-shadowX);
  126. break;
  127. case 2:
  128. shadow.setX(-shadowX);
  129. shadow.setY(-shadowY);
  130. break;
  131. case 3:
  132. shadow.setX(-shadowY);
  133. shadow.setY(shadowX);
  134. break;
  135. }
  136. shadow.setDirect((shadow.getDirect()+rotateRange-1)%4+1);
  137. }
  138. public static void changeTemplateAlignWall(Template template,int rotateRange){
  139. List<Integer> list = template.getAlignWall();
  140. List<Integer> newlist = new ArrayList<>();
  141. for(int i = 0;i<list.size();i++){
  142. int alignWall = list.get(i);
  143. alignWall = (alignWall+rotateRange-1)%4+1;
  144. newlist.add(alignWall);
  145. }
  146. template.setAlignWall(newlist);
  147. }
  148. /**
  149. * 平移
  150. * @param template
  151. * @param x,y
  152. */
  153. private static void moveTemplate(TemplateConfig template, int x, int y) {
  154. x = x*Constant.ENLARGE/Constant.NARROW;
  155. y = y*Constant.ENLARGE/Constant.NARROW;
  156. template.setX(template.getX()+x);
  157. template.setY(template.getY()+y);
  158. }
  159. private static void drawShadow(Template template, Graphics g){
  160. TemplateShadow shadow = template.getShadow();
  161. int cx = room.getX()+template.getX()+shadow.getX();
  162. int cy = room.getY()+template.getY()+shadow.getY();
  163. System.out.println(template.getName()+"阴影接入点:"+cx+","+cy);
  164. int x1,y1,x2,y2;
  165. // g.drawArc(cx,cy,6,6,0,360);
  166. // if(1==1){
  167. // return;
  168. // }
  169. Graphics2D g2d = (Graphics2D) g.create();
  170. g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  171. g2d.setColor(Color.GRAY);
  172. int shadowWidth = shadow.getWidth();
  173. if(shadowWidth < 0){
  174. shadowWidth = -shadowWidth;
  175. }
  176. switch (shadow.getDirect()){
  177. case 1:
  178. x1 = cx-shadow.getWidth()/2;
  179. y1 = cy;
  180. g2d.fillRect(x1,y1,shadowWidth,shadow.getRadis());
  181. break;
  182. case 2:
  183. x1 = cx;
  184. y1 = cy - shadowWidth/2;
  185. g2d.fillRect(x1,y1,shadow.getRadis(),shadowWidth);
  186. break;
  187. case 3:
  188. x1 = cx-shadow.getRadis()/2;
  189. y1 = cy-shadowWidth;
  190. g2d.fillRect(x1,y1,shadowWidth,shadow.getRadis());
  191. break;
  192. case 4:
  193. x1 = cx-shadow.getRadis();
  194. y1 = cy-shadowWidth/2;
  195. g2d.fillRect(x1,y1,shadow.getRadis(),shadowWidth);
  196. break;
  197. default:break;
  198. }
  199. g2d.dispose();
  200. }
  201. public static boolean checkYiZhiArray(List<TemplateConfig> templateConfigs){
  202. int templateTotalLength = 0;
  203. for(TemplateConfig templateConfig : templateConfigs){
  204. List<Integer> alignWallList = templateConfig.getAlignWall();
  205. int length = templateConfig.getLength();
  206. int width = templateConfig.getWidth();
  207. if(alignWallList.size() == 1){
  208. int alignWall = alignWallList.get(0);
  209. if(alignWall%2 == 1){
  210. templateTotalLength += length;
  211. }else {
  212. templateTotalLength+=width;
  213. }
  214. }else {
  215. templateTotalLength+= (length<=width)?length:width;
  216. }
  217. System.out.println("templateTotalLength=" + templateTotalLength);
  218. }
  219. System.out.println("room.getLength()=" + room.getLength());
  220. return templateTotalLength<=room.getLength();
  221. }
  222. public static void calcYizhiTemplatesAndDraw(List<TemplateConfig> templateConfigs,Graphics g){
  223. List<Template> oneAlignWall = new ArrayList<>();
  224. List<Template> twoAlignWall = new ArrayList<>();
  225. for (TemplateConfig templateConfig : templateConfigs){
  226. Template template = new Template(templateConfig);
  227. List<Integer> alignWall = template.getAlignWall();
  228. if(alignWall.size() == 1){
  229. oneAlignWall.add(template);
  230. }else if(alignWall.size() == 2){
  231. twoAlignWall.add(template);
  232. }
  233. }
  234. drawTwoAlignWallTemplates(twoAlignWall,g);
  235. drawOneAlignWallTemplates(oneAlignWall,g);
  236. }
  237. private static void drawTwoAlignWallTemplates(List<Template> twoAlignWall, Graphics g) {
  238. int roomAlign = 3;
  239. int positionOff = 0;
  240. for(Template template:twoAlignWall){
  241. int begin = template.getAlignWall().get(0);
  242. int routeTimes = roomAlign-begin-1;
  243. if(routeTimes<0){
  244. routeTimes += 4;
  245. }
  246. if(routeTimes!=0){
  247. routeTemplate(template,routeTimes);
  248. }
  249. template.setX(room.getLength());
  250. template.setY(template.getY()-room.getWidth());
  251. drawTemplate(template,g);
  252. }
  253. }
  254. public static void drawOneAlignWallTemplates(List<Template> templates,Graphics g){
  255. int roomAlign = 3;
  256. int positionOff = 0;
  257. for (Template template : templates){
  258. List<Integer> alignWall = template.getAlignWall();
  259. int templateAlignWall = alignWall.get(0);
  260. int routeTimes = roomAlign-templateAlignWall%4;
  261. if(routeTimes<0){
  262. routeTimes+=4;
  263. }
  264. if(routeTimes!=0){
  265. routeTemplate(template,routeTimes);
  266. }
  267. template.setY(template.getY()-room.getWidth());
  268. if(template.getLength()<0){
  269. template.setX(template.getX()-template.getLength());
  270. }
  271. //放模板后,其他模板需要依次移动positionOff位置
  272. template.setX(template.getX()+positionOff);
  273. System.out.println("positionOff 1 = " + positionOff);
  274. if((templateAlignWall+routeTimes)%2==0){
  275. positionOff+=Math.abs(template.getWidth());
  276. }else {
  277. positionOff+=Math.abs(template.getLength());
  278. }
  279. System.out.println("positionOff 2 = " + positionOff);
  280. drawTemplate(template,g);
  281. }
  282. }
  283. public static void setRoom(Room room) {
  284. TemplateUtils.room = room;
  285. }
  286. public static AxisConfig getAxisPointConfig() {
  287. return axisPointConfig;
  288. }
  289. public static void setAxisPointConfig(AxisConfig axisPointConfig) {
  290. TemplateUtils.axisPointConfig = axisPointConfig;
  291. }
  292. }