Browse Source

开始平移与旋转

duh 6 years ago
parent
commit
bcf5511be7

+ 11 - 0
src/main/java/config/subConfig/TemplateConfig.java

@@ -2,6 +2,8 @@ package config.subConfig;
 
 import config.common.TemplateInsert;
 
+import java.util.List;
+
 /**
  * @author duh
  * @create 2018/7/17 20:04
@@ -11,6 +13,7 @@ public class TemplateConfig extends TemplateInsert{
     private String name;
     private int length;
     private int width;
+    private List<Integer> alignWall;
     private TemplateShadowConfig shadow;
 
     public String getName() {
@@ -44,4 +47,12 @@ public class TemplateConfig extends TemplateInsert{
     public void setShadow(TemplateShadowConfig shadow) {
         this.shadow = shadow;
     }
+
+    public List<Integer> getAlignWall() {
+        return alignWall;
+    }
+
+    public void setAlignWall(List<Integer> alignWall) {
+        this.alignWall = alignWall;
+    }
 }

+ 3 - 1
src/main/java/constant/Constant.java

@@ -6,5 +6,7 @@ package constant;
  * @email duh@elab-plus.com
  **/
 public class Constant {
-    public static int CLOSE_WALL_LINE_WIDTH = 6;
+    public static final int CLOSE_WALL_LINE_WIDTH = 6;
+    public static int ENLARGE = 1;
+    public static int NARROW = 1;
 }

+ 5 - 0
src/main/java/controller/MyFrame.java

@@ -2,6 +2,7 @@ package controller;
 
 import config.Config;
 import config.subConfig.AxisConfig;
+import constant.Constant;
 import model.ModuleInAxis;
 import utils.YmlUtils;
 
@@ -24,6 +25,10 @@ public class MyFrame extends JFrame {
     private void initFrame() {
         // 设置 窗口标题 和 窗口大小
         setTitle(TITLE);
+        //将缩放值存入Constant
+        Constant.ENLARGE = template.getEnlarge();
+        Constant.NARROW = template.getNarrow();
+
         AxisConfig axisConfig = template.getAxisPoint();
         int lenght = axisConfig.getLength()+axisConfig.getX()+100;
         int width = axisConfig.getY()+100;

+ 4 - 4
src/main/java/controller/MyPanel.java

@@ -36,8 +36,8 @@ public class MyPanel extends JPanel{
         drawAxis(config,g);
         Room room = new Room(config.getRoom());
         drawRoom(room,g);
-//        drawTemplate(room,g);
-        drawTemplateAndMove(room,g);
+        drawTemplate(room,g);
+//        drawTemplateAndMove(room,g);
     }
 
     /**
@@ -61,6 +61,7 @@ public class MyPanel extends JPanel{
     private void drawTemplate(Room room, Graphics g) {
         List<TemplateConfig> templateList = config.getTemplates();
         TemplateUtils.setRoom(room);
+        TemplateUtils.checkYiZhiArray(templateList);
         for(TemplateConfig templateConfig : templateList){
             TemplateUtils.drawTemplate(templateConfig,g);
         }
@@ -68,11 +69,10 @@ public class MyPanel extends JPanel{
     private void drawTemplateAndMove(Room room, Graphics g) {
         List<TemplateConfig> templateList = config.getTemplates();
         TemplateUtils.setRoom(room);
-        TemplateConfig templateConfig = templateList.get(0);
+        TemplateConfig templateConfig = templateList.get(1);
         TemplateUtils.drawTemplate(templateConfig,g);
     }
     private void drawRoom(Room room, Graphics g){
-
         room.drawSelf(g);
         DoorConfig doorConfig = config.getDoor();
         Door door = new Door();

+ 9 - 0
src/main/java/model/template/Template.java

@@ -0,0 +1,9 @@
+package model.template;
+
+/**
+ * @author duh
+ * @create 2018/7/20 20:12
+ * @email duh@elab-plus.com
+ **/
+public class Template {
+}

+ 104 - 5
src/main/java/utils/TemplateUtils.java

@@ -1,12 +1,14 @@
 package utils;
 
 import config.subConfig.AxisConfig;
-import config.subConfig.RoomConfig;
 import config.subConfig.TemplateConfig;
 import config.subConfig.TemplateShadowConfig;
+import constant.Constant;
 import model.Room;
 
 import java.awt.*;
+import java.util.ArrayList;
+import java.util.List;
 
 /**
  * @author duh
@@ -23,7 +25,7 @@ public class TemplateUtils {
         }
         drawTemplateName(template,g);
         drawTemplateSelf(template,g);
-        drawShadow(template,g);
+//        drawShadow(template,g);
     }
 
     private static void drawTemplateName(TemplateConfig template, Graphics g) {
@@ -33,16 +35,89 @@ public class TemplateUtils {
     }
 
     private static void drawTemplateSelf(TemplateConfig template, Graphics g){
+        int length = template.getLength();
+        int width = template.getWidth();
         int x1 = room.getX()+template.getX();
         int y1 = room.getY()+template.getY();
-        int x2 = x1+template.getLength();
-        int y2 = y1+template.getWidth();
+        int x2 = x1+length;
+        int y2 = y1+width;
         Graphics2D g2d = (Graphics2D) g.create();
         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
         g2d.setColor(Color.RED);
-        g2d.drawRect(x1,y1,template.getLength(),template.getWidth());
+        g2d.drawRect(x1,y1,length,width);
+        //绘制贴墙边
+        List<Integer> alignWall = template.getAlignWall();
+        for (int alignWallValue : alignWall){
+            switch (alignWallValue){
+                case 1:
+                    g2d.fillRect(x1,y1+ 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,length,Constant.CLOSE_WALL_LINE_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;
+            }
+        }
         g2d.dispose();
     }
+
+    /**
+     * 绕接入点逆时针旋转rotateRange度 目前支持90的倍数 0:0 1:90 2:180 3:270
+     * @param template
+     * @param g
+     * @param rotateRange
+     */
+    private static void drawTemplateSelfByRotate(TemplateConfig template, Graphics g,int rotateRange){
+        routeTemplateConfig(template,rotateRange);
+    }
+
+    /**
+     * 旋转
+     * @param template
+     * @param rotateRange
+     */
+    private static void routeTemplateConfig(TemplateConfig template, int rotateRange) {
+        int length = template.getLength();
+        int width = template.getWidth();
+        changeTemplateAlignWall(template,rotateRange);
+        switch (rotateRange){
+            case 1:
+                template.setY(template.getY()-length);
+                template.setLength(width);
+                template.setWidth(length);
+                break;
+            case 2:
+                template.setX(template.getX()-length);
+                template.setY(template.getY()-width);
+                break;
+        }
+    }
+    public static void changeTemplateAlignWall(TemplateConfig 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%4 + 1;
+            newlist.add(alignWall);
+        }
+        template.setAlignWall(newlist);
+    }
+    /**
+     * 平移
+     * @param template
+     * @param x,y
+     */
+    private static void routeTemplateConfig(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(TemplateConfig template, Graphics g){
         TemplateShadowConfig shadow = template.getShadow();
         int cx = room.getX()+shadow.getX();
@@ -71,6 +146,30 @@ public class TemplateUtils {
         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 drawTemplates(List<TemplateConfig> templateConfigs){
+//        for
+    }
     public static void setRoom(Room room) {
         TemplateUtils.room = room;
     }

+ 11 - 1
src/main/resources/config.yml

@@ -34,6 +34,9 @@ templates:
  - name: XS
    length: 550
    width: 1000
+   x: 0
+   y: 0
+   alignWall: [4]
    shadow:
     x: 550
     y: 500
@@ -41,9 +44,13 @@ templates:
     direct: 0
     radis: 800
 #浴盆
- - name: YP
+ - name: PY
    length: 1500
    width: 800
+   x: 2800
+   y: 0
+   #1:bottom 2:right 3:top 4:left
+   alignWall: [3,4]
    shadow:
     x: 1025
     y: 800
@@ -54,6 +61,9 @@ templates:
  - name: MT
    length: 700
    width: 850
+   x: 2000
+   y: 0
+   alignWall: [4]
    shadow:
     x: 700
     y: 425