Browse Source

画图与显示分离

duh 6 years ago
parent
commit
7760ff19a9

+ 98 - 0
src/main/java/controller/DrawImageController.java

@@ -0,0 +1,98 @@
+package controller;
+
+import config.Config;
+import config.subConfig.AxisConfig;
+import config.subConfig.DoorConfig;
+import config.subConfig.TemplateConfig;
+import model.Axis;
+import model.Door;
+import model.ModuleInAxis;
+import model.Room;
+import utils.TemplateUtils;
+import utils.YmlUtils;
+
+import javax.imageio.ImageIO;
+import java.awt.*;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author duh
+ * @create 2018/7/27 13:30
+ * @email duh@elab-plus.com
+ **/
+public class DrawImageController {
+    private static Config config = YmlUtils.readYml();
+    /**
+     * 画在panel中
+     */
+    public static void saveImage(List<Integer> position) {
+        AxisConfig axisConfig = config.getAxisPoint();
+        int length = axisConfig.getLength() + axisConfig.getX() + 300;
+        int width = axisConfig.getY() + 300;
+        BufferedImage bufferedImage = new BufferedImage(length,
+                width, BufferedImage.TYPE_INT_RGB);
+        Graphics2D g2d = bufferedImage.createGraphics();
+        drawImageContent(position, g2d);
+        try {
+            ImageIO.write(bufferedImage, "jpeg", new File("E:\\config\\jpanel" + position.get(0) + position.get(1) + position.get(2) + ".jpg"));
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+    private static void drawImageContent(List<Integer> position, Graphics g) {
+        ModuleInAxis.setAxisPointConfig(config.getAxisPoint());
+        drawAxis(config, g);
+        Room room = new Room(config.getRoom());
+        drawRoom(room, g);
+        drawImageTemplate(room, position, g);
+        TemplateUtils.setTemplateIndex(0);
+    }
+    /**
+     * 画坐标系
+     *
+     * @param config
+     * @param g
+     */
+    private static void drawAxis(Config config, Graphics g) {
+        Axis axis = new Axis();
+        AxisConfig axisConfig = config.getAxisPoint();
+        //ModuleInAxis已设置坐标轴的偏转
+        axis.setX(0);
+        axis.setY(0);
+        axis.setxLength(axisConfig.getLength());
+        axis.setyLength(axisConfig.getWidth());
+        axis.setxSpacing(axisConfig.getxSpacing());
+        axis.setySpacing(axisConfig.getySpacing());
+//        System.out.println("axis3 x="+axis.getX());
+        axis.drawSelf(g);
+    }
+    private static void drawRoom(Room room, Graphics g) {
+        room.drawSelf(g);
+        DoorConfig doorConfig = config.getDoor();
+        Door door = new Door();
+        door.setConfig(doorConfig);
+        door.setRoom(room);
+        door.drawSelf(g);
+    }
+    private static void drawImageTemplate(Room room, List<Integer> position, Graphics g) {
+        List<TemplateConfig> templateList = config.getTemplates();
+        TemplateUtils.setRoom(room);
+        boolean canYizhi = TemplateUtils.checkYiZhiArray(templateList);
+        System.out.println("can yi zhi =" + canYizhi);
+        if (canYizhi) {
+            TemplateUtils.drawTemplateWithArray(position, templateList, g);
+        }
+    }
+    public static void main(String[] args) {
+        //
+        List<Integer> position = new ArrayList<>();
+        position.add(321);
+        position.add(312);
+        position.add(932);
+        saveImage(position);
+    }
+}

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

@@ -8,6 +8,8 @@ import utils.YmlUtils;
 
 import javax.swing.*;
 import java.awt.*;
+import java.util.ArrayList;
+import java.util.List;
 
 /**
  * @author duh
@@ -42,6 +44,7 @@ public class MyFrame extends JFrame {
         // 设置窗口的内容面板
         MyPanel panel = new MyPanel(this, template);
         setContentPane(panel);
+
     }
     private void setAxisForTemplate(){
         ModuleInAxis.setAxisPointConfig(template.getAxisPoint());

+ 27 - 31
src/main/java/controller/MyPanel.java

@@ -16,6 +16,7 @@ import java.awt.*;
 import java.awt.image.BufferedImage;
 import java.io.File;
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.List;
 
 /**
@@ -23,7 +24,7 @@ import java.util.List;
  * @create 2018/7/17 16:01
  * @email duh@elab-plus.com
  **/
-public class MyPanel extends JPanel{
+public class MyPanel extends JPanel {
     private MyFrame frame;
     private Config config;
 
@@ -36,40 +37,30 @@ public class MyPanel extends JPanel{
     @Override
     protected void paintComponent(Graphics g) {
         super.paintComponent(g);
-        AxisConfig axisConfig = config.getAxisPoint();
-        int lenght = axisConfig.getLength()+axisConfig.getX()+100;
-        int width = axisConfig.getY()+100;
-        BufferedImage bufferedImage = new BufferedImage(lenght,
-                width, BufferedImage.TYPE_INT_RGB);
-        Graphics2D g2d = bufferedImage.createGraphics();
         //画在panel中
         drawContent(g);
-        //保存在图片中
-        drawContent(g2d);
-
 //        drawTemplateAndMove(room,g);
-
-        try {
-            ImageIO.write(bufferedImage, "jpeg", new File("E:\\config\\jpanel.jpg"));
-        } catch (IOException e) {
-            e.printStackTrace();
-        }
     }
 
+
+
     private void drawContent(Graphics g) {
-        drawAxis(config,g);
+        drawAxis(config, g);
         Room room = new Room(config.getRoom());
-        drawRoom(room,g);
-        drawTemplate(room,g);
+        drawRoom(room, g);
+        drawTemplate(room, g);
         TemplateUtils.setTemplateIndex(0);
     }
 
+
+
     /**
      * 画坐标系
+     *
      * @param config
      * @param g
      */
-    private void drawAxis(Config config,Graphics g){
+    private void drawAxis(Config config, Graphics g) {
         Axis axis = new Axis();
         AxisConfig axisConfig = config.getAxisPoint();
         //ModuleInAxis已设置坐标轴的偏转
@@ -82,29 +73,34 @@ public class MyPanel extends JPanel{
 //        System.out.println("axis3 x="+axis.getX());
         axis.drawSelf(g);
     }
+
     private void drawTemplate(Room room, Graphics g) {
         List<TemplateConfig> templateList = config.getTemplates();
         TemplateUtils.setRoom(room);
         boolean canYizhi = TemplateUtils.checkYiZhiArray(templateList);
-        System.out.println("can yi zhi ="+canYizhi);
-        if(canYizhi){
-            TemplateUtils.calcYizhiTemplatesAndDraw(templateList,g);
+        System.out.println("can yi zhi =" + canYizhi);
+        if (canYizhi) {
+            TemplateUtils.calcYizhiTemplatesAndDraw(templateList, g);
+        }
+        for(TemplateConfig templateConfig : templateList){
+            Template template = new Template(templateConfig);
+            TemplateUtils.drawTemplate(template,g);
         }
-//        for(TemplateConfig templateConfig : templateList){
-//            Template template = new Template(templateConfig);
-//            TemplateUtils.drawTemplate(template,g);
-//        }
     }
+
+
+
     private void drawTemplateAndMove(Room room, Graphics g) {
         List<TemplateConfig> templateList = config.getTemplates();
         TemplateUtils.setRoom(room);
         TemplateConfig templateConfig = templateList.get(1);
         Template template = new Template(templateConfig);
-        TemplateUtils.drawTemplate(template,g);
-        TemplateUtils.routeTemplate(template,3);
-        TemplateUtils.drawTemplate(template,g);
+        TemplateUtils.drawTemplate(template, g);
+        TemplateUtils.routeTemplate(template, 3);
+        TemplateUtils.drawTemplate(template, g);
     }
-    private void drawRoom(Room room, Graphics g){
+
+    private void drawRoom(Room room, Graphics g) {
         room.drawSelf(g);
         DoorConfig doorConfig = config.getDoor();
         Door door = new Door();

+ 121 - 2
src/main/java/utils/TemplateUtils.java

@@ -237,6 +237,12 @@ public class TemplateUtils {
     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();
@@ -246,8 +252,6 @@ public class TemplateUtils {
                 twoAlignWall.add(template);
             }
         }
-        drawTwoAlignWallTemplates(twoAlignWall,g);
-        drawOneAlignWallTemplates(oneAlignWall,g);
     }
 
     private static void drawTwoAlignWallTemplates(List<Template> twoAlignWall, Graphics g) {
@@ -314,4 +318,119 @@ public class TemplateUtils {
     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();
+    }
 }