Browse Source

绘制的图形导出到jpg中

duh 6 years ago
parent
commit
e303820f0b

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

@@ -10,8 +10,12 @@ import model.Room;
 import model.template.Template;
 import utils.TemplateUtils;
 
+import javax.imageio.ImageIO;
 import javax.swing.*;
 import java.awt.*;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.IOException;
 import java.util.List;
 
 /**
@@ -32,11 +36,32 @@ 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("jpanel.jpg"));
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+
+    private void drawContent(Graphics g) {
         drawAxis(config,g);
         Room room = new Room(config.getRoom());
         drawRoom(room,g);
-//        drawTemplate(room,g);
-        drawTemplateAndMove(room,g);
+        drawTemplate(room,g);
+        TemplateUtils.setTemplateIndex(0);
     }
 
     /**
@@ -65,10 +90,10 @@ public class MyPanel extends JPanel{
         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();

+ 4 - 7
src/main/java/model/template/Template.java

@@ -1,10 +1,7 @@
 package model.template;
 
 import config.subConfig.TemplateConfig;
-import config.subConfig.TemplateShadowConfig;
-import model.ModuleInAxis;
 
-import java.awt.*;
 import java.util.List;
 
 /**
@@ -16,7 +13,7 @@ public class Template extends ModuleInRoom {
     private int length;
     private int width;
     private List<Integer> alignWall;
-    private TemplateShadow shadow;
+    private TemplateClearance shadow;
 
     public Template(TemplateConfig templateConfig) {
         this.name = templateConfig.getName();
@@ -25,7 +22,7 @@ public class Template extends ModuleInRoom {
         alignWall = templateConfig.getAlignWall();
         x = templateConfig.getX();
         y = templateConfig.getY();
-        shadow = new TemplateShadow(this,templateConfig.getShadow());
+        shadow = new TemplateClearance(this,templateConfig.getShadow());
     }
 
     public int getLength() {
@@ -52,11 +49,11 @@ public class Template extends ModuleInRoom {
         this.alignWall = alignWall;
     }
 
-    public TemplateShadow getShadow() {
+    public TemplateClearance getShadow() {
         return shadow;
     }
 
-    public void setShadow(TemplateShadow shadow) {
+    public void setShadow(TemplateClearance shadow) {
         this.shadow = shadow;
     }
 }

+ 6 - 2
src/main/java/model/template/TemplateShadow.java

@@ -10,7 +10,11 @@ import java.util.List;
  * @create 2018/7/21 15:16
  * @email duh@elab-plus.com
  **/
-public class TemplateShadow {
+
+/**
+ * 模块使用空间
+ */
+public class TemplateClearance {
     private Template template;
     private int x;
     private int y;
@@ -19,7 +23,7 @@ public class TemplateShadow {
     private int radis;
     private List<Point> points;
 
-    public TemplateShadow(Template template, TemplateShadowConfig config) {
+    public TemplateClearance(Template template, TemplateShadowConfig config) {
         this.template = template;
         if(null != config){
             x = config.getX();

+ 16 - 8
src/main/java/utils/TemplateUtils.java

@@ -5,7 +5,7 @@ import config.subConfig.TemplateConfig;
 import constant.Constant;
 import model.Room;
 import model.template.Template;
-import model.template.TemplateShadow;
+import model.template.TemplateClearance;
 
 import java.awt.*;
 import java.util.ArrayList;
@@ -19,6 +19,8 @@ import java.util.List;
 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 !!");
@@ -43,7 +45,7 @@ public class TemplateUtils {
 
         Graphics2D g2d = (Graphics2D) g.create();
         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
-        g2d.setColor(Color.RED);
+        g2d.setColor(templateColors[templateIndex++%templateColors.length]);
         if(length < 0){
             length=-length;
             x1 = x1-length;
@@ -125,7 +127,7 @@ public class TemplateUtils {
     }
 
     public static void routeTemplateShadow(Template template, int rotateRange) {
-        TemplateShadow shadow = template.getShadow();
+        TemplateClearance shadow = template.getShadow();
         int routeX = template.getX();
         int routeY = template.getY();
         int shadowX = shadow.getX();
@@ -169,7 +171,7 @@ public class TemplateUtils {
         template.setY(template.getY()+y);
     }
     private static void drawShadow(Template template, Graphics g){
-        TemplateShadow shadow = template.getShadow();
+        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);
@@ -189,22 +191,22 @@ public class TemplateUtils {
             case 1:
                 x1 = cx-shadow.getWidth()/2;
                 y1 = cy;
-                g2d.fillRect(x1,y1,shadowWidth,shadow.getRadis());
+                g2d.drawRect(x1,y1,shadowWidth,shadow.getRadis());
                 break;
             case 2:
                 x1 = cx;
                 y1 = cy - shadowWidth/2;
-                g2d.fillRect(x1,y1,shadow.getRadis(),shadowWidth);
+                g2d.drawRect(x1,y1,shadow.getRadis(),shadowWidth);
                 break;
             case 3:
                 x1 = cx-shadow.getRadis()/2;
                 y1 = cy-shadowWidth;
-                g2d.fillRect(x1,y1,shadowWidth,shadow.getRadis());
+                g2d.drawRect(x1,y1,shadowWidth,shadow.getRadis());
                 break;
             case 4:
                 x1 = cx-shadow.getRadis();
                 y1 = cy-shadowWidth/2;
-                g2d.fillRect(x1,y1,shadow.getRadis(),shadowWidth);
+                g2d.drawRect(x1,y1,shadow.getRadis(),shadowWidth);
                 break;
             default:break;
         }
@@ -270,6 +272,8 @@ public class TemplateUtils {
         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;
@@ -306,4 +310,8 @@ public class TemplateUtils {
     public static void setAxisPointConfig(AxisConfig axisPointConfig) {
         TemplateUtils.axisPointConfig = axisPointConfig;
     }
+
+    public static void setTemplateIndex(int templateIndex) {
+        TemplateUtils.templateIndex = templateIndex;
+    }
 }

+ 10 - 10
src/main/resources/config.yml

@@ -1,5 +1,5 @@
 enlarge: 1
-narrow: 10
+narrow: 6
 axisPoint:
   x: 200
   y: 2500
@@ -11,8 +11,8 @@ room:
   length: 2800
   width: 2000
   #相对于坐标轴接入点
-  x: 20
-  y: 20
+  x: 100
+  y: 100
 #门
 door:
   #相对于坐标轴接入点
@@ -33,14 +33,14 @@ door:
 templates:
  - name: XS
    length: 550
-   width: 1000
+   width: 900
    x: 0
    y: 0
    alignWall: [4]
    shadow:
     x: 550
-    y: 500
-    width: 1000
+    y: 450
+    width: 900
     #1:bottom 2:right 3:top 4:left 贴边向外,小于0向内
     direct: 2
     radis: 800
@@ -61,14 +61,14 @@ templates:
 #马桶
  - name: MT
    length: 700
-   width: 850
-   x: 0
+   width: 900
+   x: 700
    y: 0
    alignWall: [4]
    shadow:
     x: 700
-    y: 425
-    width: 800
+    y: 450
+    width: 900
     #数值等于坐标轴的朝向
     direct: 2
     radis: 500