package utils; import config.subConfig.AxisConfig; import config.subConfig.TemplateConfig; import constant.Constant; import model.Room; import model.template.Template; import model.template.TemplateShadow; import java.awt.*; import java.util.ArrayList; import java.util.List; /** * @author duh * @create 2018/7/17 20:10 * @email duh@elab-plus.com **/ public class TemplateUtils { private static Room room; private static AxisConfig axisPointConfig; public static void drawTemplate(Template template, Graphics g){ if(null == room){ System.out.println("--------------room is not exist !!"); return; } drawTemplateName(template,g); drawTemplateSelf(template,g); drawShadow(template,g); } private static void drawTemplateName(Template template, Graphics g) { int x1 = room.getX()+template.getX()+template.getLength()/2; int y1 = room.getY()+template.getY()+template.getWidth()/2; g.drawString(template.getName(), x1, y1); } private static void drawTemplateSelf(Template template, Graphics g){ int length = template.getLength(); int width = template.getWidth(); int x1 = room.getX()+template.getX(); int y1 = room.getY()+template.getY(); Graphics2D g2d = (Graphics2D) g.create(); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setColor(Color.RED); if(length < 0){ length=-length; x1 = x1-length; } if(width < 0){ width=-width; y1=y1-width; } g2d.drawRect(x1,y1,length,width); drawAlignWall(template, length, width, x1, y1, g2d); g2d.dispose(); } /** * x1 y1 为四边形左上角顶点 * @param template * @param length * @param width * @param x1 * @param y1 * @param g2d */ private static void drawAlignWall(Template template, int length, int width, int x1, int y1, Graphics2D g2d) { //绘制贴墙边 List alignWall = template.getAlignWall(); for (int alignWallValue : alignWall){ switch (alignWallValue){ case 1: g2d.fillRect(x1,y1+width- Constant.CLOSE_WALL_LINE_WIDTH/2,length,Constant.CLOSE_WALL_LINE_WIDTH); break; case 2: g2d.fillRect(x1+length-Constant.CLOSE_WALL_LINE_WIDTH/2,y1,Constant.CLOSE_WALL_LINE_WIDTH,width); break; case 3: g2d.fillRect(x1,y1- Constant.CLOSE_WALL_LINE_WIDTH/2,length,Constant.CLOSE_WALL_LINE_WIDTH); break; case 4: g2d.fillRect(x1-Constant.CLOSE_WALL_LINE_WIDTH/2,y1,Constant.CLOSE_WALL_LINE_WIDTH,width); break; } } } /** * 绕接入点逆时针旋转rotateRange度 目前支持90的倍数 0:0 1:90 2:180 3:270 * @param template * @param g * @param rotateRange */ private static void drawTemplateSelfByRotate(Template template, Graphics g,int rotateRange){ routeTemplate(template,rotateRange); } /** * 旋转 * @param template * @param rotateRange */ public static void routeTemplate(Template template, int rotateRange) { int length = template.getLength(); int width = template.getWidth(); changeTemplateAlignWall(template,rotateRange); switch (rotateRange){ case 1: template.setLength(width); template.setWidth(-length); break; case 2: template.setLength(-length); template.setWidth(-width); break; case 3: template.setLength(-width); template.setWidth(length); break; } routeTemplateShadow(template,rotateRange); } public static void routeTemplateShadow(Template template, int rotateRange) { TemplateShadow shadow = template.getShadow(); int routeX = template.getX(); int routeY = template.getY(); int shadowX = shadow.getX(); int shadowY = shadow.getY(); switch (rotateRange){ case 1: shadow.setX(shadowY); shadow.setY(-shadowX); break; case 2: shadow.setX(-shadowX); shadow.setY(-shadowY); break; case 3: shadow.setX(-shadowY); shadow.setY(shadowX); break; } shadow.setDirect((shadow.getDirect()+rotateRange-1)%4+1); } public static void changeTemplateAlignWall(Template template,int rotateRange){ List list = template.getAlignWall(); List newlist = new ArrayList<>(); for(int i = 0;i templateConfigs){ int templateTotalLength = 0; for(TemplateConfig templateConfig : templateConfigs){ List alignWallList = templateConfig.getAlignWall(); int length = templateConfig.getLength(); int width = templateConfig.getWidth(); if(alignWallList.size() == 1){ int alignWall = alignWallList.get(0); if(alignWall%2 == 1){ templateTotalLength += length; }else { templateTotalLength+=width; } }else { templateTotalLength+= (length<=width)?length:width; } System.out.println("templateTotalLength=" + templateTotalLength); } System.out.println("room.getLength()=" + room.getLength()); return templateTotalLength<=room.getLength(); } public static void calcYizhiTemplatesAndDraw(List templateConfigs,Graphics g){ List