📄 meterplot.java
字号:
(int) meterMiddleY);
arrow.addPoint((int) (meterMiddleX + DEFAULT_CIRCLE_SIZE / 4),
(int) meterMiddleY);
}
arrow.addPoint((int) valueP1, (int) valueP2);
Ellipse2D circle = new Ellipse2D.Double(meterMiddleX - DEFAULT_CIRCLE_SIZE / 2,
meterMiddleY - DEFAULT_CIRCLE_SIZE / 2,
DEFAULT_CIRCLE_SIZE,
DEFAULT_CIRCLE_SIZE);
g2.fill(arrow);
g2.fill(circle);
}
g2.clip(savedClip);
g2.setComposite(originalComposite);
}
if (drawBorder) {
drawOutline(g2, plotArea);
}
}
/**
* Draws a colored range (arc) for one level.
*
* @param g2 The graphics device.
* @param meterArea The drawing area.
* @param data The dataset.
* @param type The level.
*/
void drawArcFor(Graphics2D g2, Rectangle2D meterArea, MeterDataset data, int type) {
Number minValue = null;
Number maxValue = null;
Paint paint = null;
switch (type) {
case MeterDataset.NORMAL_DATA:
minValue = data.getMinimumNormalValue();
maxValue = data.getMaximumNormalValue();
paint = getNormalPaint();
break;
case MeterDataset.WARNING_DATA:
minValue = data.getMinimumWarningValue();
maxValue = data.getMaximumWarningValue();
paint = getWarningPaint();
break;
case MeterDataset.CRITICAL_DATA:
minValue = data.getMinimumCriticalValue();
maxValue = data.getMaximumCriticalValue();
paint = getCriticalPaint();
break;
case MeterDataset.FULL_DATA:
minValue = data.getMinimumValue();
maxValue = data.getMaximumValue();
paint = DEFAULT_BACKGROUND_PAINT;
break;
default:
return;
}
if (minValue != null && maxValue != null) {
if (data.getBorderType() == type) {
drawArc(g2, meterArea,
minValue.doubleValue(),
data.getMinimumValue().doubleValue(),
paint);
drawArc(g2, meterArea,
data.getMaximumValue().doubleValue(),
maxValue.doubleValue(),
paint);
}
else {
drawArc(g2, meterArea,
minValue.doubleValue(),
maxValue.doubleValue(),
paint);
}
// draw a tick at each end of the range...
drawTick(g2, meterArea, minValue.doubleValue(), true, paint);
drawTick(g2, meterArea, maxValue.doubleValue(), true, paint);
}
}
/**
* Draws an arc.
*
* @param g2 the graphics device.
* @param area the plot area.
* @param minValue the minimum value.
* @param maxValue the maximum value.
* @param paint the paint.
*/
void drawArc(Graphics2D g2, Rectangle2D area, double minValue, double maxValue, Paint paint) {
drawArc(g2, area, minValue, maxValue, paint, 0);
}
/**
* Draws an arc.
*
* @param g2 the graphics device.
* @param area the plot area.
* @param minValue the minimum value.
* @param maxValue the maximum value.
* @param paint the paint.
* @param outlineType the outline type.
*/
void drawArc(Graphics2D g2, Rectangle2D area, double minValue, double maxValue,
Paint paint, int outlineType) {
double startAngle = calculateAngle(maxValue);
double endAngle = calculateAngle(minValue);
double extent = endAngle - startAngle;
double x = area.getX();
double y = area.getY();
double w = area.getWidth();
double h = area.getHeight();
g2.setPaint(paint);
if (outlineType > 0) {
g2.setStroke(new BasicStroke(10.0f));
}
else {
g2.setStroke(new BasicStroke(DEFAULT_BORDER_SIZE));
}
int joinType = Arc2D.OPEN;
if (outlineType > 0) {
switch (dialType) {
case DIALTYPE_PIE:
joinType = Arc2D.PIE;
break;
case DIALTYPE_CHORD:
if (meterAngle > 180) {
joinType = Arc2D.CHORD;
}
else {
joinType = Arc2D.PIE;
}
break;
case DIALTYPE_CIRCLE:
joinType = Arc2D.PIE;
extent = 360;
break;
default:
throw new IllegalStateException("MeterPlot.drawArc(...): "
+ "dialType not recognised.");
}
}
Arc2D.Double arc = new Arc2D.Double(x, y, w, h, startAngle, extent, joinType);
if (outlineType > 0) {
g2.fill(arc);
}
else {
g2.draw(arc);
}
}
/**
* Calculate an angle ???
*
* @param value the value.
*
* @return the result.
*/
double calculateAngle(double value) {
value -= minMeterValue;
double ret = meterCalcAngle - ((value / meterRange) * meterAngle);
return ret;
}
/**
* Draws the ticks.
*
* @param g2 the graphics device.
* @param meterArea the meter area.
* @param minValue the minimum value.
* @param maxValue the maximum value.
*/
void drawTicks(Graphics2D g2, Rectangle2D meterArea, double minValue, double maxValue) {
int numberOfTicks = 20;
double diff = (maxValue - minValue) / numberOfTicks;
for (double i = minValue; i <= maxValue; i += diff) {
drawTick(g2, meterArea, i);
}
}
/**
* Draws a tick.
*
* @param g2 the graphics device.
* @param meterArea the meter area.
* @param value the value.
*/
void drawTick(Graphics2D g2, Rectangle2D meterArea, double value) {
drawTick(g2, meterArea, value, false, null, false, null);
}
/**
* Draws a tick.
*
* @param g2 the graphics device.
* @param meterArea the meter area.
* @param value the value.
* @param label the label.
* @param color the color.
*/
void drawTick(Graphics2D g2, Rectangle2D meterArea, double value, boolean label, Paint color) {
drawTick(g2, meterArea, value, label, color, false, null);
}
/**
* Draws a tick on the chart (also handles a special case [curValue=true] that draws the
* value in the middle of the dial).
*
* @param g2 the graphics device.
* @param meterArea the meter area.
* @param value the tick value.
* @param label a flag that controls whether or not a value label is drawn.
* @param labelPaint the label color.
* @param curValue a flag for the special case of the current value.
* @param units the unit-of-measure for the dial.
*/
void drawTick(Graphics2D g2, Rectangle2D meterArea,
double value, boolean label, Paint labelPaint, boolean curValue, String units) {
double valueAngle = calculateAngle(value);
double meterMiddleX = meterArea.getCenterX();
double meterMiddleY = meterArea.getCenterY();
if (labelPaint == null) {
labelPaint = Color.white;
}
g2.setPaint(labelPaint);
g2.setStroke(new BasicStroke(2.0f));
double valueP2X = 0;
double valueP2Y = 0;
if (!curValue) {
double radius = (meterArea.getWidth() / 2) + DEFAULT_BORDER_SIZE;
double radius1 = radius - 15;
double valueP1X = meterMiddleX + (radius * Math.cos(Math.PI * (valueAngle / 180)));
double valueP1Y = meterMiddleY - (radius * Math.sin(Math.PI * (valueAngle / 180)));
valueP2X = meterMiddleX + (radius1 * Math.cos(Math.PI * (valueAngle / 180)));
valueP2Y = meterMiddleY - (radius1 * Math.sin(Math.PI * (valueAngle / 180)));
Line2D.Double line = new Line2D.Double(valueP1X, valueP1Y, valueP2X, valueP2Y);
g2.draw(line);
}
else {
valueP2X = meterMiddleX;
valueP2Y = meterMiddleY;
valueAngle = 90;
}
if (this.tickLabelType == VALUE_LABELS && label) {
DecimalFormat df = new DecimalFormat("#,###,###,##0.00");
String tickLabel = df.format(value);
if (curValue && units != null) {
tickLabel += " " + units;
}
if (curValue) {
g2.setFont(getValueFont());
}
else {
if (tickLabelFont != null) {
g2.setFont(tickLabelFont);
}
}
Rectangle2D tickLabelBounds = g2.getFont().getStringBounds(tickLabel,
g2.getFontRenderContext());
double x = valueP2X;
double y = valueP2Y;
if (curValue) {
y += DEFAULT_CIRCLE_SIZE;
}
if (valueAngle == 90 || valueAngle == 270) {
x = x - tickLabelBounds.getWidth() / 2;
}
else if (valueAngle < 90 || valueAngle > 270) {
x = x - tickLabelBounds.getWidth();
}
if ((valueAngle > 135 && valueAngle < 225) || valueAngle > 315 || valueAngle < 45) {
y = y - tickLabelBounds.getHeight() / 2;
}
else {
y = y + tickLabelBounds.getHeight() / 2;
}
g2.drawString(tickLabel, (float) x, (float) y);
}
}
/**
* Returns a short string describing the type of plot.
*
* @return always <i>Meter Plot</i>.
*/
public String getPlotType() {
return "Meter Plot";
}
/**
* A zoom method that does nothing.
* <p>
* Plots are required to support the zoom operation. In the case of a pie
* chart, it doesn't make sense to zoom in or out, so the method is empty.
*
* @param percent The zoom percentage.
*/
public void zoom(double percent) {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -