TemplateUtils.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. }
  137. public static void changeTemplateAlignWall(Template template,int rotateRange){
  138. List<Integer> list = template.getAlignWall();
  139. List<Integer> newlist = new ArrayList<>();
  140. for(int i = 0;i<list.size();i++){
  141. int alignWall = list.get(i);
  142. alignWall = (alignWall+rotateRange-1)%4+1;
  143. newlist.add(alignWall);
  144. }
  145. template.setAlignWall(newlist);
  146. }
  147. /**
  148. * 平移
  149. * @param template
  150. * @param x,y
  151. */
  152. private static void routeTemplate(TemplateConfig template, int x, int y) {
  153. x = x*Constant.ENLARGE/Constant.NARROW;
  154. y = y*Constant.ENLARGE/Constant.NARROW;
  155. template.setX(template.getX()+x);
  156. template.setY(template.getY()+y);
  157. }
  158. private static void drawShadow(Template template, Graphics g){
  159. TemplateShadow shadow = template.getShadow();
  160. int cx = room.getX()+template.getX()+shadow.getX();
  161. int cy = room.getY()+template.getY()+shadow.getY();
  162. int x1,y1,x2,y2;
  163. Graphics2D g2d = (Graphics2D) g.create();
  164. g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  165. g2d.setColor(Color.GRAY);
  166. switch (shadow.getDirect()){
  167. case 0:
  168. x1 = cx;
  169. y1 = cy - template.getWidth()/2;
  170. x2 = cx+shadow.getRadis();
  171. y2 = cy + template.getWidth()/2;
  172. g2d.fillRect(x1,y1,shadow.getRadis(),template.getWidth());
  173. break;
  174. case 90:
  175. x1 = cx-shadow.getRadis()/2;
  176. y1 = cy;
  177. x2 = cx+shadow.getRadis();
  178. y2 = cy + template.getWidth()/2;
  179. g2d.fillRect(x1,y1,shadow.getRadis(),shadow.getRadis());
  180. break;
  181. default:break;
  182. }
  183. g2d.dispose();
  184. }
  185. public static boolean checkYiZhiArray(List<TemplateConfig> templateConfigs){
  186. int templateTotalLength = 0;
  187. for(TemplateConfig templateConfig : templateConfigs){
  188. List<Integer> alignWallList = templateConfig.getAlignWall();
  189. int length = templateConfig.getLength();
  190. int width = templateConfig.getWidth();
  191. if(alignWallList.size() == 1){
  192. int alignWall = alignWallList.get(0);
  193. if(alignWall%2 == 1){
  194. templateTotalLength += length;
  195. }else {
  196. templateTotalLength+=width;
  197. }
  198. }else {
  199. templateTotalLength+= (length<=width)?length:width;
  200. }
  201. System.out.println("templateTotalLength=" + templateTotalLength);
  202. }
  203. System.out.println("room.getLength()=" + room.getLength());
  204. return templateTotalLength<=room.getLength();
  205. }
  206. public static void drawTemplates(List<TemplateConfig> templateConfigs){
  207. // for
  208. }
  209. public static void setRoom(Room room) {
  210. TemplateUtils.room = room;
  211. }
  212. public static AxisConfig getAxisPointConfig() {
  213. return axisPointConfig;
  214. }
  215. public static void setAxisPointConfig(AxisConfig axisPointConfig) {
  216. TemplateUtils.axisPointConfig = axisPointConfig;
  217. }
  218. }