123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436 |
- package utils;
- import config.subConfig.AxisConfig;
- import config.subConfig.TemplateConfig;
- import constant.Constant;
- import model.Room;
- import model.template.Template;
- import model.template.TemplateClearance;
- 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;
- private static Color[] templateColors = {Color.RED,Color.GREEN,Color.blue,Color.CYAN};
- private static int templateIndex = 0;
- 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(templateColors[templateIndex++%templateColors.length]);
- 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<Integer> 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) {
- TemplateClearance 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<Integer> list = template.getAlignWall();
- List<Integer> newlist = new ArrayList<>();
- for(int i = 0;i<list.size();i++){
- int alignWall = list.get(i);
- alignWall = (alignWall+rotateRange-1)%4+1;
- newlist.add(alignWall);
- }
- template.setAlignWall(newlist);
- }
- /**
- * 平移
- * @param template
- * @param x,y
- */
- private static void moveTemplate(TemplateConfig template, int x, int y) {
- x = x*Constant.ENLARGE/Constant.NARROW;
- y = y*Constant.ENLARGE/Constant.NARROW;
- template.setX(template.getX()+x);
- template.setY(template.getY()+y);
- }
- private static void drawShadow(Template template, Graphics g){
- TemplateClearance shadow = template.getShadow();
- int cx = room.getX()+template.getX()+shadow.getX();
- int cy = room.getY()+template.getY()+shadow.getY();
- System.out.println(template.getName()+"阴影接入点:"+cx+","+cy);
- int x1,y1,x2,y2;
- // g.drawArc(cx,cy,6,6,0,360);
- // if(1==1){
- // return;
- // }
- Graphics2D g2d = (Graphics2D) g.create();
- g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
- g2d.setColor(Color.GRAY);
- int shadowWidth = shadow.getWidth();
- if(shadowWidth < 0){
- shadowWidth = -shadowWidth;
- }
- switch (shadow.getDirect()){
- case 1:
- x1 = cx-shadow.getWidth()/2;
- y1 = cy;
- g2d.drawRect(x1,y1,shadowWidth,shadow.getRadis());
- break;
- case 2:
- x1 = cx;
- y1 = cy - shadowWidth/2;
- g2d.drawRect(x1,y1,shadow.getRadis(),shadowWidth);
- break;
- case 3:
- x1 = cx-shadow.getRadis()/2;
- y1 = cy-shadowWidth;
- g2d.drawRect(x1,y1,shadowWidth,shadow.getRadis());
- break;
- case 4:
- x1 = cx-shadow.getRadis();
- y1 = cy-shadowWidth/2;
- g2d.drawRect(x1,y1,shadow.getRadis(),shadowWidth);
- break;
- default:break;
- }
- g2d.dispose();
- }
- public static boolean checkYiZhiArray(List<TemplateConfig> templateConfigs){
- int templateTotalLength = 0;
- for(TemplateConfig templateConfig : templateConfigs){
- List<Integer> 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<TemplateConfig> templateConfigs,Graphics g){
- List<Template> oneAlignWall = new ArrayList<>();
- List<Template> twoAlignWall = new ArrayList<>();
- getOneAndTwoAlignTemplate(templateConfigs, oneAlignWall, twoAlignWall);
- drawTwoAlignWallTemplates(twoAlignWall,g);
- drawOneAlignWallTemplates(oneAlignWall,g);
- }
- private static void getOneAndTwoAlignTemplate(List<TemplateConfig> templateConfigs, List<Template> oneAlignWall, List<Template> twoAlignWall) {
- for (TemplateConfig templateConfig : templateConfigs){
- Template template = new Template(templateConfig);
- List<Integer> alignWall = template.getAlignWall();
- if(alignWall.size() == 1){
- oneAlignWall.add(template);
- }else if(alignWall.size() == 2){
- twoAlignWall.add(template);
- }
- }
- }
- private static void drawTwoAlignWallTemplates(List<Template> twoAlignWall, Graphics g) {
- int roomAlign = 3;
- int positionOff = 0;
- for(Template template:twoAlignWall){
- int begin = template.getAlignWall().get(0);
- int routeTimes = roomAlign-begin-1;
- if(routeTimes<0){
- routeTimes += 4;
- }
- if(routeTimes!=0){
- routeTemplate(template,routeTimes);
- }
- template.setX(room.getLength());
- template.setY(template.getY()-room.getWidth());
- drawTemplate(template,g);
- }
- }
- public static void drawOneAlignWallTemplates(List<Template> templates,Graphics g){
- int roomAlign = 3;
- int positionOff = 0;
- for (Template template : templates){
- template.setX(0);
- template.setY(0);
- List<Integer> alignWall = template.getAlignWall();
- int templateAlignWall = alignWall.get(0);
- int routeTimes = roomAlign-templateAlignWall%4;
- if(routeTimes<0){
- routeTimes+=4;
- }
- if(routeTimes!=0){
- routeTemplate(template,routeTimes);
- }
- template.setY(template.getY()-room.getWidth());
- if(template.getLength()<0){
- template.setX(template.getX()-template.getLength());
- }
- //放模板后,其他模板需要依次移动positionOff位置
- template.setX(template.getX()+positionOff);
- System.out.println("positionOff 1 = " + positionOff);
- if((templateAlignWall+routeTimes)%2==0){
- positionOff+=Math.abs(template.getWidth());
- }else {
- positionOff+=Math.abs(template.getLength());
- }
- System.out.println("positionOff 2 = " + positionOff);
- drawTemplate(template,g);
- }
- }
- public static void setRoom(Room room) {
- TemplateUtils.room = room;
- }
- public static AxisConfig getAxisPointConfig() {
- return axisPointConfig;
- }
- public static void setAxisPointConfig(AxisConfig axisPointConfig) {
- TemplateUtils.axisPointConfig = axisPointConfig;
- }
- public static void setTemplateIndex(int templateIndex) {
- TemplateUtils.templateIndex = templateIndex;
- }
- /**
- * posintion like [321, 312, 943]
- * @param position
- * @param templateConfigs
- * @param g
- */
- public static void drawTemplateWithArray(List<Integer> position,List<TemplateConfig> templateConfigs,Graphics g){
- List<Template> oneAlignWall = new ArrayList<>();
- List<Template> twoAlignWall = new ArrayList<>();
- getOneAndTwoAlignTemplate(templateConfigs, oneAlignWall, twoAlignWall);
- int twoAlignPosition = position.get(2);
- //画2边贴墙
- drawAlignTwoTemplateWithArrayPosition(g, twoAlignWall, twoAlignPosition);
- }
- private static void drawAlignTwoTemplateWithArrayPosition(Graphics g, List<Template> twoAlignWall, int twoAlignPosition) {
- int wallPointIndex = twoAlignPosition%100/10;
- int alignIndex = twoAlignPosition%10;
- Template twoAlignWallTemplate = twoAlignWall.get(0);
- int templateX = twoAlignWallTemplate.getX();
- int templateY = twoAlignWallTemplate.getY();
- System.out.println("drawAlignTwoTemplateWithArrayPosition 1 ="+templateX+","+templateY);
- switch (wallPointIndex){
- case 2:
- twoAlignWallTemplate.setX(templateX+room.getLength());
- break;
- case 3:
- twoAlignWallTemplate.setX(templateX+room.getLength());
- twoAlignWallTemplate.setY(templateY-room.getWidth());
- break;
- case 4:
- twoAlignWallTemplate.setY(templateY-room.getWidth());
- break;
- }
- System.out.println("drawAlignTwoTemplateWithArrayPosition 2 ="+twoAlignWallTemplate.getX()+","+twoAlignWallTemplate.getY());
- if(alignIndex!=4){
- routeTemplate(twoAlignWallTemplate,alignIndex);
- }
- saveTemplate(twoAlignWallTemplate,g);
- }
- public static void saveTemplate(Template template, Graphics g){
- if(null == room){
- System.out.println("--------------room is not exist !!");
- return;
- }
- saveTemplateName(template,g);
- saveTemplateSelf(template,g);
- saveShadow(template,g);
- }
- private static void saveTemplateName(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 saveTemplateSelf(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(templateColors[templateIndex++%templateColors.length]);
- 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();
- }
- private static void saveShadow(Template template, Graphics g){
- TemplateClearance shadow = template.getShadow();
- int cx = room.getX()+template.getX()+shadow.getX();
- int cy = room.getY()+template.getY()+shadow.getY();
- System.out.println(template.getName()+"阴影接入点:"+cx+","+cy);
- int x1,y1,x2,y2;
- Graphics2D g2d = (Graphics2D) g.create();
- g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
- g2d.setColor(Color.GRAY);
- int shadowWidth = shadow.getWidth();
- if(shadowWidth < 0){
- shadowWidth = -shadowWidth;
- }
- switch (shadow.getDirect()){
- case 1:
- x1 = cx-shadow.getWidth()/2;
- y1 = cy;
- g2d.drawRect(x1,y1,shadowWidth,shadow.getRadis());
- break;
- case 2:
- x1 = cx;
- y1 = cy - shadowWidth/2;
- g2d.drawRect(x1,y1,shadow.getRadis(),shadowWidth);
- break;
- case 3:
- x1 = cx-shadow.getRadis()/2;
- y1 = cy-shadowWidth;
- g2d.drawRect(x1,y1,shadowWidth,shadow.getRadis());
- break;
- case 4:
- x1 = cx-shadow.getRadis();
- y1 = cy-shadowWidth/2;
- g2d.drawRect(x1,y1,shadow.getRadis(),shadowWidth);
- break;
- default:break;
- }
- g2d.dispose();
- }
- }
|