TemplateUtils.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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.TemplateClearance;
  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. private static Color[] templateColors = {Color.RED,Color.GREEN,Color.blue,Color.CYAN};
  20. private static int templateIndex = 0;
  21. public static void drawTemplate(Template template, Graphics g){
  22. if(null == room){
  23. System.out.println("--------------room is not exist !!");
  24. return;
  25. }
  26. drawTemplateName(template,g);
  27. drawTemplateSelf(template,g);
  28. drawShadow(template,g);
  29. }
  30. private static void drawTemplateName(Template template, Graphics g) {
  31. int x1 = room.getX()+template.getX()+template.getLength()/2;
  32. int y1 = room.getY()+template.getY()+template.getWidth()/2;
  33. g.drawString(template.getName(), x1, y1);
  34. }
  35. private static void drawTemplateSelf(Template template, Graphics g){
  36. int length = template.getLength();
  37. int width = template.getWidth();
  38. int x1 = room.getX()+template.getX();
  39. int y1 = room.getY()+template.getY();
  40. Graphics2D g2d = (Graphics2D) g.create();
  41. g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  42. g2d.setColor(templateColors[templateIndex++%templateColors.length]);
  43. if(length < 0){
  44. length=-length;
  45. x1 = x1-length;
  46. }
  47. if(width < 0){
  48. width=-width;
  49. y1=y1-width;
  50. }
  51. g2d.drawRect(x1,y1,length,width);
  52. drawAlignWall(template, length, width, x1, y1, g2d);
  53. g2d.dispose();
  54. }
  55. /**
  56. * x1 y1 为四边形左上角顶点
  57. * @param template
  58. * @param length
  59. * @param width
  60. * @param x1
  61. * @param y1
  62. * @param g2d
  63. */
  64. private static void drawAlignWall(Template template, int length, int width, int x1, int y1, Graphics2D g2d) {
  65. //绘制贴墙边
  66. List<Integer> alignWall = template.getAlignWall();
  67. for (int alignWallValue : alignWall){
  68. switch (alignWallValue){
  69. case 1:
  70. g2d.fillRect(x1,y1+width- Constant.CLOSE_WALL_LINE_WIDTH/2,length,Constant.CLOSE_WALL_LINE_WIDTH);
  71. break;
  72. case 2:
  73. g2d.fillRect(x1+length-Constant.CLOSE_WALL_LINE_WIDTH/2,y1,Constant.CLOSE_WALL_LINE_WIDTH,width);
  74. break;
  75. case 3:
  76. g2d.fillRect(x1,y1- Constant.CLOSE_WALL_LINE_WIDTH/2,length,Constant.CLOSE_WALL_LINE_WIDTH);
  77. break;
  78. case 4:
  79. g2d.fillRect(x1-Constant.CLOSE_WALL_LINE_WIDTH/2,y1,Constant.CLOSE_WALL_LINE_WIDTH,width);
  80. break;
  81. }
  82. }
  83. }
  84. /**
  85. * 绕接入点逆时针旋转rotateRange度 目前支持90的倍数 0:0 1:90 2:180 3:270
  86. * @param template
  87. * @param g
  88. * @param rotateRange
  89. */
  90. private static void drawTemplateSelfByRotate(Template template, Graphics g,int rotateRange){
  91. routeTemplate(template,rotateRange);
  92. }
  93. /**
  94. * 旋转
  95. * @param template
  96. * @param rotateRange
  97. */
  98. public static void routeTemplate(Template template, int rotateRange) {
  99. int length = template.getLength();
  100. int width = template.getWidth();
  101. changeTemplateAlignWall(template,rotateRange);
  102. switch (rotateRange){
  103. case 1:
  104. template.setLength(width);
  105. template.setWidth(-length);
  106. break;
  107. case 2:
  108. template.setLength(-length);
  109. template.setWidth(-width);
  110. break;
  111. case 3:
  112. template.setLength(-width);
  113. template.setWidth(length);
  114. break;
  115. }
  116. routeTemplateShadow(template,rotateRange);
  117. }
  118. public static void routeTemplateShadow(Template template, int rotateRange) {
  119. TemplateClearance shadow = template.getShadow();
  120. int routeX = template.getX();
  121. int routeY = template.getY();
  122. int shadowX = shadow.getX();
  123. int shadowY = shadow.getY();
  124. switch (rotateRange){
  125. case 1:
  126. shadow.setX(shadowY);
  127. shadow.setY(-shadowX);
  128. break;
  129. case 2:
  130. shadow.setX(-shadowX);
  131. shadow.setY(-shadowY);
  132. break;
  133. case 3:
  134. shadow.setX(-shadowY);
  135. shadow.setY(shadowX);
  136. break;
  137. }
  138. shadow.setDirect((shadow.getDirect()+rotateRange-1)%4+1);
  139. }
  140. public static void changeTemplateAlignWall(Template template,int rotateRange){
  141. List<Integer> list = template.getAlignWall();
  142. List<Integer> newlist = new ArrayList<>();
  143. for(int i = 0;i<list.size();i++){
  144. int alignWall = list.get(i);
  145. alignWall = (alignWall+rotateRange-1)%4+1;
  146. newlist.add(alignWall);
  147. }
  148. template.setAlignWall(newlist);
  149. }
  150. /**
  151. * 平移
  152. * @param template
  153. * @param x,y
  154. */
  155. private static void moveTemplate(TemplateConfig template, int x, int y) {
  156. x = x*Constant.ENLARGE/Constant.NARROW;
  157. y = y*Constant.ENLARGE/Constant.NARROW;
  158. template.setX(template.getX()+x);
  159. template.setY(template.getY()+y);
  160. }
  161. private static void drawShadow(Template template, Graphics g){
  162. TemplateClearance shadow = template.getShadow();
  163. int cx = room.getX()+template.getX()+shadow.getX();
  164. int cy = room.getY()+template.getY()+shadow.getY();
  165. System.out.println(template.getName()+"阴影接入点:"+cx+","+cy);
  166. int x1,y1,x2,y2;
  167. // g.drawArc(cx,cy,6,6,0,360);
  168. // if(1==1){
  169. // return;
  170. // }
  171. Graphics2D g2d = (Graphics2D) g.create();
  172. g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  173. g2d.setColor(Color.GRAY);
  174. int shadowWidth = shadow.getWidth();
  175. if(shadowWidth < 0){
  176. shadowWidth = -shadowWidth;
  177. }
  178. switch (shadow.getDirect()){
  179. case 1:
  180. x1 = cx-shadow.getWidth()/2;
  181. y1 = cy;
  182. g2d.drawRect(x1,y1,shadowWidth,shadow.getRadis());
  183. break;
  184. case 2:
  185. x1 = cx;
  186. y1 = cy - shadowWidth/2;
  187. g2d.drawRect(x1,y1,shadow.getRadis(),shadowWidth);
  188. break;
  189. case 3:
  190. x1 = cx-shadow.getRadis()/2;
  191. y1 = cy-shadowWidth;
  192. g2d.drawRect(x1,y1,shadowWidth,shadow.getRadis());
  193. break;
  194. case 4:
  195. x1 = cx-shadow.getRadis();
  196. y1 = cy-shadowWidth/2;
  197. g2d.drawRect(x1,y1,shadow.getRadis(),shadowWidth);
  198. break;
  199. default:break;
  200. }
  201. g2d.dispose();
  202. }
  203. public static boolean checkYiZhiArray(List<TemplateConfig> templateConfigs){
  204. int templateTotalLength = 0;
  205. for(TemplateConfig templateConfig : templateConfigs){
  206. List<Integer> alignWallList = templateConfig.getAlignWall();
  207. int length = templateConfig.getLength();
  208. int width = templateConfig.getWidth();
  209. if(alignWallList.size() == 1){
  210. int alignWall = alignWallList.get(0);
  211. if(alignWall%2 == 1){
  212. templateTotalLength += length;
  213. }else {
  214. templateTotalLength+=width;
  215. }
  216. }else {
  217. templateTotalLength+= (length<=width)?length:width;
  218. }
  219. System.out.println("templateTotalLength=" + templateTotalLength);
  220. }
  221. System.out.println("room.getLength()=" + room.getLength());
  222. return templateTotalLength<=room.getLength();
  223. }
  224. public static void calcYizhiTemplatesAndDraw(List<TemplateConfig> templateConfigs,Graphics g){
  225. List<Template> oneAlignWall = new ArrayList<>();
  226. List<Template> twoAlignWall = new ArrayList<>();
  227. getOneAndTwoAlignTemplate(templateConfigs, oneAlignWall, twoAlignWall);
  228. drawTwoAlignWallTemplates(twoAlignWall,g);
  229. drawOneAlignWallTemplates(oneAlignWall,g);
  230. }
  231. private static void getOneAndTwoAlignTemplate(List<TemplateConfig> templateConfigs, List<Template> oneAlignWall, List<Template> twoAlignWall) {
  232. for (TemplateConfig templateConfig : templateConfigs){
  233. Template template = new Template(templateConfig);
  234. List<Integer> alignWall = template.getAlignWall();
  235. if(alignWall.size() == 1){
  236. oneAlignWall.add(template);
  237. }else if(alignWall.size() == 2){
  238. twoAlignWall.add(template);
  239. }
  240. }
  241. }
  242. private static void drawTwoAlignWallTemplates(List<Template> twoAlignWall, Graphics g) {
  243. int roomAlign = 3;
  244. int positionOff = 0;
  245. for(Template template:twoAlignWall){
  246. int begin = template.getAlignWall().get(0);
  247. int routeTimes = roomAlign-begin-1;
  248. if(routeTimes<0){
  249. routeTimes += 4;
  250. }
  251. if(routeTimes!=0){
  252. routeTemplate(template,routeTimes);
  253. }
  254. template.setX(room.getLength());
  255. template.setY(template.getY()-room.getWidth());
  256. drawTemplate(template,g);
  257. }
  258. }
  259. public static void drawOneAlignWallTemplates(List<Template> templates,Graphics g){
  260. int roomAlign = 3;
  261. int positionOff = 0;
  262. for (Template template : templates){
  263. template.setX(0);
  264. template.setY(0);
  265. List<Integer> alignWall = template.getAlignWall();
  266. int templateAlignWall = alignWall.get(0);
  267. int routeTimes = roomAlign-templateAlignWall%4;
  268. if(routeTimes<0){
  269. routeTimes+=4;
  270. }
  271. if(routeTimes!=0){
  272. routeTemplate(template,routeTimes);
  273. }
  274. template.setY(template.getY()-room.getWidth());
  275. if(template.getLength()<0){
  276. template.setX(template.getX()-template.getLength());
  277. }
  278. //放模板后,其他模板需要依次移动positionOff位置
  279. template.setX(template.getX()+positionOff);
  280. System.out.println("positionOff 1 = " + positionOff);
  281. if((templateAlignWall+routeTimes)%2==0){
  282. positionOff+=Math.abs(template.getWidth());
  283. }else {
  284. positionOff+=Math.abs(template.getLength());
  285. }
  286. System.out.println("positionOff 2 = " + positionOff);
  287. drawTemplate(template,g);
  288. }
  289. }
  290. public static void setRoom(Room room) {
  291. TemplateUtils.room = room;
  292. }
  293. public static AxisConfig getAxisPointConfig() {
  294. return axisPointConfig;
  295. }
  296. public static void setAxisPointConfig(AxisConfig axisPointConfig) {
  297. TemplateUtils.axisPointConfig = axisPointConfig;
  298. }
  299. public static void setTemplateIndex(int templateIndex) {
  300. TemplateUtils.templateIndex = templateIndex;
  301. }
  302. /**
  303. * posintion like [321, 312, 943]
  304. * @param position
  305. * @param templateConfigs
  306. * @param g
  307. */
  308. public static void drawTemplateWithArray(List<Integer> position,List<TemplateConfig> templateConfigs,Graphics g){
  309. List<Template> oneAlignWall = new ArrayList<>();
  310. List<Template> twoAlignWall = new ArrayList<>();
  311. getOneAndTwoAlignTemplate(templateConfigs, oneAlignWall, twoAlignWall);
  312. int twoAlignPosition = position.get(2);
  313. //画2边贴墙
  314. drawAlignTwoTemplateWithArrayPosition(g, twoAlignWall, twoAlignPosition);
  315. }
  316. private static void drawAlignTwoTemplateWithArrayPosition(Graphics g, List<Template> twoAlignWall, int twoAlignPosition) {
  317. int wallPointIndex = twoAlignPosition%100/10;
  318. int alignIndex = twoAlignPosition%10;
  319. Template twoAlignWallTemplate = twoAlignWall.get(0);
  320. int templateX = twoAlignWallTemplate.getX();
  321. int templateY = twoAlignWallTemplate.getY();
  322. System.out.println("drawAlignTwoTemplateWithArrayPosition 1 ="+templateX+","+templateY);
  323. switch (wallPointIndex){
  324. case 2:
  325. twoAlignWallTemplate.setX(templateX+room.getLength());
  326. break;
  327. case 3:
  328. twoAlignWallTemplate.setX(templateX+room.getLength());
  329. twoAlignWallTemplate.setY(templateY-room.getWidth());
  330. break;
  331. case 4:
  332. twoAlignWallTemplate.setY(templateY-room.getWidth());
  333. break;
  334. }
  335. System.out.println("drawAlignTwoTemplateWithArrayPosition 2 ="+twoAlignWallTemplate.getX()+","+twoAlignWallTemplate.getY());
  336. if(alignIndex!=4){
  337. routeTemplate(twoAlignWallTemplate,alignIndex);
  338. }
  339. saveTemplate(twoAlignWallTemplate,g);
  340. }
  341. public static void saveTemplate(Template template, Graphics g){
  342. if(null == room){
  343. System.out.println("--------------room is not exist !!");
  344. return;
  345. }
  346. saveTemplateName(template,g);
  347. saveTemplateSelf(template,g);
  348. saveShadow(template,g);
  349. }
  350. private static void saveTemplateName(Template template, Graphics g) {
  351. int x1 = room.getX()+template.getX()+template.getLength()/2;
  352. int y1 = room.getY()+template.getY()+template.getWidth()/2;
  353. g.drawString(template.getName(), x1, y1);
  354. }
  355. private static void saveTemplateSelf(Template template, Graphics g){
  356. int length = template.getLength();
  357. int width = template.getWidth();
  358. int x1 = room.getX()+template.getX();
  359. int y1 = room.getY()+template.getY();
  360. Graphics2D g2d = (Graphics2D) g.create();
  361. g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  362. g2d.setColor(templateColors[templateIndex++%templateColors.length]);
  363. if(length < 0){
  364. length=-length;
  365. x1 = x1-length;
  366. }
  367. if(width < 0){
  368. width=-width;
  369. y1=y1-width;
  370. }
  371. g2d.drawRect(x1,y1,length,width);
  372. drawAlignWall(template, length, width, x1, y1, g2d);
  373. g2d.dispose();
  374. }
  375. private static void saveShadow(Template template, Graphics g){
  376. TemplateClearance shadow = template.getShadow();
  377. int cx = room.getX()+template.getX()+shadow.getX();
  378. int cy = room.getY()+template.getY()+shadow.getY();
  379. System.out.println(template.getName()+"阴影接入点:"+cx+","+cy);
  380. int x1,y1,x2,y2;
  381. Graphics2D g2d = (Graphics2D) g.create();
  382. g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  383. g2d.setColor(Color.GRAY);
  384. int shadowWidth = shadow.getWidth();
  385. if(shadowWidth < 0){
  386. shadowWidth = -shadowWidth;
  387. }
  388. switch (shadow.getDirect()){
  389. case 1:
  390. x1 = cx-shadow.getWidth()/2;
  391. y1 = cy;
  392. g2d.drawRect(x1,y1,shadowWidth,shadow.getRadis());
  393. break;
  394. case 2:
  395. x1 = cx;
  396. y1 = cy - shadowWidth/2;
  397. g2d.drawRect(x1,y1,shadow.getRadis(),shadowWidth);
  398. break;
  399. case 3:
  400. x1 = cx-shadow.getRadis()/2;
  401. y1 = cy-shadowWidth;
  402. g2d.drawRect(x1,y1,shadowWidth,shadow.getRadis());
  403. break;
  404. case 4:
  405. x1 = cx-shadow.getRadis();
  406. y1 = cy-shadowWidth/2;
  407. g2d.drawRect(x1,y1,shadow.getRadis(),shadowWidth);
  408. break;
  409. default:break;
  410. }
  411. g2d.dispose();
  412. }
  413. }