?? meterplot.java
字號:
break;
default:
return;
}
// 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,
maxValue,
paint);
// }
// draw a tick at each end of the range...
drawTick(g2, meterArea, minValue, true, paint);
drawTick(g2, meterArea, maxValue, 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) {
if (this.shape == DialShape.PIE) {
joinType = Arc2D.PIE;
}
else if (this.shape == DialShape.CHORD) {
if (meterAngle > 180) {
joinType = Arc2D.CHORD;
}
else {
joinType = Arc2D.PIE;
}
}
else if (this.shape == DialShape.CIRCLE) {
joinType = Arc2D.PIE;
extent = 360;
}
else {
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 localizationResources.getString("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) {
}
/**
* Tests an object for equality with this plot.
*
* @param object the object.
*
* @return A boolean.
*/
public boolean equals(Object object) {
if (object == null) {
return false;
}
if (object == this) {
return true;
}
if (object instanceof MeterPlot && super.equals(object)) {
MeterPlot p = (MeterPlot) object;
//private ValueDataset dataset <-- ignored
boolean b0 = (this.units.equals(p.units));
boolean b1 = (this.range.equals(p.range));
boolean b2 = (this.normalRange.equals(p.normalRange));
boolean b3 = (this.warningRange.equals(p.warningRange));
boolean b4 = (this.criticalRange.equals(p.criticalRange));
boolean b5 = ObjectUtils.equal(this.dialOutlinePaint, p.dialOutlinePaint);
boolean b6 = ObjectUtils.equal(this.normalPaint, p.normalPaint);
boolean b7 = ObjectUtils.equal(this.warningPaint, p.warningPaint);
boolean b8 = ObjectUtils.equal(this.criticalPaint, p.criticalPaint);
boolean b9 = (this.shape == p.shape);
boolean b10 = ObjectUtils.equal(this.dialBackgroundPaint, p.dialBackgroundPaint);
boolean b11 = ObjectUtils.equal(this.needlePaint, p.needlePaint);
boolean b12 = ObjectUtils.equal(this.valueFont, p.valueFont);
boolean b13 = ObjectUtils.equal(this.valuePaint, p.valuePaint);
//private int tickLabelType;
//private Font tickLabelFont;
//private boolean drawBorder;
//private int meterCalcAngle = -1;
//private double meterRange = -1;
//private int meterAngle = DEFAULT_METER_ANGLE;
//private double minMeterValue = 0.0;
return b0 && b1 && b2 && b3 && b4 && b5 && b6 && b7 && b8 && b9
&& b10 && b11 && b12 && b13;
}
return false;
}
/**
* Provides serialization support.
*
* @param stream the output stream.
*
* @throws IOException if there is an I/O error.
*/
private void writeObject(ObjectOutputStream stream) throws IOException {
stream.defaultWriteObject();
SerialUtilities.writePaint(this.criticalPaint, stream);
SerialUtilities.writePaint(this.dialBackgroundPaint, stream);
SerialUtilities.writePaint(this.needlePaint, stream);
SerialUtilities.writePaint(this.normalPaint, stream);
SerialUtilities.writePaint(this.valuePaint, stream);
SerialUtilities.writePaint(this.warningPaint, stream);
}
/**
* Provides serialization support.
*
* @param stream the input stream.
*
* @throws IOException if there is an I/O error.
* @throws ClassNotFoundException if there is a classpath problem.
*/
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
stream.defaultReadObject();
this.criticalPaint = SerialUtilities.readPaint(stream);
this.dialBackgroundPaint = SerialUtilities.readPaint(stream);
this.needlePaint = SerialUtilities.readPaint(stream);
this.normalPaint = SerialUtilities.readPaint(stream);
this.valuePaint = SerialUtilities.readPaint(stream);
this.warningPaint = SerialUtilities.readPaint(stream);
if (dataset != null) dataset.addChangeListener(this);
}
///////////////////////////////////////////////////////////////////////////////////////////////
// DEPRECATED
///////////////////////////////////////////////////////////////////////////////////////////////
/** Constant for meter type 'pie'. */
public static final int DIALTYPE_PIE = 0;
/** Constant for meter type 'circle'. */
public static final int DIALTYPE_CIRCLE = 1;
/** Constant for meter type 'chord'. */
public static final int DIALTYPE_CHORD = 2;
/**
* Returns the type of dial (DIALTYPE_PIE, DIALTYPE_CIRCLE, DIALTYPE_CHORD).
*
* @return The dial type.
*
* @deprecated Use getDialShape().
*/
public int getDialType() {
if (this.shape == DialShape.CIRCLE) {
return MeterPlot.DIALTYPE_CIRCLE;
}
else if (this.shape == DialShape.CHORD) {
return MeterPlot.DIALTYPE_CHORD;
}
else if (this.shape == DialShape.PIE) {
return MeterPlot.DIALTYPE_PIE;
}
else {
throw new IllegalStateException("MeterPlot.getDialType: unrecognised dial type.");
}
}
/**
* Sets the dial type (background shape).
* <P>
* This controls the shape of the dial background. Use one of the constants:
* DIALTYPE_PIE, DIALTYPE_CIRCLE, or DIALTYPE_CHORD.
*
* @param type The dial type.
*
* @deprecated Use setDialShape(...).
*/
public void setDialType(int type) {
switch (type) {
case MeterPlot.DIALTYPE_CIRCLE:
setDialShape(DialShape.CIRCLE);
break;
case MeterPlot.DIALTYPE_CHORD:
setDialShape(DialShape.CHORD);
break;
case MeterPlot.DIALTYPE_PIE:
setDialShape(DialShape.PIE);
break;
default:
throw new IllegalArgumentException("MeterPlot.setDialType: unrecognised type.");
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -