Browse Source

初始化代码

duh 6 years ago
commit
81df61f085

+ 20 - 0
pom.xml

@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <groupId>com.elab</groupId>
+    <artifactId>hxb</artifactId>
+    <version>1.0-SNAPSHOT</version>
+    <dependencies>
+        <!-- https://mvnrepository.com/artifact/org.yaml/snakeyaml -->
+        <dependency>
+            <groupId>org.yaml</groupId>
+            <artifactId>snakeyaml</artifactId>
+            <version>1.21</version>
+        </dependency>
+
+    </dependencies>
+
+</project>

+ 27 - 0
src/main/java/config/RectangleConfig.java

@@ -0,0 +1,27 @@
+package config;
+
+/**
+ * @author duh
+ * @create 2018/7/17 15:28
+ * @email duh@elab-plus.com
+ **/
+public class RectangleConfig {
+    protected int length;
+    protected int width;
+
+    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;
+    }
+}

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

@@ -0,0 +1,10 @@
+package config;
+
+/**
+ * @author duh
+ * @create 2018/7/17 15:28
+ * @email duh@elab-plus.com
+ **/
+public class RoomConfig extends RectangleConfig {
+
+}

+ 27 - 0
src/main/java/config/TemplateConfig.java

@@ -0,0 +1,27 @@
+package config;
+
+/**
+ * @author duh
+ * @create 2018/7/17 15:25
+ * @email duh@elab-plus.com
+ **/
+public class TemplateConfig {
+    private RoomConfig room;
+    private XSConfig XS;
+
+    public RoomConfig getRoom() {
+        return room;
+    }
+
+    public void setRoom(RoomConfig room) {
+        this.room = room;
+    }
+
+    public XSConfig getXS() {
+        return XS;
+    }
+
+    public void setXS(XSConfig XS) {
+        this.XS = XS;
+    }
+}

+ 45 - 0
src/main/java/config/XSConfig.java

@@ -0,0 +1,45 @@
+package config;
+
+/**
+ * @author duh
+ * @create 2018/7/17 15:18
+ * @email duh@elab-plus.com
+ **/
+public class XSConfig {
+    private int length;
+    private int width;
+    private String shadowPoint;
+    private int radis;
+
+    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 String getShadowPoint() {
+        return shadowPoint;
+    }
+
+    public void setShadowPoint(String shadowPoint) {
+        this.shadowPoint = shadowPoint;
+    }
+
+    public int getRadis() {
+        return radis;
+    }
+
+    public void setRadis(int radis) {
+        this.radis = radis;
+    }
+}

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

@@ -0,0 +1,45 @@
+package controller;
+
+import config.TemplateConfig;
+import utils.YmlUtils;
+import view.Axis;
+import view.AxisBorder;
+
+import javax.imageio.ImageIO;
+import java.awt.*;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.IOException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+import static utils.AxisUtils.drawAxis;
+
+/**
+ * @author duh
+ * @create 2018/7/17 15:06
+ * @email duh@elab-plus.com
+ **/
+public class MyFrame {
+    public static void main(String[] args) throws Exception {
+        TemplateConfig template = YmlUtils.readYml();
+        int width = template.getRoom().getWidth();
+        int height = template.getRoom().getLength();
+
+        AxisBorder axisBorder = new AxisBorder(60, 60, 40, 40, 0xffffff);
+        BufferedImage bufferedImage = new BufferedImage(width+axisBorder.getLeft() + axisBorder.getRight(),
+                height + axisBorder.getTop() +axisBorder.getBottom(), BufferedImage.TYPE_INT_RGB);
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
+
+        Graphics2D g = bufferedImage.createGraphics();
+        Date startDate = sdf.parse("20140318120000");
+        Date endDate = sdf.parse("20140320120000");
+        Axis xAxis = new Axis(0x000000, 2, "长", "datetime", 0, 0, 5, startDate, endDate, "");
+        Axis yAxis = new Axis(0x000000, 2, "宽", "value", 30, 0, 5, null, null, "");
+        drawAxis(axisBorder, xAxis, yAxis, width, height, g);
+
+        //导出图片
+        String pathname = "E:\\config\\test.jpg";
+        ImageIO.write(bufferedImage, "jpg", new File(pathname));
+    }
+}

+ 48 - 0
src/main/java/model/Template.java

@@ -0,0 +1,48 @@
+package model;
+
+/**
+ * @author duh
+ * @create 2018/7/17 15:00
+ * @email duh@elab-plus.com
+ **/
+public class Template {
+    protected String name;
+    //x坐标
+    protected int x;
+    //y坐标
+    protected int y;
+    //旋转角度
+    protected int scale;
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    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 getScale() {
+        return scale;
+    }
+
+    public void setScale(int scale) {
+        this.scale = scale;
+    }
+}

+ 13 - 0
src/main/java/model/XS.java

@@ -0,0 +1,13 @@
+package model;
+
+/**
+ * @author duh
+ * @create 2018/7/17 15:00
+ * @email duh@elab-plus.com
+ **/
+public class XS extends Template{
+    public XS(){
+        setName("XS");
+    }
+
+}

+ 241 - 0
src/main/java/utils/AxisUtils.java

@@ -0,0 +1,241 @@
+package utils;
+
+import view.Axis;
+import view.AxisBorder;
+import view.Point;
+
+import javax.imageio.ImageIO;
+import java.awt.*;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
+
+/**
+ * @author duh
+ * @create 2018/7/17 15:03
+ * @email duh@elab-plus.com
+ **/
+/**
+ * Copyright (c) 2014, alax
+ * All Rights Reserved.
+ */
+
+
+/**
+ *
+ * @author   	 alax_app@yeah.net
+ * @Date	 	 2014-3-19 上午12:30:20
+ *
+ */
+public class AxisUtils {
+
+
+    public static void main(String[] args) throws Exception {
+        int width = 1000;
+        int height = 400;
+        AxisBorder axisBorder = new AxisBorder(60, 60, 40, 40, 0xffffff);
+        BufferedImage bufferedImage = new BufferedImage(width+axisBorder.getLeft() + axisBorder.getRight(),
+                height + axisBorder.getTop() +axisBorder.getBottom(), BufferedImage.TYPE_INT_RGB);
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
+
+        Graphics2D g = bufferedImage.createGraphics();
+        Date startDate = sdf.parse("20140318120000");
+        Date endDate = sdf.parse("20140320120000");
+        Axis xAxis = new Axis(0x000000, 2, "时间", "datetime", 0, 0, 5, startDate, endDate, "");
+        Axis yAxis = new Axis(0x000000, 2, "高度", "value", 30, 0, 5, null, null, "");
+        drawAxis(axisBorder, xAxis, yAxis, width, height, g);
+
+        //导出图片
+        String pathname = "E:\\config\\test.jpg";
+        ImageIO.write(bufferedImage, "jpg", new File(pathname));
+
+    }
+    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);
+        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);
+    }
+
+    /**
+     * 画x轴坐标
+     * @param axisBorder
+     * @param xaxis
+     * @param width
+     * @param height
+     * @param g
+     */
+    private static void drawXAxis(AxisBorder axisBorder,
+                                  Axis xaxis, int width ,int height, Graphics2D g){
+        //画x坐标
+        Point point1 = new Point(axisBorder.getLeft(), axisBorder.getTop() + height);
+        Point point2 = new Point(axisBorder.getLeft() + width, axisBorder.getTop() + height);
+        drawLine(point1, point2, new Color(xaxis.getLineColor()) , xaxis.getLineWidth(), g);
+        //画刻度
+        int ticks = xaxis.getTicks();
+        int skips = width / ticks;
+        int x = axisBorder.getLeft();
+        int y1 = axisBorder.getTop() + height ;
+        int y2 = y1 + 10;
+        String type = xaxis.getType();	//获取坐标轴类型
+        for(int i = 1 ; i <= ticks ; i++){
+            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)){
+            double vskips = (xaxis.getMax() - xaxis.getMin() ) / ticks + xaxis.getMin();
+            for(int i = 1 ; i <= ticks ; i++){
+                drawString(g, x + skips * i, y2, getValueLabel(vskips * i + xaxis.getMin(), xaxis.getLabelFormat()));
+            }
+        }else if(type.equals(Axis.DATETIME)){
+            long t0 = xaxis.getStartDate().getTime() ;
+            long tskips = ( xaxis.getEndDate().getTime() - t0 ) /ticks;
+            Calendar c = Calendar.getInstance();
+            for(int i = 1 ; i <= ticks ; i++){
+                c.setTimeInMillis(t0 + i * tskips);
+                drawString(g, x + skips * i, y2+10, getDateLabel(c.getTime(), xaxis.getLabelFormat()));
+            }
+        }
+        //画title
+        drawString(g, axisBorder.getLeft(),
+                axisBorder.getTop() + height + axisBorder.getBottom() /2, xaxis.getTitle());
+
+    }
+
+
+    /**
+     * 画Y轴坐标
+     * @param axisBorder
+     * @param axis
+     * @param width
+     * @param height
+     * @param g
+     */
+    private static void drawYAxis(AxisBorder axisBorder,
+                                  Axis axis, int width ,int height, Graphics2D g){
+        //画x坐标
+        Point point1 = new Point(axisBorder.getLeft(), axisBorder.getTop() + height);
+        Point point2 = new Point(axisBorder.getLeft(), axisBorder.getTop());
+
+
+        drawLine(point1, point2, new Color(axis.getLineColor()) , axis.getLineWidth(), g);
+
+        //画刻度
+        int ticks = axis.getTicks();
+        int skips = height / ticks;
+        int x1 = axisBorder.getLeft();
+        int x2 = x1 - 10 ;
+        int y1 = axisBorder.getTop() + height ;
+        String type = axis.getType();	//获取坐标轴类型
+        for(int i = 1 ; i <= ticks ; i++){
+            point1 = new Point(x2,y1 - skips * i);
+            point2 = new Point(x1,y1 - skips * i);
+            drawLine(point1, point2, new Color(axis.getLineColor()) , axis.getLineWidth(), g);
+
+        }
+        //画label
+        if(type.equals(Axis.VALUE)){
+            double vskips = (axis.getMax() - axis.getMin() ) / ticks + axis.getMin();
+            for(int i = 1 ; i <= ticks ; i++){
+                drawString(g, x2 - 20 , y1 - skips * i, getValueLabel(vskips * i + axis.getMin(), axis.getLabelFormat()));
+            }
+        }else if(type.equals(Axis.DATETIME)){
+            long t0 = axis.getStartDate().getTime() ;
+            long tskips = ( axis.getEndDate().getTime() - t0 ) /ticks;
+            Calendar c = Calendar.getInstance();
+            for(int i = 1 ; i <= ticks ; i++){
+                c.setTimeInMillis(t0 + i * tskips);
+                drawString(g, x2 - 20 , y1 - skips * i, getDateLabel(c.getTime(), axis.getLabelFormat()));
+            }
+        }
+        //画title
+        //画y坐标title
+        int x = Math.min(30, axisBorder.getLeft());
+        int y0 = axisBorder.getTop() + height/2 + 80;
+
+        //逆时针旋转180度
+        g.rotate(-Math.PI/2, x, y0);
+        Font font = new Font("宋体", Font.BOLD, 16);
+        g.setFont(font);
+        g.drawString(axis.getTitle(),x,y0);
+        //恢复画布,顺时针旋转180度
+        g.rotate(Math.PI/2,x,y0);
+
+    }
+
+    /**
+     * 画线
+     * @param point1
+     * @param point2
+     * @param lineColor
+     * @param lineWidth
+     * @param g
+     */
+    private static void drawLine(Point point1 , Point point2, Color lineColor, int lineWidth ,Graphics2D g){
+        g.setStroke(new BasicStroke(lineWidth));
+        g.setColor(lineColor);
+        g.drawLine(point1.getX(), point1.getY(), point2.getX(), point2.getY());
+    }
+
+    /**
+     * 写字符
+     */
+    private static void drawString(Graphics2D g,int x ,int y,String str){
+        g.drawString(str, x, y);
+    }
+
+
+
+    /**
+     * 获取数字型label
+     * @param value
+     * @param format
+     * @return
+     */
+    public static String getValueLabel(double value , String format ){
+        if(format == null  || format.equals("")){
+            return value +"";
+        }
+        return String.format(format, value+"");
+    }
+
+    /**
+     * 获取时间类型label
+     * @param date
+     * @param format
+     * @return
+     */
+    public static String getDateLabel(Date date , String format){
+        SimpleDateFormat sdf = new SimpleDateFormat("dd");
+        if(format == null  || format.equals("")){
+            format = "dd日HH时";
+        }
+        sdf.applyPattern(format);
+        return sdf.format(date);
+    }
+
+    /**
+     * 画border
+     * @param border
+     * @param width
+     * @param height
+     * @param g
+     */
+    public static void drawBorder(AxisBorder border, int width, int height,Graphics2D g){
+        g.setColor(new Color(border.getBackgroundColor()));
+        g.fillRect(0, 0, border.getLeft(), border.getTop()+height+border.getBottom()); //填充背景色
+        g.fillRect(border.getLeft(), 0, width+border.getRight(), border.getTop());
+        g.fillRect(border.getLeft(), border.getTop()+height, width+border.getRight(), border.getBottom());
+        g.fillRect(border.getLeft()+width, border.getTop(), border.getRight(), height);
+    }
+
+}

+ 30 - 0
src/main/java/utils/YmlUtils.java

@@ -0,0 +1,30 @@
+package utils;
+
+import config.TemplateConfig;
+import config.XSConfig;
+import org.yaml.snakeyaml.Yaml;
+
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.net.URL;
+
+/**
+ * @author duh
+ * @create 2018/7/17 15:20
+ * @email duh@elab-plus.com
+ **/
+public class YmlUtils {
+    public static TemplateConfig readYml(){
+        Yaml yaml = new Yaml();
+        URL url = YmlUtils.class.getClassLoader().getResource("template.yml");
+        if(null != url){
+            try {
+                TemplateConfig templateConfig = yaml.loadAs(new FileInputStream(url.getFile()), TemplateConfig.class);
+                return templateConfig;
+            } catch (FileNotFoundException e) {
+                e.printStackTrace();
+            }
+        }
+        return null;
+    }
+}

+ 145 - 0
src/main/java/view/Axis.java

@@ -0,0 +1,145 @@
+package view;
+
+import java.util.Date;
+/**
+ * 坐标系
+ * @author duh
+ * @create 2018/7/17 15:02
+ * @email duh@elab-plus.com
+ **/
+public class Axis {
+    public static final String VALUE = "value";
+    public static final String DATETIME = "datetime";
+
+    private int lineColor;		//y轴颜色
+    private int lineWidth;		//y轴宽度
+    private String title;		//y轴标题
+    private String type;		//y轴类型  支持value 类型 和datetime类型
+
+    private double max;			//最大值
+    private double min;			//最小值
+
+    private int ticks;			//刻度个数
+
+    private Date startDate;
+    private Date endDate;
+
+
+    private String labelFormat; //刻度值排版
+
+
+    public Axis(){
+
+    }
+
+
+
+    public Axis(int lineColor, int lineWidth, String title, String type,
+                double max, double min, int ticks, Date startDate, Date endDate,
+                String labelFormat) {
+        super();
+        this.lineColor = lineColor;
+        this.lineWidth = lineWidth;
+        this.title = title;
+        this.type = type;
+        this.max = max;
+        this.min = min;
+        this.ticks = ticks;
+        this.startDate = startDate;
+        this.endDate = endDate;
+        this.labelFormat = labelFormat;
+    }
+
+
+
+
+
+
+    public int getLineColor() {
+        return lineColor;
+    }
+
+    public void setLineColor(int lineColor) {
+        this.lineColor = lineColor;
+    }
+
+    public int getLineWidth() {
+        return lineWidth;
+    }
+
+    public void setLineWidth(int lineWidth) {
+        this.lineWidth = lineWidth;
+    }
+
+    public String getTitle() {
+        return title;
+    }
+
+    public void setTitle(String title) {
+        this.title = title;
+    }
+
+    public String getType() {
+        return type;
+    }
+
+    public void setType(String type) {
+        this.type = type;
+    }
+
+    public double getMax() {
+        return max;
+    }
+
+    public void setMax(double max) {
+        this.max = max;
+    }
+
+    public double getMin() {
+        return min;
+    }
+
+    public void setMin(double min) {
+        this.min = min;
+    }
+
+
+    public String getLabelFormat() {
+        return labelFormat;
+    }
+
+    public void setLabelFormat(String labelFormat) {
+        this.labelFormat = labelFormat;
+    }
+
+
+    public Date getStartDate() {
+        return startDate;
+    }
+
+
+    public void setStartDate(Date startDate) {
+        this.startDate = startDate;
+    }
+
+
+    public Date getEndDate() {
+        return endDate;
+    }
+
+    public void setEndDate(Date endDate) {
+        this.endDate = endDate;
+    }
+
+
+    public int getTicks() {
+        return ticks;
+    }
+
+
+    public void setTicks(int ticks) {
+        this.ticks = ticks;
+    }
+
+
+}

+ 75 - 0
src/main/java/view/AxisBorder.java

@@ -0,0 +1,75 @@
+package view;
+
+/**
+ * @author duh
+ * @create 2018/7/17 15:01
+ * @email duh@elab-plus.com
+ * 边界类
+ **/
+public class AxisBorder {
+
+    private int left;
+    private int right;
+    private int top;
+    private int bottom;
+
+    private int backgroundColor;	//背景色
+
+    public AxisBorder(){
+
+    }
+
+    public AxisBorder(int left, int right, int top, int bottom,
+                      int backgroundColor) {
+        super();
+        this.left = left;
+        this.right = right;
+        this.top = top;
+        this.bottom = bottom;
+        this.backgroundColor = backgroundColor;
+    }
+
+
+
+    public int getLeft() {
+        return left;
+    }
+
+    public void setLeft(int left) {
+        this.left = left;
+    }
+
+    public int getRight() {
+        return right;
+    }
+
+    public void setRight(int right) {
+        this.right = right;
+    }
+
+    public int getTop() {
+        return top;
+    }
+
+    public void setTop(int top) {
+        this.top = top;
+    }
+
+    public int getBottom() {
+        return bottom;
+    }
+
+    public void setBottom(int bottom) {
+        this.bottom = bottom;
+    }
+
+    public int getBackgroundColor() {
+        return backgroundColor;
+    }
+
+    public void setBackgroundColor(int backgroundColor) {
+        this.backgroundColor = backgroundColor;
+    }
+
+}
+

+ 33 - 0
src/main/java/view/Point.java

@@ -0,0 +1,33 @@
+package view;
+
+/**
+ * 点
+ * @author duh
+ * @create 2018/7/17 15:03
+ * @email duh@elab-plus.com
+ **/
+public class Point {
+
+    private int x;
+    private int y;
+
+
+    public Point(int x, int y) {
+        super();
+        this.x = x;
+        this.y = y;
+    }
+    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;
+    }
+
+}

+ 8 - 0
src/main/resources/template.yml

@@ -0,0 +1,8 @@
+room:
+ length: 1000
+ width: 550
+XS:
+  length: 1000
+  width: 550
+  shadowPoint: 0.5,1
+  radis: 800