Browse Source

画坐标系

duh 6 years ago
parent
commit
138233fa6a

+ 10 - 0
src/main/java/config/Config.java

@@ -1,5 +1,6 @@
 package config;
 
+import config.subConfig.AxisConfig;
 import config.subConfig.DoorConfig;
 import config.subConfig.RoomConfig;
 import config.subConfig.TemplateConfig;
@@ -12,6 +13,7 @@ import java.util.List;
  * @email duh@elab-plus.com
  **/
 public class Config {
+    private AxisConfig axisPoint;
     private int enlarge;
     private int narrow;
     private RoomConfig room;
@@ -19,6 +21,14 @@ public class Config {
     private List<TemplateConfig> templates;
     private int zoom;
 
+    public AxisConfig getAxisPoint() {
+        return axisPoint;
+    }
+
+    public void setAxisPoint(AxisConfig axisPoint) {
+        this.axisPoint = axisPoint;
+    }
+
     public int getEnlarge() {
         return enlarge;
     }

+ 63 - 0
src/main/java/config/subConfig/AxisConfig.java

@@ -0,0 +1,63 @@
+package config.subConfig;
+
+/**
+ * @author duh
+ * @create 2018/7/18 15:50
+ * @email duh@elab-plus.com
+ **/
+public class AxisConfig {
+    private int x;
+    private int y;
+    private int length;
+    private int width;
+    private int xSpacing;
+    private int ySpacing;
+
+    public int getX() {
+        return x;
+    }
+
+    public void setX(int x) {
+        this.x = x;
+    }
+
+    public int getY() {
+        return y;
+    }
+
+    public void setY(int y) {
+        this.y = y;
+    }
+
+    public int getLength() {
+        return length;
+    }
+
+    public void setLength(int length) {
+        this.length = length;
+    }
+
+    public int getWidth() {
+        return width;
+    }
+
+    public void setWidth(int width) {
+        this.width = width;
+    }
+
+    public int getxSpacing() {
+        return xSpacing;
+    }
+
+    public void setxSpacing(int xSpacing) {
+        this.xSpacing = xSpacing;
+    }
+
+    public int getySpacing() {
+        return ySpacing;
+    }
+
+    public void setySpacing(int ySpacing) {
+        this.ySpacing = ySpacing;
+    }
+}

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

@@ -1,6 +1,7 @@
 package controller;
 
 import config.Config;
+import model.ModuleInAxis;
 import utils.YmlUtils;
 
 import javax.swing.*;
@@ -23,6 +24,8 @@ public class MyFrame extends JFrame {
         // 设置 窗口标题 和 窗口大小
         setTitle(TITLE);
         setSize(template.getRoom().getWidth()+100, template.getRoom().getLength()+100);
+        setAxisForTemplate();
+
         // 设置窗口关闭按钮的默认操作(点击关闭时退出进程)
         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
         // 把窗口位置设置到屏幕的中心
@@ -31,6 +34,9 @@ public class MyFrame extends JFrame {
         MyPanel panel = new MyPanel(this, template);
         setContentPane(panel);
     }
+    private void setAxisForTemplate(){
+        ModuleInAxis.setAxisPointConfig(template.getAxisPoint());
+    }
 
     public static void main(String[] args) throws Exception {
         EventQueue.invokeLater(new Runnable() {

+ 22 - 7
src/main/java/controller/MyPanel.java

@@ -1,9 +1,11 @@
 package controller;
 
 import config.Config;
+import config.subConfig.AxisConfig;
 import config.subConfig.DoorConfig;
 import config.subConfig.RoomConfig;
 import config.subConfig.TemplateConfig;
+import model.Axis;
 import model.Door;
 import model.Room;
 import utils.TemplateUtils;
@@ -20,12 +22,12 @@ import java.util.List;
  **/
 public class MyPanel extends JPanel{
     private MyFrame frame;
-    private Config templateConfig;
+    private Config config;
 
-    public MyPanel(MyFrame frame, Config templateConfig) {
+    public MyPanel(MyFrame frame, Config config) {
         super();
         this.frame = frame;
-        this.templateConfig = templateConfig;
+        this.config = config;
     }
 
     @Override
@@ -35,23 +37,36 @@ public class MyPanel extends JPanel{
 //        drawRect(g);
 //        drawImage(g);
 //        drawArc(g);
+        drawAxis(config,g);
         drawRoom(g);
     }
+    private void drawAxis(Config config,Graphics g){
+        Axis axis = new Axis();
+        AxisConfig axisConfig = config.getAxisPoint();
+        axis.setX(axisConfig.getX());
+        axis.setY(axisConfig.getY());
+        axis.setxLength(axisConfig.getLength());
+        axis.setyLength(axisConfig.getWidth());
+        axis.setxSpacing(axisConfig.getxSpacing());
+        axis.setySpacing(axisConfig.getySpacing());
+        axis.drawSelf(g);
+    }
     private void drawTemplate(RoomConfig room, Graphics g) {
-        List<TemplateConfig> templateList = templateConfig.getTemplates();
+        List<TemplateConfig> templateList = config.getTemplates();
         TemplateUtils.setRoom(room);
         for(TemplateConfig templateConfig : templateList){
             TemplateUtils.drawTemplate(templateConfig,g);
         }
     }
     private void drawRoom(Graphics g){
-        Room room = new Room(templateConfig.getRoom());
+        Room room = new Room(config.getRoom());
         room.drawSelf(g);
-        DoorConfig doorConfig = templateConfig.getDoor();
+        DoorConfig doorConfig = config.getDoor();
         Door door = new Door();
         door.setConfig(doorConfig);
+        door.setRoom(config.getRoom());
         door.drawSelf(g);
-        drawTemplate(templateConfig.getRoom(),g);
+//        drawTemplate(templateConfig.getRoom(),g);
     }
     /**
      * 1. 线段 / 折线

+ 74 - 0
src/main/java/model/Axis.java

@@ -0,0 +1,74 @@
+package model;
+
+import java.awt.*;
+
+/**
+ * @author duh
+ * @create 2018/7/18 18:58
+ * @email duh@elab-plus.com
+ **/
+public class Axis  extends ModuleInAxis {
+    private int xLength;
+    private int yLength;
+    private int xSpacing;
+    private int ySpacing;
+    private int spacingLength = 5;
+    @Override
+    public void drawSelf(Graphics g) {
+        System.out.println("begin to draw axis");
+        Graphics2D g2d = (Graphics2D) g.create();
+        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
+        g2d.setColor(Color.RED);
+        int axisX = x;
+        int axisY = y;
+        g2d.drawString("0",axisX,axisY+10);
+        drawXAxis(axisX,axisY,xLength,g2d);
+        drawYAxis(axisX,axisY,yLength,g2d);
+        g2d.dispose();
+    }
+    private void drawXAxis(int x1, int y1, int length, Graphics2D g2d){
+        g2d.drawLine(x1,y1,x1+length,y1);
+        int xNumber = length/xSpacing;
+        for(int i = 1;i<xNumber;i++){
+            g2d.drawLine(x1+i*xSpacing,y1,x1+i*xSpacing,y1+spacingLength);
+        }
+    }
+    private void drawYAxis(int x1, int y1, int length, Graphics2D g2d){
+        g2d.drawLine(x1,y1,x1,y1-length);
+        int yNumber = length/ySpacing;
+        for(int i = 1;i<yNumber;i++){
+            g2d.drawLine(x1,y1-i*ySpacing,x1-spacingLength,y1-i*ySpacing);
+        }
+    }
+    public int getxLength() {
+        return xLength;
+    }
+
+    public void setxLength(int xLength) {
+        this.xLength = xLength;
+    }
+
+    public int getyLength() {
+        return yLength;
+    }
+
+    public void setyLength(int yLength) {
+        this.yLength = yLength;
+    }
+
+    public int getxSpacing() {
+        return xSpacing;
+    }
+
+    public void setxSpacing(int xSpacing) {
+        this.xSpacing = xSpacing;
+    }
+
+    public int getySpacing() {
+        return ySpacing;
+    }
+
+    public void setySpacing(int ySpacing) {
+        this.ySpacing = ySpacing;
+    }
+}

+ 20 - 4
src/main/java/model/Door.java

@@ -1,6 +1,7 @@
 package model;
 
 import config.subConfig.DoorConfig;
+import config.subConfig.RoomConfig;
 
 import java.awt.*;
 
@@ -9,10 +10,15 @@ import java.awt.*;
  * @create 2018/7/18 13:46
  * @email duh@elab-plus.com
  **/
-public class Door extends Template{
+public class Door extends ModuleInAxis {
     private DoorConfig config;
+    private RoomConfig room;
     @Override
     public void drawSelf(Graphics g) {
+        if(null == room || null == config){
+            System.out.println("room is null or config is null");
+            return;
+        }
         // 1. 绘制一条圆弧: 椭圆的外切矩形 左上角坐标为(0, 0), 宽100, 高100,
         //                弧的开始角度为0度, 需要绘制的角度数为-90度,
         //                椭圆右边水平线为0度, 逆时针为正角度, 顺时针为负角度
@@ -20,11 +26,13 @@ public class Door extends Template{
         Graphics2D g2d = (Graphics2D) g.create();
         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
         g2d.setColor(Color.RED);
-        g2d.drawArc(config.getX(), config.getY(), config.getRadis(), config.getRadis(), 0, 90*config.getOpenDir());
+        int x = room.getX()+config.getX();
+        int y = room.getY()+config.getY();
+        g2d.drawArc(x, y, config.getRadis(), config.getRadis(), 0, 90*config.getOpenDir());
         g2d.setColor(Color.GRAY);
         // 3. 填充一个扇形
-        g2d.fillArc(config.getX(), config.getY(), config.getRadis(), config.getRadis(), 0, 90*config.getOpenDir());
-
+        g2d.fillArc(x, y, config.getRadis(), config.getRadis(), 0, 90*config.getOpenDir());
+        g2d.drawString(x+","+y,x-10,y+10);
         g2d.dispose();
     }
 
@@ -36,4 +44,12 @@ public class Door extends Template{
     public void setConfig(DoorConfig config) {
         this.config = config;
     }
+
+    public RoomConfig getRoom() {
+        return room;
+    }
+
+    public void setRoom(RoomConfig room) {
+        this.room = room;
+    }
 }

+ 15 - 3
src/main/java/model/Template.java

@@ -1,13 +1,17 @@
 package model;
 
+import config.subConfig.AxisConfig;
+
 import java.awt.*;
 
 /**
+ * 坐标系中物体
  * @author duh
  * @create 2018/7/17 15:00
  * @email duh@elab-plus.com
  **/
-public abstract class Template {
+public abstract class ModuleInAxis {
+    protected static AxisConfig axisPointConfig;
     protected String name;
     //x坐标
     protected int x;
@@ -16,6 +20,14 @@ public abstract class Template {
     //旋转角度
     protected int scale;
 
+    public static AxisConfig getAxisPointConfig() {
+        return axisPointConfig;
+    }
+
+    public static void setAxisPointConfig(AxisConfig axisPointConfig) {
+        ModuleInAxis.axisPointConfig = axisPointConfig;
+    }
+
     public String getName() {
         return name;
     }
@@ -29,7 +41,7 @@ public abstract class Template {
     }
 
     public void setX(int x) {
-        this.x = x;
+        this.x = x+axisPointConfig.getX();
     }
 
     public int getY() {
@@ -37,7 +49,7 @@ public abstract class Template {
     }
 
     public void setY(int y) {
-        this.y = y;
+        this.y = y+axisPointConfig.getY();
     }
 
     public int getScale() {

+ 2 - 1
src/main/java/model/Room.java

@@ -9,7 +9,7 @@ import java.awt.*;
  * @create 2018/7/17 16:19
  * @email duh@elab-plus.com
  **/
-public class Room extends Template{
+public class Room extends ModuleInAxis {
     private int length;
     private int width;
     public Room(){
@@ -28,6 +28,7 @@ public class Room extends Template{
         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
         g2d.setColor(Color.GRAY);
         // 1. 绘制一个矩形: 起点(30, 20), 宽80, 高100
+        g2d.drawString(x+","+y,x,y);
         g2d.drawRect(x, y, width, length);
         g2d.dispose();
     }

+ 3 - 4
src/main/java/utils/AxisUtils.java

@@ -22,12 +22,12 @@ public class AxisUtils {
     public static void drawAxis(AxisBorder border ,Axis xaxis, Axis yaxis, int width, int height, Graphics2D g){
         //抗锯齿 字体更平滑
         g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
-        drawBorder(border, width, height, g);
+//        drawBorder(border, width, height, g);
         drawXAxis(border, xaxis, width, height, g);
         drawYAxis(border, yaxis, width, height, g);
         //设置作图区域颜色为白色
-        g.setColor(new Color(0xFFFFFF));
-        g.fillRect(border.getLeft(), border.getTop(), width, height);
+//        g.setColor(new Color(0xFFFFFF));
+//        g.fillRect(border.getLeft(), border.getTop(), width, height);
     }
 
     /**
@@ -55,7 +55,6 @@ public class AxisUtils {
             point1 = new Point(x + skips * i,y1);
             point2 = new Point(x + skips * i,y2);
             drawLine(point1, point2, new Color(xaxis.getLineColor()) , xaxis.getLineWidth(), g);
-
         }
         //画label
         if(type.equals(Axis.VALUE)){

+ 10 - 0
src/main/java/utils/TemplateUtils.java

@@ -1,5 +1,6 @@
 package utils;
 
+import config.subConfig.AxisConfig;
 import config.subConfig.RoomConfig;
 import config.subConfig.TemplateConfig;
 import config.subConfig.TemplateShadowConfig;
@@ -13,6 +14,7 @@ import java.awt.*;
  **/
 public class TemplateUtils {
     private static RoomConfig room;
+    private static AxisConfig axisPointConfig;
 
     public static void drawTemplate(TemplateConfig template, Graphics g){
         if(null == room){
@@ -77,4 +79,12 @@ public class TemplateUtils {
     public static void setRoom(RoomConfig room) {
         TemplateUtils.room = room;
     }
+
+    public static AxisConfig getAxisPointConfig() {
+        return axisPointConfig;
+    }
+
+    public static void setAxisPointConfig(AxisConfig axisPointConfig) {
+        TemplateUtils.axisPointConfig = axisPointConfig;
+    }
 }

+ 12 - 1
src/main/java/utils/YmlUtils.java

@@ -3,7 +3,6 @@ package utils;
 import com.google.gson.Gson;
 import config.Config;
 import config.subConfig.*;
-import org.omg.IOP.ENCODING_CDR_ENCAPS;
 import org.yaml.snakeyaml.Yaml;
 
 import java.io.FileInputStream;
@@ -38,6 +37,7 @@ public class YmlUtils {
         int enlarge = config.getEnlarge();
         int narrow = config.getNarrow();
 
+        zoomAxis(config, enlarge, narrow);
         zoomRoom(config, enlarge, narrow);
         List<TemplateConfig> templates = config.getTemplates();
         for(TemplateConfig templateConfig : templates){
@@ -46,6 +46,17 @@ public class YmlUtils {
 
     }
 
+    private static void zoomAxis(Config config, int enlarge, int narrow) {
+        AxisConfig axis = config.getAxisPoint();
+        axis.setX(axis.getX()*enlarge/narrow);
+        axis.setY(axis.getY()*enlarge/narrow);
+        axis.setLength(axis.getLength()*enlarge/narrow);
+        axis.setWidth(axis.getWidth()*enlarge/narrow);
+        axis.setxSpacing(axis.getxSpacing()*enlarge/narrow);
+        axis.setySpacing(axis.getySpacing()*enlarge/narrow);
+        config.setAxisPoint(axis);
+    }
+
     private static void zoomTemplate(TemplateConfig templateConfig, int enlarge, int narrow) {
         templateConfig.setLength(templateConfig.getLength()*enlarge/narrow);
         templateConfig.setWidth(templateConfig.getWidth()*enlarge/narrow);

+ 9 - 2
src/main/resources/config.yml

@@ -1,5 +1,12 @@
 enlarge: 1
-narrow: 4
+narrow: 10
+axisPoint:
+  x: 200
+  y: 2100
+  length: 3000
+  width: 2200
+  xSpacing: 100
+  ySpacing: 100
 room:
   length: 2800
   width: 2000
@@ -11,7 +18,7 @@ door:
   x: 100
   y: 0
   position: 1
-  radis: 280
+  radis: 750
   #扇形1234象限
   openDir: 1
   doorShadow: