import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Random;
import javax.imageio.ImageIO;
public class FontImage {
private static Font MAXFONT = new Font("宋体", Font.BOLD, 30);
private static Font MIDDLEFONT = new Font("宋体", Font.BOLD, 20);
private static Font SMALLFONT = new Font("宋体", Font.PLAIN, 12);
private static int PICTURE_WIDTH = 1200;// 图形的宽所占据的像素点(圆形的直径)
private static int PICTURE_HEIGTH = 300;// 图形的宽所占据的像素点(圆形的直径)
private static int SHAPE_WIDTH = 40;// 图形的宽所占据的像素点(圆形的直径)
private static int SHAPE_HEIGTH = 40;// 图形的高所占据的像素点(圆形的直径)
private static int LINE_LENGTH = 60;// 图形之间的连接线的长度
private static int OPERATION_NAME_LENGTH = 8;//圆形下面名称的长度
private static int tempX = 0;
public static void main(String[] args) throws Exception {
JSONArray array = new JSONArray();
Random rd = new Random();
// int count = rd.nextInt(8) + 1;
int count = 7;
for (int i = 0; i < count; i++) {
JSONObject json = new JSONObject();
json.put("operationIndex", i + 1);
json.put("name", "2222222222222" + 1);
array.add(json);
}
createImage( new File("C:\\Users\\hly00\\Desktop\\a.png"), array);
}
/**
* @Description: 绘制图片
* @params * @param null
* @return
* @exception
* @date 2019/8/24 14:15
*/
private void createAllControlPlanShape(File outFile, JSONArray array){
}
/**
* @Description: 创建图片
* @params * @param null
* @return
* @exception
* @date 2019/8/24 14:14
*/
public static void createImage(File outFile, JSONArray array) throws Exception {
BasicStroke bs = new BasicStroke(2);
// 创建图片
BufferedImage image = new BufferedImage(PICTURE_WIDTH, PICTURE_HEIGTH,
BufferedImage.TYPE_INT_BGR);
Graphics2D g2d = (Graphics2D) image.getGraphics();
//设置线的宽
g2d.setStroke(bs);
// 抗锯齿
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setClip(0, 0, PICTURE_WIDTH, PICTURE_HEIGTH);
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, PICTURE_WIDTH, PICTURE_HEIGTH);// 先用黑色填充整张图片,也就是背景
g2d.setColor(Color.BLACK);// 在换成黑色
g2d.setFont(MAXFONT);// 设置画笔字体
//图形的中线 距离左上角 的y轴的距离
int lineY = PICTURE_HEIGTH / 2;
// System.out.println("lineY = " + lineY);
int size = array.size();
// System.out.println("size = " + size);
int startXPosition = getStartXPosition(size);
// System.out.println("startXPosition = " + startXPosition);
//绘制 开始的三角形
Polygon polygon1 = new Polygon();
//三角形的顶点坐标
int x = startXPosition + SHAPE_WIDTH / 2;
int y = lineY - SHAPE_HEIGTH / 2;
polygon1.addPoint(x, y);
//三角形的右点坐标
x = startXPosition + SHAPE_WIDTH;
y = lineY + SHAPE_HEIGTH / 2;
polygon1.addPoint(x, y);
//三角形的左点坐标
x = startXPosition;
y = lineY + SHAPE_HEIGTH / 2;
polygon1.addPoint(x, y);
//绘制
g2d.drawPolygon(polygon1);
//获取到现在绘图的位置开始的X坐标
int tempX = getTempX(startXPosition);
for (int a = 0; a < 3; a++) {
JSONObject json = JSONObject.fromObject(array.get(a));
crateShape(json, g2d, lineY);
}
//绘制一条横线
createLine(g2d, lineY);
JSONObject json3 = JSONObject.fromObject(array.get(3));
//绘制一条子弹
createBullet(g2d, lineY, json3);
//绘制一条横线
createLine(g2d, lineY);
JSONObject json4 = JSONObject.fromObject(array.get(4));
//绘制一条空心箭头
createArrow(g2d, lineY, json4);
//绘制一条横线
createLine(g2d, lineY);
JSONObject json5 = JSONObject.fromObject(array.get(5));
//绘制一条矩形
createDrawRect(g2d, lineY, json5);
//绘制一条横线
createBranchLine(g2d, lineY);
JSONObject json6 = JSONObject.fromObject(array.get(6));
//绘制一条菱形
createDiamond(g2d, lineY, json6);
g2d.dispose();
ImageIO.write(image, "png", outFile);// 输出png图片
}
/**
* @return
* @throws
* @Description: 创建图形
* @params * @param null
* @date 2019/8/22 15:41
*/
private static void crateShape(JSONObject json, Graphics2D g2d, int lineY) {
//绘制一条横线
createLine(g2d, lineY);
//绘制一个圆形 并添加文字
createRoundAddFont(g2d, lineY, json);
}
/**
* @return
* @throws
* @Description: 绘制虚线
* @params * @param null
* @date 2019/8/22 18:09
*/
private static void createBranchLine(Graphics2D g2d, int lineY) {
int temp_length = LINE_LENGTH / 22;
//绘制虚线
g2d.drawLine(tempX, lineY, tempX + temp_length * 4, lineY);//画一条线段
g2d.drawLine(tempX + temp_length * 6, lineY, tempX + temp_length * 10, lineY);//画一条线段
g2d.drawLine(tempX + temp_length * 12, lineY, tempX + temp_length * 16, lineY);//画一条线段
g2d.drawLine(tempX + temp_length * 18, lineY, tempX + temp_length * 22, lineY);//画一条线段
g2d.drawLine(tempX + temp_length * 24, lineY, tempX + LINE_LENGTH, lineY);//画一条线段
tempX += LINE_LENGTH;
}
/**
* @return
* @throws
* @Description: 绘制子弹并添加序号和图形下面的文字
* @params * @param null
* @date 2019/8/22 18:09
*/
private static void createBullet(Graphics2D g2d, int lineY, JSONObject json) {
//画出一个闭合多边形(子弹)
//竖线
g2d.drawLine(tempX, lineY - SHAPE_HEIGTH / 2, tempX, lineY + SHAPE_HEIGTH / 2);
//上横线
g2d.drawLine(tempX, lineY - SHAPE_HEIGTH / 2, tempX + SHAPE_WIDTH / 2, lineY - SHAPE_HEIGTH / 2);
//下横线
g2d.drawLine(tempX, lineY + SHAPE_HEIGTH / 2, tempX + SHAPE_WIDTH / 2, lineY + SHAPE_HEIGTH / 2);
//圆弧
g2d.drawArc(tempX, lineY - SHAPE_HEIGTH / 2, SHAPE_WIDTH, SHAPE_HEIGTH, -90, 180);
//获取编号
String operationIndexStr = json.getString("operationIndex");
//添加图形里面的序号
g2d.drawString(operationIndexStr, tempX + (SHAPE_WIDTH / 2) - 9, lineY + 10);
//添加图形下面的名称
addShapeUnderStandFont(g2d, json, lineY);
//更新最新X轴坐标
tempX += SHAPE_HEIGTH;
}
/**
* @return
* @throws
* @Description: 绘制空心箭头并添加序号和图形下面的文字
* @params * @param null
* @date 2019/8/22 18:09
*/
private static void createArrow(Graphics2D g2d, int lineY, JSONObject json) {
//画出一个闭合多边形(空心箭头)
int[] xPoints = {tempX, tempX + SHAPE_WIDTH / 2, tempX + SHAPE_WIDTH / 2, tempX + SHAPE_WIDTH, tempX + SHAPE_WIDTH / 2, tempX + SHAPE_WIDTH / 2, tempX};
int[] yPoints = {lineY - SHAPE_HEIGTH / 4, lineY - SHAPE_HEIGTH / 4, lineY - SHAPE_HEIGTH / 2, lineY, lineY + SHAPE_HEIGTH / 2, lineY + SHAPE_HEIGTH / 4, lineY + SHAPE_HEIGTH / 4};
g2d.drawPolygon(xPoints, yPoints, 7);
//获取编号
String operationIndexStr = json.getString("operationIndex");
//添加图形里面的序号
g2d.drawString(operationIndexStr, tempX + (SHAPE_WIDTH / 2) - 9, lineY + 10);
//添加图形下面的名称
addShapeUnderStandFont(g2d, json, lineY);
//更新最新X轴坐标
tempX += SHAPE_HEIGTH;
}
/**
* @return
* @throws
* @Description: 绘制菱形并添加序号和图形下面的文字
* @params * @param null
* @date 2019/8/22 18:09
*/
private static void createDiamond(Graphics2D g, int lineY, JSONObject json) {
//画出一个闭合多边形(菱形)
int[] xPoints = {tempX, tempX + SHAPE_WIDTH / 2, tempX + SHAPE_WIDTH, tempX + SHAPE_WIDTH / 2};
int[] yPoints = {lineY, lineY - SHAPE_HEIGTH / 2, lineY, lineY + SHAPE_HEIGTH / 2};
g.drawPolygon(xPoints, yPoints, 4);
//获取编号
String operationIndexStr = json.getString("operationIndex");
//添加图形里面的序号
g.drawString(operationIndexStr, tempX + (SHAPE_WIDTH / 2) - 9, lineY + 10);
//添加图形下面的名称
addShapeUnderStandFont(g, json, lineY);
//更新最新X轴坐标
tempX += SHAPE_HEIGTH;
}
/**
* @return
* @throws
* @Description: 绘制矩形并添加序号和图形下面的文字
* @params * @param null
* @date 2019/8/22 18:03
*/
private static void createDrawRect(Graphics2D g2d, int lineY, JSONObject json) {
//绘制矩形
g2d.drawRect(tempX, lineY - SHAPE_HEIGTH / 2, SHAPE_WIDTH, SHAPE_HEIGTH);
//获取编号
String operationIndexStr = json.getString("operationIndex");
//添加图形里面的序号
g2d.drawString(operationIndexStr, tempX + (SHAPE_WIDTH / 2) - 9, lineY + 10);
//添加图形下面的名称
addShapeUnderStandFont(g2d, json, lineY);
//更新最新X轴坐标
tempX += SHAPE_HEIGTH;
}
/**
* @return
* @throws
* @Description: 绘制 圆形并添加序号和图形下面的文字
* @params * @param null
* @date 2019/8/22 15:40
*/
private static void createRoundAddFont(Graphics2D g2d, int lineY, JSONObject json) {
//画圆
g2d.drawRoundRect(tempX, lineY - SHAPE_HEIGTH / 2, SHAPE_WIDTH, SHAPE_HEIGTH, SHAPE_WIDTH, SHAPE_HEIGTH);//画圆
//获取编号
String operationIndexStr = json.getString("operationIndex");
//添加图形里面的序号
g2d.drawString(operationIndexStr, tempX + (SHAPE_WIDTH / 2) - 9, lineY + 10);
//添加图形下面的名称
addShapeUnderStandFont(g2d, json, lineY);
//更新最新X轴坐标
tempX += SHAPE_HEIGTH;
}
/**
* @return
* @throws
* @Description: 给图形下面添加的名称
* @params * @param null
* @date 2019/8/22 17:09
*/
private static void addShapeUnderStandFont(Graphics2D g2d, JSONObject json, int lineY) {
//获取名称
String operatioName = json.getString("name");
//获取名称长度
int operatioNameLength = operatioName.length();
// System.out.println("operatioNameLength = "+operatioNameLength);
//更换为小字体
g2d.setFont(SMALLFONT);
int operationNameY = lineY + SHAPE_HEIGTH;
int subSize = operatioNameLength / OPERATION_NAME_LENGTH;
int subLast = operatioNameLength % OPERATION_NAME_LENGTH;
if (subLast > 0) {
subSize++;
}
for (int i = 0; i < subSize - 1; i++) {
String str = operatioName.substring(i * OPERATION_NAME_LENGTH, (i + 1) * OPERATION_NAME_LENGTH);
int length = str.length();
// System.out.println("str.length = " + length);
// int operationNameX = tempX - LINE_LENGTH / 2 * length / 16;
int operationNameX = tempX+SHAPE_WIDTH/2 - OPERATION_NAME_LENGTH/2*10;
// System.out.println("operationNameX = " + operationNameX);
g2d.drawString(str, operationNameX, operationNameY);
operationNameY += SHAPE_HEIGTH / 2 * 2 / 3;
}
String str = operatioName.substring((subSize - 1) * OPERATION_NAME_LENGTH);
final int length = str.length();
// System.out.println("last.str.length = " + length);
// int operationNameX = tempX - LINE_LENGTH / 2 * length / 16;
int operationNameX = tempX+SHAPE_WIDTH/2 - OPERATION_NAME_LENGTH/2*10;
// System.out.println("operationNameX = " + operationNameX);
g2d.drawString(str, operationNameX, operationNameY);
//更换为大字体
g2d.setFont(MAXFONT);
}
/**
* @return
* @throws
* @Description: 绘制横线
* @params * @param null
* @date 2019/8/22 15:22
*/
private static void createLine(Graphics2D g2d, int lineY) {
g2d.drawLine(tempX, lineY, tempX + LINE_LENGTH, lineY);//画一条线段
tempX += LINE_LENGTH;
}
/**
* @return
* @throws
* @Description: 获取最开始的图形距离左上角 X 轴的距离s
* @params * @param int shapeSize 图形的个数
* @date 2019/8/22 14:48
*/
private static int getStartXPosition(int shapeSize) {
int start_X = (PICTURE_WIDTH - (SHAPE_WIDTH / 4 * 3) - (SHAPE_WIDTH + LINE_LENGTH) * shapeSize) / 2;
return start_X;
}
/**
* @return
* @throws
* @Description: 获取到现在绘图的位置开始的X坐标
* @params * @param null
* @date 2019/8/22 15:16
*/
private static int getTempX(int startXPosition) {
tempX = startXPosition + SHAPE_WIDTH / 4 * 3;
return tempX;
}
}