All Downloads are FREE. Search and download functionalities are using the official Maven repository.

eu.hansolo.steelseries.gauges.DisplayCircular Maven / Gradle / Ivy

Go to download

The SteelSeries is a javabeans component library that contains gauges. You will find linear and radial gauges. In addition you will also find digital displays, indicators from cars and some instruments from airplanes and sailboats.

There is a newer version: 3.9.30
Show newest version
package eu.hansolo.steelseries.gauges;


/**
 *
 * @author hansolo
 */
public final class DisplayCircular extends AbstractRadial
{
    private java.awt.image.BufferedImage frameImage;    
    private java.awt.image.BufferedImage backgroundImage;
    private java.awt.image.BufferedImage titleImage;
    private java.awt.image.BufferedImage lcdImage;
    private java.awt.image.BufferedImage foregroundImage;
    private java.awt.image.BufferedImage disabledImage;
    private java.awt.Font lcdFormerValueFont;
    private double oldValue;    
    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();
    private java.awt.font.TextLayout oldValueLayout;
    private final java.awt.geom.Rectangle2D OLD_VALUE_BOUNDARY = new java.awt.geom.Rectangle2D.Double();
                

    public DisplayCircular()
    {
        super();
        setLcdVisible(true);        
        oldValue = 0;        
        init(getInnerBounds().width, getInnerBounds().height);
    }

    @Override
    public final AbstractGauge init(final int WIDTH, final int HEIGHT)
    {                                        
        if (frameImage != null)
        {
            frameImage.flush();
        }
        frameImage = create_FRAME_Image(WIDTH);                                        

        if (backgroundImage != null)
        {
            backgroundImage.flush();                        
        }
        backgroundImage = create_BACKGROUND_Image(WIDTH);

        if (titleImage != null)
        {
            titleImage.flush();
        }
        titleImage = create_TITLE_Image(WIDTH, getTitle(), getUnitString());

        if (lcdImage != null)
        {
            lcdImage.flush();
        }                    
        lcdImage = create_LCD_Image((int) (getWidth() * 0.6542056075f), (int) (getHeight() * 0.2990654206f), getLcdColor(), getCustomLcdBackground());                                                            

        if (foregroundImage != null)
        {
            foregroundImage.flush();
        }
        foregroundImage = create_FOREGROUND_Image(WIDTH, false, getForegroundType());

        if (disabledImage != null)
        {
            disabledImage.flush();
        }
        disabledImage = create_DISABLED_Image(WIDTH);                    
                
        if (lcdImage != null)
        {
            if (isDigitalFont())
            {
                setLcdValueFont(LCD_DIGITAL_FONT.deriveFont(0.5f * lcdImage.getHeight()));    
                lcdFormerValueFont = LCD_DIGITAL_FONT.deriveFont(0.2f * lcdImage.getHeight());
            }
            else
            {
                setLcdValueFont(LCD_STANDARD_FONT.deriveFont(0.46875f * lcdImage.getHeight()));     
                lcdFormerValueFont = LCD_STANDARD_FONT.deriveFont(0.1875f * lcdImage.getHeight());
            }

            if (getUseCustomLcdUnitFont())
            {
                setLcdUnitFont(getCustomLcdUnitFont().deriveFont(0.1875f * lcdImage.getHeight()));
            }
            else
            {
                setLcdUnitFont(LCD_STANDARD_FONT.deriveFont(0.1875f * lcdImage.getHeight()));
            }
        }

        return this;
    }

    @Override
    protected void paintComponent(java.awt.Graphics g)
    {
        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 the coordinate system related to insets
        G2.translate(getInnerBounds().x, getInnerBounds().y);

        // Draw the frame
        if (isFrameVisible())
        {
            G2.drawImage(frameImage, 0, 0, null);
        }

        // Draw the background
        if (isBackgroundVisible())
        {
            G2.drawImage(backgroundImage, 0, 0, null);
        }

        // Draw LCD display
        if (isLcdVisible() && lcdImage != null)
        {
            G2.drawImage(lcdImage, (int) ((getGaugeBounds().width - lcdImage.getWidth()) / 2.0), (int) ((getGaugeBounds().height - lcdImage.getHeight()) / 2.0), null);

            // Draw lcd text
            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() && !getLcdUnitString().isEmpty())
            {
                unitLayout = new java.awt.font.TextLayout(getLcdUnitString(), G2.getFont(), RENDER_CONTEXT);
                UNIT_BOUNDARY.setFrame(unitLayout.getBounds());
                G2.drawString(getLcdUnitString(), (int) ((lcdImage.getWidth() - UNIT_BOUNDARY.getWidth()) - lcdImage.getWidth() * 0.03f + (getGaugeBounds().width - lcdImage.getWidth()) / 2.0), (int) (lcdImage.getHeight() * 0.6f + (getGaugeBounds().height - lcdImage.getHeight()) / 2.0));
                UNIT_STRING_WIDTH = UNIT_BOUNDARY.getWidth();
            }
            else
            {
                UNIT_STRING_WIDTH = 0;
            }

            // Draw value
            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) ((lcdImage.getWidth() - UNIT_STRING_WIDTH - VALUE_BOUNDARY.getWidth()) - lcdImage.getWidth() * 0.09 + ((getGaugeBounds().width - lcdImage.getWidth()) / 2.0)), (int) (lcdImage.getHeight() * 0.6f + (getGaugeBounds().height - lcdImage.getHeight()) / 2.0));

            // Draw oldValue
            G2.setFont(lcdFormerValueFont);
            oldValueLayout = new java.awt.font.TextLayout(formatLcdValue(oldValue), G2.getFont(), RENDER_CONTEXT);
            OLD_VALUE_BOUNDARY.setFrame(oldValueLayout.getBounds());
            G2.drawString(formatLcdValue(oldValue), (int) ((lcdImage.getWidth() - OLD_VALUE_BOUNDARY.getWidth()) / 2f + (getGaugeBounds().width - lcdImage.getWidth()) / 2.0), (int) (lcdImage.getHeight() * 0.9f + (getGaugeBounds().height - lcdImage.getHeight()) / 2.0));
        }
                        
        // Draw the foreground
        if (isForegroundVisible())
        {
            G2.drawImage(foregroundImage, 0, 0, null);
        }
        
        if (!isEnabled())
        {
            G2.drawImage(disabledImage, 0, 0, null);
        }
        
        // Translate the coordinate system back to original
        G2.translate(-getInnerBounds().x, -getInnerBounds().y);

        G2.dispose();
    }
    
    @Override
    public void setLcdValue(final double VALUE)
    {
        oldValue = getLcdValue();
        super.setLcdValue(VALUE);                        
    }
    
    @Override
    public java.awt.geom.Point2D getCenter()
    {
        return new java.awt.geom.Point2D.Double(backgroundImage.getWidth() / 2.0 + getInnerBounds().x, backgroundImage.getHeight() / 2.0 + getInnerBounds().y);
    }

    @Override
    public java.awt.geom.Rectangle2D getBounds2D()
    {
        return new java.awt.geom.Rectangle2D.Double(backgroundImage.getMinX(), backgroundImage.getMinY(), backgroundImage.getWidth(), backgroundImage.getHeight());
    }

    @Override
    public String toString()
    {
        return "DisplayCircular";
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy