001/*
002 *    Copyright 2024-2025, Warm-Flow (290631660@qq.com).
003 *
004 *    Licensed under the Apache License, Version 2.0 (the "License");
005 *    you may not use this file except in compliance with the License.
006 *    You may obtain a copy of the License at
007 *
008 *       https://www.apache.org/licenses/LICENSE-2.0
009 *
010 *    Unless required by applicable law or agreed to in writing, software
011 *    distributed under the License is distributed on an "AS IS" BASIS,
012 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *    See the License for the specific language governing permissions and
014 *    limitations under the License.
015 */
016package org.dromara.warm.flow.core.chart;
017
018import org.dromara.warm.flow.core.utils.DrawUtils;
019import org.dromara.warm.flow.core.utils.ObjectUtil;
020import org.dromara.warm.flow.core.utils.StringUtils;
021
022import java.awt.*;
023import java.util.Arrays;
024
025/**
026 * 流程图开始或者结束节点
027 */
028public class SkipChart implements FlowChart {
029    private int n;
030
031    private int[] xPoints;
032
033    private int[] yPoints;
034
035    private Color c;
036
037    private TextChart textChart;
038
039    public SkipChart() {
040    }
041
042    public SkipChart(int[] xPoints, int[] yPoints, Color c, TextChart textChart) {
043        this.xPoints = xPoints;
044        this.yPoints = yPoints;
045        this.c = c;
046        this.textChart = textChart;
047    }
048
049    public int getN() {
050        return n;
051    }
052
053    public SkipChart setN(int n) {
054        this.n = n;
055        return this;
056    }
057
058    public int[] getxPoints() {
059        return xPoints;
060    }
061
062    public SkipChart setxPoints(int[] xPoints) {
063        this.xPoints = xPoints;
064        return this;
065    }
066
067    public int[] getyPoints() {
068        return yPoints;
069    }
070
071    public SkipChart setyPoints(int[] yPoints) {
072        this.yPoints = yPoints;
073        return this;
074    }
075
076    public Color getC() {
077        return c;
078    }
079
080    public SkipChart setC(Color c) {
081        this.c = c;
082        return this;
083    }
084
085    public TextChart getTextChart() {
086        return textChart;
087    }
088
089    public SkipChart setTextChart(TextChart textChart) {
090        this.textChart = textChart;
091        return this;
092    }
093
094    @Override
095    public void draw(Graphics2D graphics) {
096        graphics.setColor(c);
097        // 画跳转线
098        xPoints = Arrays.stream(xPoints).map(x -> x * n).toArray();
099        yPoints = Arrays.stream(yPoints).map(y -> y * n).toArray();
100
101        graphics.drawPolyline(xPoints, yPoints, xPoints.length);
102        // 画箭头, 判断箭头朝向
103        int[] xArrow;
104        int[] yArrow;
105        int xEndOne = xPoints[xPoints.length - 1];
106        int xEndTwo = xPoints[xPoints.length - 2];
107        int yEndOne = yPoints[yPoints.length - 1];
108        int yEndTwo = yPoints[yPoints.length - 2];
109        int xArrowLength = 5 * n;
110        int yArrowLength = 10 * n;
111        if (xEndOne == xEndTwo) {
112            xArrow = new int[]{xEndOne - xArrowLength, xEndOne, xEndOne + xArrowLength};
113            // 1、 如果最后两个左边x相同,最后一个 < 倒数第二个,朝下
114            if (yEndOne > yEndTwo) {
115                yArrow = new int[]{yEndOne - yArrowLength, yEndOne, yEndOne - yArrowLength};
116            } else {
117                // 2、 如果最后两个左边x相同,最后一个 > 倒数第二个,朝上
118                yArrow = new int[]{yEndOne + yArrowLength, yEndOne, yEndOne + yArrowLength};
119            }
120        } else {
121            yArrow = new int[]{yEndOne - xArrowLength, yEndOne, yEndOne + xArrowLength};
122            // 3、 如果最后两个左边y相同,最后一个 < 倒数第二个,朝左
123            if (xEndOne < xEndTwo) {
124                xArrow = new int[]{xEndOne + yArrowLength, xEndOne, xEndOne + yArrowLength};
125            } else {
126                // 4、 如果最后两个左边y相同,最后一个 > 倒数第二个,朝右
127                xArrow = new int[]{xEndOne - yArrowLength, xEndOne, xEndOne - yArrowLength};
128            }
129        }
130        graphics.fillPolygon(xArrow, yArrow, 3);
131        if (ObjectUtil.isNotNull(textChart) && StringUtils.isNotEmpty(textChart.getTitle())) {
132            textChart.setxText(textChart.getxText() - DrawUtils.stringWidth(graphics, textChart.getTitle()) / 2);
133            textChart.setyText(textChart.getyText() - 10);
134            // 填充文字说明
135            textChart.setN(n).draw(graphics);
136        }
137    }
138}