public GaugeFigure() { super(); transparent = true; scale.setScaleLineVisible(false); scale.setTickLabelSide(LabelSide.Secondary); ramp.setRampWidth(10); valueLabel = new Label(); valueLabel.setFont(DEFAULT_LABEL_FONT); needle = new Needle(); needle.setFill(true); needle.setOutline(false); needleCenter = new NeedleCenter(); needleCenter.setOutline(false); setLayoutManager(new GaugeLayout()); add(ramp, GaugeLayout.RAMP); add(scale, GaugeLayout.SCALE); add(valueLabel, GaugeLayout.VALUE_LABEL); add(needle, GaugeLayout.NEEDLE); add(needleCenter, GaugeLayout.NEEDLE_CENTER); addFigureListener( new FigureListener() { public void figureMoved(IFigure source) { ramp.setDirty(true); revalidate(); } }); }
public void layout(IFigure container) { Rectangle area = container.getClientArea(); area.width = Math.min(area.width, area.height); area.height = area.width; area.shrink(BORDER_WIDTH, BORDER_WIDTH); Point center = area.getCenter(); if (scale != null) { scale.setBounds(area); } if (ramp != null && ramp.isVisible()) { Rectangle rampBounds = area.getCopy(); ramp.setBounds(rampBounds.shrink(area.width / 4, area.height / 4)); } if (valueLabel != null) { Dimension labelSize = valueLabel.getPreferredSize(); valueLabel.setBounds( new Rectangle( area.x + area.width / 2 - labelSize.width / 2, area.y + area.height * 7 / 8 - labelSize.height / 2, labelSize.width, labelSize.height)); } if (needle != null && scale != null) { needlePoints.setPoint(new Point(center.x, center.y - NeedleCenter.DIAMETER / 2 + 3), 0); scale.getScaleTickMarks(); needlePoints.setPoint( new Point( center.x + area.width / 2 - RoundScaleTickMarks.MAJOR_TICK_LENGTH - GAP_BTW_NEEDLE_SCALE, center.y), 1); needlePoints.setPoint(new Point(center.x, center.y + NeedleCenter.DIAMETER / 2 - 3), 2); double valuePosition = 360 - scale.getValuePosition(getCoercedValue(), false); if (maximum > minimum) { if (value > maximum) valuePosition += 10; else if (value < minimum) valuePosition -= 10; } else { if (value > minimum) valuePosition -= 10; else if (value < maximum) valuePosition += 10; } needlePoints.setPoint( PointsUtil.rotate(needlePoints.getPoint(0), valuePosition, center), 0); needlePoints.setPoint( PointsUtil.rotate(needlePoints.getPoint(1), valuePosition, center), 1); needlePoints.setPoint( PointsUtil.rotate(needlePoints.getPoint(2), valuePosition, center), 2); needle.setPoints(needlePoints); } if (needleCenter != null) { needleCenter.setBounds( new Rectangle( center.x - NeedleCenter.DIAMETER / 2, center.y - NeedleCenter.DIAMETER / 2, NeedleCenter.DIAMETER, NeedleCenter.DIAMETER)); } }