
eu.hansolo.steelseries.gauges.RadialBargraph Maven / Gradle / Ivy
package eu.hansolo.steelseries.gauges;
/**
*
* @author Gerrit Grunwald
*/
public class RadialBargraph extends AbstractRadialBargraph
{
private double tickmarkOffset;
private double originCorrection;
private double apexAngle; // Angle where the values will be displayed
private double bargraphOffset;
private double ledTrackStartAngle;
private double ledTrackAngleExtend;
// One image to reduce memory consumption
private java.awt.image.BufferedImage bImage;
private java.awt.image.BufferedImage fImage;
private java.awt.image.BufferedImage disabledImage;
private eu.hansolo.steelseries.tools.ColorDef barGraphColor;
private java.awt.geom.Point2D center;
private java.awt.geom.Rectangle2D led;
private java.awt.geom.Point2D ledCenter;
private java.awt.Color[] ledColors;
private final float[] LED_FRACTIONS;
private java.awt.RadialGradientPaint ledGradient;
private final java.util.HashMap SECTION_GRADIENTS;
private final java.util.HashMap SECTION_ANGLES;
private final java.awt.geom.Point2D LCD_POSITION;
private final java.awt.Dimension LCD_DIM = new java.awt.Dimension();
private final java.awt.font.FontRenderContext RENDER_CONTEXT = new java.awt.font.FontRenderContext(null, true, true);
private java.awt.font.TextLayout unitLayout;
private final java.awt.geom.Rectangle2D UNIT_BOUNDARY = new java.awt.geom.Rectangle2D.Double();
private java.awt.font.TextLayout valueLayout;
private final java.awt.geom.Rectangle2D VALUE_BOUNDARY = new java.awt.geom.Rectangle2D.Double();
public RadialBargraph()
{
super();
LCD_POSITION = new java.awt.geom.Point2D.Double();
switch(getGaugeType())
{
case TYPE1:
tickmarkOffset = 0.5 * Math.PI;
originCorrection = 180;
apexAngle = 90; // Angle where the values will be displayed
bargraphOffset = 0;
break;
case TYPE2:
tickmarkOffset = 0.5 * Math.PI;
originCorrection = 180;
apexAngle = 180; // Angle where the values will be displayed
bargraphOffset = 0;
break;
case TYPE3:
tickmarkOffset = 0.0;
originCorrection = 270;
apexAngle = 270; // Angle where the values will be displayed
bargraphOffset = -90;
break;
case TYPE4:
tickmarkOffset = 0.0;
originCorrection = 240;
apexAngle = 300; // Angle where the values will be displayed
bargraphOffset = -60;
break;
default:
tickmarkOffset = 0.0;
originCorrection = 240;
apexAngle = 300; // Angle where the values will be displayed
bargraphOffset = -60;
break;
}
barGraphColor = eu.hansolo.steelseries.tools.ColorDef.RED;
LED_FRACTIONS = new float[]
{
0.0f,
1.0f
};
SECTION_GRADIENTS = new java.util.HashMap(4);
SECTION_ANGLES = new java.util.HashMap(4);
ledTrackStartAngle = originCorrection - (0 * (apexAngle / (getMaxValue() - getMinValue())));
ledTrackAngleExtend = -(getMaxValue() - getMinValue()) * (apexAngle / (getMaxValue() - getMinValue()));
calcBargraphTrack();
prepareBargraph(getInnerBounds().width);
setTickLabelPeriod(20);
setLcdVisible(true);
}
@Override
public final AbstractGauge init(final int WIDTH, final int HEIGHT)
{
switch(getGaugeType())
{
case TYPE1:
tickmarkOffset = 0.5 * Math.PI;
originCorrection = 180;
apexAngle = 90; // Angle where the values will be displayed
bargraphOffset = 0;
break;
case TYPE2:
tickmarkOffset = 0.5 * Math.PI;
originCorrection = 180;
apexAngle = 180; // Angle where the values will be displayed
bargraphOffset = 0;
break;
case TYPE3:
tickmarkOffset = 0.0;
originCorrection = 270;
apexAngle = 270; // Angle where the values will be displayed
bargraphOffset = -90;
break;
case TYPE4:
tickmarkOffset = 0.0;
originCorrection = 240;
apexAngle = 300; // Angle where the values will be displayed
bargraphOffset = -60;
break;
default:
tickmarkOffset = 0.0;
originCorrection = 240;
apexAngle = 300; // Angle where the values will be displayed
bargraphOffset = -60;
break;
}
if (isDigitalFont())
{
setLcdValueFont(LCD_DIGITAL_FONT.deriveFont(0.7f * WIDTH * 0.15f));
}
else
{
setLcdValueFont(LCD_STANDARD_FONT.deriveFont(0.625f * WIDTH * 0.15f));
}
if (getUseCustomLcdUnitFont())
{
setLcdUnitFont(getCustomLcdUnitFont().deriveFont(0.25f * WIDTH * 0.15f));
}
else
{
setLcdUnitFont(LCD_STANDARD_FONT.deriveFont(0.25f * WIDTH * 0.15f));
}
ledTrackStartAngle = originCorrection - (0 * (apexAngle / (getMaxValue() - getMinValue())));
ledTrackAngleExtend = -(getMaxValue() - getMinValue()) * (apexAngle / (getMaxValue() - getMinValue()));
calcBargraphTrack();
prepareBargraph(WIDTH);
// Create Background Image
if (bImage != null)
{
bImage.flush();
}
bImage = create_Image(WIDTH, WIDTH);
// Create Foreground Image
if (fImage != null)
{
fImage.flush();
}
fImage = create_Image(WIDTH, WIDTH);
if (isFrameVisible())
{
switch (getFrameType())
{
case ROUND:
FRAME_FACTORY.createRadialFrame(WIDTH, getFrameDesign(), getCustomFrameDesign(), isFrame3dEffectVisible(), bImage);
break;
case SQUARE:
FRAME_FACTORY.createLinearFrame(WIDTH, WIDTH, getFrameDesign(), getCustomFrameDesign(), isFrame3dEffectVisible(), bImage);
break;
default:
FRAME_FACTORY.createRadialFrame(WIDTH, getFrameDesign(), getCustomFrameDesign(), isFrame3dEffectVisible(), bImage);
break;
}
}
if (isBackgroundVisible())
{
create_BACKGROUND_Image(WIDTH, "", "", bImage);
}
create_BARGRAPH_TRACK_Image(WIDTH, ledTrackStartAngle, ledTrackAngleExtend, apexAngle, bargraphOffset, bImage);
create_TITLE_Image(WIDTH, getTitle(), getUnitString(), bImage);
if (isLcdVisible())
{
create_LCD_Image(new java.awt.geom.Rectangle2D.Double(((getGaugeBounds().width - WIDTH * 0.48) / 2.0), (getGaugeBounds().height * 0.425), (WIDTH * 0.48), (WIDTH * 0.15)), getLcdColor(), getCustomLcdBackground(), bImage);
LCD_POSITION.setLocation(((getGaugeBounds().width - WIDTH * 0.48) / 2.0), (getGaugeBounds().height * 0.425));
LCD_DIM.setSize(WIDTH * 0.48, WIDTH * 0.15);
}
create_TICKMARKS_Image(WIDTH, getFreeAreaAngle(), tickmarkOffset, getMinValue(), getMaxValue(), getAngleStep(), getTickLabelPeriod(), 0, getScaleDividerPower(), false, isDrawTickLabels(), getTickmarkSections(), 0.38f, 0.09f, new java.awt.geom.Point2D.Double(WIDTH / 2.0, WIDTH / 2.0), eu.hansolo.steelseries.tools.Direction.CLOCKWISE, null, bImage);
if (isForegroundVisible())
{
FOREGROUND_FACTORY.createRadialForeground(WIDTH, false, getForegroundType(), fImage);
}
if (disabledImage != null)
{
disabledImage.flush();
}
disabledImage = create_DISABLED_Image(WIDTH);
setCurrentLedImage(getLedImageOff());
return this;
}
@Override
protected void paintComponent(java.awt.Graphics g)
{
if (!isInitialized())
{
return;
}
final java.awt.Graphics2D G2 = (java.awt.Graphics2D) g.create();
G2.setRenderingHint(java.awt.RenderingHints.KEY_ANTIALIASING, java.awt.RenderingHints.VALUE_ANTIALIAS_ON);
G2.setRenderingHint(java.awt.RenderingHints.KEY_RENDERING, java.awt.RenderingHints.VALUE_RENDER_QUALITY);
//G2.setRenderingHint(java.awt.RenderingHints.KEY_DITHERING, java.awt.RenderingHints.VALUE_DITHER_ENABLE);
//G2.setRenderingHint(java.awt.RenderingHints.KEY_ALPHA_INTERPOLATION, java.awt.RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
//G2.setRenderingHint(java.awt.RenderingHints.KEY_COLOR_RENDERING, java.awt.RenderingHints.VALUE_COLOR_RENDER_QUALITY);
G2.setRenderingHint(java.awt.RenderingHints.KEY_STROKE_CONTROL, java.awt.RenderingHints.VALUE_STROKE_PURE);
//G2.setRenderingHint(java.awt.RenderingHints.KEY_FRACTIONALMETRICS, java.awt.RenderingHints.VALUE_FRACTIONALMETRICS_ON);
G2.setRenderingHint(java.awt.RenderingHints.KEY_TEXT_ANTIALIASING, java.awt.RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
// Translate coordinate system related to insets
G2.translate(getInnerBounds().x, getInnerBounds().y);
// Draw combined background image
G2.drawImage(bImage, 0, 0, null);
// Draw LED if enabled
if (isLedVisible())
{
G2.drawImage(getCurrentLedImage(), (int) (getGaugeBounds().width * getLedPositionX()), (int) (getGaugeBounds().height * getLedPositionY()), null);
}
// Draw LCD display
if (isLcdVisible())
{
if (getLcdColor() == eu.hansolo.steelseries.tools.LcdColor.CUSTOM)
{
G2.setColor(getCustomLcdForeground());
}
else
{
G2.setColor(getLcdColor().TEXT_COLOR);
}
G2.setFont(getLcdUnitFont());
final double UNIT_STRING_WIDTH;
if (isLcdUnitStringVisible())
{
unitLayout = new java.awt.font.TextLayout(getLcdUnitString(), G2.getFont(), RENDER_CONTEXT);
UNIT_BOUNDARY.setFrame(unitLayout.getBounds());
G2.drawString(getLcdUnitString(), (int) (LCD_POSITION.getX() + (LCD_DIM.width - UNIT_BOUNDARY.getWidth()) - LCD_DIM.width * 0.03), (int) (LCD_POSITION.getY() + LCD_DIM.height * 0.76f));
UNIT_STRING_WIDTH = UNIT_BOUNDARY.getWidth();
}
else
{
UNIT_STRING_WIDTH = 0;
}
G2.setFont(getLcdValueFont());
valueLayout = new java.awt.font.TextLayout(formatLcdValue(getLcdValue()), G2.getFont(), RENDER_CONTEXT);
VALUE_BOUNDARY.setFrame(valueLayout.getBounds());
G2.drawString(formatLcdValue(getLcdValue()), (int) (LCD_POSITION.getX() + (LCD_DIM.width - UNIT_STRING_WIDTH - VALUE_BOUNDARY.getWidth()) - LCD_DIM.width * 0.09), (int) (LCD_POSITION.getY() + LCD_DIM.height * 0.76f));
}
// Draw the active leds in dependence on the current value
final java.awt.geom.AffineTransform OLD_TRANSFORM = G2.getTransform();
final double ACTIVE_LED_ANGLE = ((getValue() + Math.abs(getMinValue())) / (getMaxValue() - getMinValue())) * apexAngle;
// Use default bargraph color if sections not visible
if (!isSectionsVisible())
{
G2.setPaint(ledGradient);
}
for (double angle = 0 ; angle <= ACTIVE_LED_ANGLE ; angle += 5.0)
{
// If sections visible, color the bargraph with the given section colors
if (isSectionsVisible())
{
for (eu.hansolo.steelseries.tools.Section section : getSections())
{
if (angle >= SECTION_ANGLES.get(section).getX() && angle < SECTION_ANGLES.get(section).getY())
{
G2.setPaint(SECTION_GRADIENTS.get(section));
}
}
}
G2.rotate(Math.toRadians(angle + bargraphOffset), center.getX(), center.getY());
G2.fill(led);
G2.setTransform(OLD_TRANSFORM);
}
// Draw peak value if enabled
if (isPeakValueEnabled() && isPeakValueVisible())
{
G2.rotate(Math.toRadians(((getPeakValue() + Math.abs(getMinValue())) / (getMaxValue() - getMinValue())) * apexAngle), center.getX(), center.getY());
G2.fill(led);
G2.setTransform(OLD_TRANSFORM);
}
// Draw the foreground
if (isForegroundVisible())
{
G2.drawImage(fImage, 0, 0, null);
}
if (!isEnabled())
{
G2.drawImage(disabledImage, 0, 0, null);
}
// Translate coordinate system back to original
G2.translate(-getInnerBounds().x, -getInnerBounds().y);
G2.dispose();
}
@Override
public eu.hansolo.steelseries.tools.ColorDef getBarGraphColor()
{
return this.barGraphColor;
}
@Override
public void setBarGraphColor(final eu.hansolo.steelseries.tools.ColorDef BARGRAPH_COLOR)
{
this.barGraphColor = BARGRAPH_COLOR;
init(getInnerBounds().width, getInnerBounds().height);
repaint(getInnerBounds());
}
private void prepareBargraph(final int WIDTH)
{
center = new java.awt.geom.Point2D.Double(WIDTH / 2.0 + getInnerBounds().x, WIDTH / 2.0 + getInnerBounds().y);
led = new java.awt.geom.Rectangle2D.Double(WIDTH * 0.1168224299, WIDTH * 0.4859813084, WIDTH * 0.06074766355140187, WIDTH * 0.023364486);
ledCenter = new java.awt.geom.Point2D.Double(led.getCenterX(), led.getCenterY());
if (!getSections().isEmpty())
{
SECTION_GRADIENTS.clear();
SECTION_ANGLES.clear();
for (eu.hansolo.steelseries.tools.Section section : getSections())
{
SECTION_GRADIENTS.put(section, new java.awt.RadialGradientPaint(ledCenter, (float)(0.030373831775700934 * WIDTH), LED_FRACTIONS, new java.awt.Color[]{section.getColor().brighter(), section.getColor().darker()}));
SECTION_ANGLES.put(section, new java.awt.geom.Point2D.Double((((section.getStart() + Math.abs(getMinValue())) / (getMaxValue() - getMinValue())) * apexAngle), (((section.getStop() + Math.abs(getMinValue())) / (getMaxValue() - getMinValue())) * apexAngle)));
}
}
if (barGraphColor != eu.hansolo.steelseries.tools.ColorDef.CUSTOM)
{
ledColors = new java.awt.Color[]
{
barGraphColor.LIGHT,
barGraphColor.DARK
};
}
else
{
ledColors = new java.awt.Color[]
{
getCustomBarGraphColorObject().LIGHT,
getCustomBarGraphColorObject().DARK
};
}
ledGradient = new java.awt.RadialGradientPaint(ledCenter, (float)(0.030373831775700934 * WIDTH), LED_FRACTIONS, ledColors);
}
private void calcBargraphTrack()
{
ledTrackStartAngle = originCorrection;
ledTrackAngleExtend = -(getMaxValue() - getMinValue()) * (apexAngle / (getMaxValue() - getMinValue()));
}
@Override
public void setValue(double value)
{
super.setValue(value);
if (isValueCoupled())
{
setLcdValue(value);
}
repaint(getInnerBounds());
}
@Override
public java.awt.geom.Point2D getCenter()
{
return new java.awt.geom.Point2D.Double(getInnerBounds().getCenterX() + getInnerBounds().x, getInnerBounds().getCenterX() + getInnerBounds().y);
}
@Override
public java.awt.geom.Rectangle2D getBounds2D()
{
return getInnerBounds();
}
@Override
public String toString()
{
return "RadialBargraph " + getGaugeType();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy