插件开发工具库,推荐依赖该工具库。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

133 lines
5.0 KiB

package com.fanruan.api.design.work.component;
import com.fanruan.api.design.DesignKit;
import com.fanruan.api.design.ui.component.UIComboBox;
import com.fanruan.api.macro.LineConstants;
import com.fanruan.api.util.GraphKit;
import com.fr.base.FRContext;
import com.fr.base.ScreenResolution;
import com.fr.design.gui.icombobox.UIComboBoxRenderer;
import com.fr.general.FRFont;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.GeneralPath;
import java.awt.geom.Point2D;
/**
* @author richie
* @version 10.0
* Created by richie on 2019/10/25
* 用于选择线条类型的下拉框
*/
public class LineComboBox extends UIComboBox<Integer> {
public LineComboBox(int[] lineStyleArray) {
Integer[] lineStyleIntegerArray = new Integer[lineStyleArray.length];
for (int i = 0; i < lineStyleArray.length; i++) {
lineStyleIntegerArray[i] = lineStyleArray[i];
}
this.setModel(new DefaultComboBoxModel(lineStyleIntegerArray));
this.setRenderer(new LineComboBox.LineCellRenderer());
}
/**
* 获取选择的线条类型
*
* @return 用于表示线条类型的整数
*/
public int getSelectedLineStyle() {
int style = (Integer) getSelectedItem();
return (style < 0) ? LineConstants.LINE_NONE : style;
}
/**
* 设置选中的线条类型
*
* @param style 用于表示线条类型的整数
*/
public void setSelectedLineStyle(int style) {
this.setSelectedItem(style);
}
private static class LineCellRenderer extends UIComboBoxRenderer {
private int style = LineConstants.LINE_NONE;
public Component getListCellRendererComponent(
JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
JLabel comp = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
this.style = (Integer) value;
comp.setText(null);
return comp;
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
Dimension d = getSize();
g2d.setColor(getForeground());
FRFont font = FRContext.getDefaultValues().getFRFont();
int resolution = ScreenResolution.getScreenResolution();
Font rfont = font.applyResolutionNP(resolution);
g2d.setFont(rfont);
FontMetrics fm = GraphKit.getFontMetrics(rfont);
if (style == LineConstants.LINE_NONE) {
GraphKit.drawString(g2d, DesignKit.i18nText("Fine-Design_Report_None"), 4, (d.height - fm.getHeight()) / 2D + fm.getAscent());
} else {
GraphKit.drawLine(g2d, 4, d.height / 2D, d.width - 8D, d.height / 2D, style);
}
if (isShowAxisWithLineStyle()) { // 带有坐标轴箭头的样式.
drawArrow(g2d, new Point2D.Double(4, d.height / 2D), new Point2D.Double(d.width - 8D, d.height / 2D));
}
}
private void drawArrow(Graphics2D g2d, Point2D p0, Point2D p1) {
Point2D s = new Point2D.Double(p1.getX() - p0.getX(), p1.getY() - p0.getY());
Point2D t = new Point2D.Double();
double d1 = p0.distance(p1);
//d2-d5设定箭头的大小,p1-p2为坐标轴的延长线,p2-p5-p3-p6为箭头4个点的具体位置
double d2 = 9;
double d3 = 15;
double d4 = 7;
double d5 = 3;
t.setLocation(d2 * s.getX() / d1, d2 * s.getY() / d1);
Point2D p2 = new Point2D.Double(p1.getX() + t.getX(), p1.getY() + t.getY());
t.setLocation(d3 * s.getX() / d1, d3 * s.getY() / d1);
Point2D p3 = new Point2D.Double(p1.getX() + t.getX(), p1.getY() + t.getY());
t.setLocation(d4 * s.getX() / d1, d4 * s.getY() / d1);
Point2D p4 = new Point2D.Double(p1.getX() + t.getX(), p1.getY() + t.getY());
Point2D p5 = new Point2D.Double(p4.getX() + s.getY() / d1 * d5, p4.getY() - s.getX() / d1 * d5);
Point2D p6 = new Point2D.Double(p4.getX() - s.getY() / d1 * d5, p4.getY() + s.getX() / d1 * d5);
GeneralPath arrow = new GeneralPath();
arrow.moveTo((float) p2.getX() - 10, (float) p2.getY());
arrow.lineTo((float) p5.getX() - 10, (float) p5.getY());
arrow.lineTo((float) p3.getX() - 10, (float) p3.getY());
arrow.lineTo((float) p6.getX() - 10, (float) p6.getY());
arrow.closePath();
g2d.draw(arrow);
g2d.fill(arrow);
}
private boolean isShowAxisWithLineStyle() {
return style == LineConstants.LINE_CHART_MED_ARROW
|| style == LineConstants.LINE_CHART_THICK_ARROW || style == LineConstants.LINE_CHART_THIN_ARROW;
}
public Dimension getPreferredSize() {
return new Dimension(60, 16);
}
public Dimension getMinimumSize() {
return getPreferredSize();
}
}
}