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

eu.hansolo.steelseries.extras.Led 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.extras;


/**
 *
 * @author hansolo
 */
public class Led extends javax.swing.JComponent implements java.awt.event.ActionListener, java.awt.event.ComponentListener
{
    // 
    private static final eu.hansolo.steelseries.tools.Util UTIL = eu.hansolo.steelseries.tools.Util.INSTANCE;
    private final java.awt.Rectangle INNER_BOUNDS;
    private final java.awt.GraphicsConfiguration GFX_CONF = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
    private eu.hansolo.steelseries.tools.LedColor ledColor = eu.hansolo.steelseries.tools.LedColor.RED_LED;
    private eu.hansolo.steelseries.tools.CustomLedColor customLedColor = new eu.hansolo.steelseries.tools.CustomLedColor(java.awt.Color.RED);
    private java.awt.image.BufferedImage ledImageOff = create_LED_Image(16, 0, ledColor);
    private java.awt.image.BufferedImage ledImageOn = create_LED_Image(16, 1, ledColor);
    private java.awt.image.BufferedImage currentLedImage;
    private final javax.swing.Timer LED_BLINKING_TIMER = new javax.swing.Timer(500, this);
    private boolean ledBlinking;
    private boolean ledOn;
    private boolean initialized;
    // 

    // 
    public Led()
    {
        super();
        addComponentListener(this);
        INNER_BOUNDS = new java.awt.Rectangle(getPreferredSize()); 
        ledBlinking = false;
        ledOn = false;
        initialized = false;                        
        init(INNER_BOUNDS.width);
    }
    // 
    
    // 
    private void init(final int WIDTH)
    {
        if (ledImageOff != null)
        {
            ledImageOff.flush();
        }
        ledImageOff = create_LED_Image(WIDTH, 0, ledColor);
        
        if (ledImageOn != null)
        {
            ledImageOn.flush();
        }
        ledImageOn = create_LED_Image(WIDTH, 1, ledColor);

        if (ledOn)
        {
            setCurrentLedImage(ledImageOn);
        }
        else
        {
            setCurrentLedImage(ledImageOff);
        }
    }
    // 
    
    @Override
    protected void paintComponent(java.awt.Graphics g)
    {
        if (!initialized)
        {
            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);

        G2.translate(INNER_BOUNDS.x, INNER_BOUNDS.y); 
        
        G2.drawImage(getCurrentLedImage(), 0, 0, null);
        
        G2.translate(-INNER_BOUNDS.x, -INNER_BOUNDS.y); 
        
        G2.dispose();
    }

    // 
    /**
     * Returns the color of led.
     * The LedColor is not a standard color but defines a
     * color scheme for the led. The default ledcolor is RED
     * @return the selected the color for the led
     */
    public eu.hansolo.steelseries.tools.LedColor getLedColor()
    {
        return this.ledColor;
    }

    /**
     * Sets the color of the threshold led.
     * The LedColor is not a standard color but defines a
     * color scheme for the led. The default ledcolor is RED
     * @param LED_COLOR
     */
    public void setLedColor(final eu.hansolo.steelseries.tools.LedColor LED_COLOR)
    {
        if (LED_COLOR == null)
        {
            this.ledColor = eu.hansolo.steelseries.tools.LedColor.RED_LED;
        }
        else
        {
            this.ledColor = LED_COLOR;
        }
        final boolean LED_WAS_ON = currentLedImage.equals(ledImageOn) ? true : false;

        ledImageOff = create_LED_Image(getWidth(), 0, LED_COLOR);
        ledImageOn = create_LED_Image(getWidth(), 1, LED_COLOR);

        currentLedImage = LED_WAS_ON == true ? ledImageOn : ledImageOff;

        repaint();
    }

    /**
     * Returns the color that will be used to calculate the custom led color
     * @return the color that will be used to calculate the custom led color
     */
    public java.awt.Color getCustomLedColor()
    {
        return this.customLedColor.COLOR;
    }
    
    /**
     * Sets the color that will be used to calculate the custom led color
     * @param COLOR 
     */
    public void setCustomLedColor(final java.awt.Color COLOR)
    {
        this.customLedColor = new eu.hansolo.steelseries.tools.CustomLedColor(COLOR);
        final boolean LED_WAS_ON = currentLedImage.equals(ledImageOn) ? true : false;

        ledImageOff = create_LED_Image(getWidth(), 0, ledColor);
        ledImageOn = create_LED_Image(getWidth(), 1, ledColor);

        currentLedImage = LED_WAS_ON == true ? ledImageOn : ledImageOff;

        repaint();
    }
    
    /**
     * Returns the object that represents the custom led color
     * @return the object that represents the custom led color
     */
    public eu.hansolo.steelseries.tools.CustomLedColor getCustomLedColorObject()
    {
        return this.customLedColor;
    }
    
    /**
     * Returns true if the led is on
     * @return true if the led is on
     */
    public boolean isLedOn()
    {
        return this.ledOn;
    }

    /**
     * Sets the state of the led
     * @param LED_ON 
     */
    public void setLedOn(final boolean LED_ON)
    {
        this.ledOn = LED_ON;
        init(getWidth());
        repaint();
    }

    /**
     * Returns the state of the threshold led.
     * The led could blink which will be triggered by a javax.swing.Timer
     * that triggers every 500 ms. The blinking will be done by switching
     * between two images.
     * @return true if the led is blinking
     */
    public boolean isLedBlinking()
    {
        return this.ledBlinking;
    }

    /**
     * Sets the state of the threshold led.
     * The led could blink which will be triggered by a javax.swing.Timer
     * that triggers every 500 ms. The blinking will be done by switching
     * between two images.
     * @param LED_BLINKING
     */
    public void setLedBlinking(final boolean LED_BLINKING)
    {
        this.ledBlinking = LED_BLINKING;
        if (LED_BLINKING)
        {
            LED_BLINKING_TIMER.start();
        }
        else
        {
            setCurrentLedImage(getLedImageOff());
            LED_BLINKING_TIMER.stop();
        }
    }
    // 
    
    // 
    /**
     * Returns the image of the switched on threshold led
     * with the currently active ledcolor.
     * @return the image of the led with the state active
     * and the selected led color
     */
    private java.awt.image.BufferedImage getLedImageOn()
    {
        return this.ledImageOn;
    }

    /**
     * Returns the image of the switched off threshold led
     * with the currently active ledcolor.
     * @return the image of the led with the state inactive
     * and the selected led color
     */
    private java.awt.image.BufferedImage getLedImageOff()
    {
        return this.ledImageOff;
    }

    /**
     * Returns the image of the currently used led image.
     * @return the led image at the moment (depends on blinking)
     */
    private java.awt.image.BufferedImage getCurrentLedImage()
    {
        return this.currentLedImage;
    }

    /**
     * Sets the image of the currently used led image.
     * @param CURRENT_LED_IMAGE
     */
    private void setCurrentLedImage(final java.awt.image.BufferedImage CURRENT_LED_IMAGE)
    {
        this.currentLedImage = CURRENT_LED_IMAGE;
        repaint(INNER_BOUNDS);
    }

    /**
     * Returns a buffered image that represents a led with the given size, state and color
     * @param SIZE
     * @param STATE
     * @param LED_COLOR
     * @return a buffered image that represents a led with the given size, state and color
     */
    private java.awt.image.BufferedImage create_LED_Image(final int SIZE, final int STATE, final eu.hansolo.steelseries.tools.LedColor LED_COLOR)
    {
        if (SIZE <= 0)
        {
            return GFX_CONF.createCompatibleImage(10, 10, java.awt.Transparency.TRANSLUCENT);
        }
        
        final java.awt.image.BufferedImage IMAGE = GFX_CONF.createCompatibleImage(SIZE, SIZE, java.awt.Transparency.TRANSLUCENT);
        final java.awt.Graphics2D G2 = IMAGE.createGraphics();

        G2.setRenderingHint(java.awt.RenderingHints.KEY_ANTIALIASING, java.awt.RenderingHints.VALUE_ANTIALIAS_ON);

        final int IMAGE_WIDTH = IMAGE.getWidth();
        final int IMAGE_HEIGHT = IMAGE.getHeight();

        // Define led data
        final java.awt.geom.Ellipse2D LED = new java.awt.geom.Ellipse2D.Double(0.25 * IMAGE_WIDTH, 0.25 * IMAGE_HEIGHT, 0.5 * IMAGE_WIDTH, 0.5 * IMAGE_HEIGHT);
        final java.awt.geom.Ellipse2D LED_CORONA = new java.awt.geom.Ellipse2D.Double(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);

        final java.awt.geom.Point2D LED_CENTER = new java.awt.geom.Point2D.Double(LED.getCenterX(), LED.getCenterY());

        final float[] LED_FRACTIONS =
        {
            0.0f,
            0.2f,
            1.0f
        };

        final java.awt.Color[] LED_OFF_COLORS;
        final java.awt.Color[] LED_ON_COLORS;
        final java.awt.Color[] LED_ON_CORONA_COLORS;
        
        if (LED_COLOR != eu.hansolo.steelseries.tools.LedColor.CUSTOM)
        {
            LED_OFF_COLORS = new java.awt.Color[]
            {
                LED_COLOR.INNER_COLOR1_OFF,
                LED_COLOR.INNER_COLOR2_OFF,
                LED_COLOR.OUTER_COLOR_OFF
            };

            LED_ON_COLORS = new java.awt.Color[]
            {
                LED_COLOR.INNER_COLOR1_ON,
                LED_COLOR.INNER_COLOR2_ON,
                LED_COLOR.OUTER_COLOR_ON
            };

            LED_ON_CORONA_COLORS = new java.awt.Color[]
            {
                UTIL.setAlpha(LED_COLOR.CORONA_COLOR, 0.0f),
                UTIL.setAlpha(LED_COLOR.CORONA_COLOR, 0.4f),
                UTIL.setAlpha(LED_COLOR.CORONA_COLOR, 0.25f),
                UTIL.setAlpha(LED_COLOR.CORONA_COLOR, 0.15f),
                UTIL.setAlpha(LED_COLOR.CORONA_COLOR, 0.05f),
                UTIL.setAlpha(LED_COLOR.CORONA_COLOR, 0.0f)
            };
        }
        else
        {
            LED_OFF_COLORS = new java.awt.Color[]
            {
                customLedColor.INNER_COLOR1_OFF,
                customLedColor.INNER_COLOR2_OFF,
                customLedColor.OUTER_COLOR_OFF
            };

            LED_ON_COLORS = new java.awt.Color[]
            {
                customLedColor.INNER_COLOR1_ON,
                customLedColor.INNER_COLOR2_ON,
                customLedColor.OUTER_COLOR_ON
            };

            LED_ON_CORONA_COLORS = new java.awt.Color[]
            {
                UTIL.setAlpha(customLedColor.CORONA_COLOR, 0.0f),
                UTIL.setAlpha(customLedColor.CORONA_COLOR, 0.4f),
                UTIL.setAlpha(customLedColor.CORONA_COLOR, 0.25f),
                UTIL.setAlpha(customLedColor.CORONA_COLOR, 0.15f),
                UTIL.setAlpha(customLedColor.CORONA_COLOR, 0.05f),
                UTIL.setAlpha(customLedColor.CORONA_COLOR, 0.0f)
            };
        }
                       
        final float[] LED_INNER_SHADOW_FRACTIONS =
        {
            0.0f,
            0.8f,
            1.0f
        };

        final java.awt.Color[] LED_INNER_SHADOW_COLORS =
        {
            new java.awt.Color(0.0f, 0.0f, 0.0f, 0.0f),
            new java.awt.Color(0.0f, 0.0f, 0.0f, 0.0f),
            new java.awt.Color(0.0f, 0.0f, 0.0f, 0.4f),
        };

        final float[] LED_ON_CORONA_FRACTIONS =
        {
            0.0f,
            0.6f,
            0.7f,
            0.8f,
            0.85f,
            1.0f
        };
        
        // Define gradients for the lower led
        final java.awt.RadialGradientPaint LED_OFF_GRADIENT = new java.awt.RadialGradientPaint(LED_CENTER, 0.25f * IMAGE_WIDTH, LED_FRACTIONS, LED_OFF_COLORS);
        final java.awt.RadialGradientPaint LED_ON_GRADIENT = new java.awt.RadialGradientPaint(LED_CENTER, 0.25f * IMAGE_WIDTH, LED_FRACTIONS, LED_ON_COLORS);
        final java.awt.RadialGradientPaint LED_INNER_SHADOW_GRADIENT = new java.awt.RadialGradientPaint(LED_CENTER, 0.25f * IMAGE_WIDTH, LED_INNER_SHADOW_FRACTIONS, LED_INNER_SHADOW_COLORS);
        final java.awt.RadialGradientPaint LED_ON_CORONA_GRADIENT = new java.awt.RadialGradientPaint(LED_CENTER, 0.5f * IMAGE_WIDTH, LED_ON_CORONA_FRACTIONS, LED_ON_CORONA_COLORS);


        // Define light reflex data
        final java.awt.geom.Ellipse2D LED_LIGHTREFLEX = new java.awt.geom.Ellipse2D.Double(0.4 * IMAGE_WIDTH, 0.35 * IMAGE_WIDTH, 0.2 * IMAGE_WIDTH, 0.15 * IMAGE_WIDTH);
        final java.awt.geom.Point2D LED_LIGHTREFLEX_START = new java.awt.geom.Point2D.Double(0, LED_LIGHTREFLEX.getMinY());
        final java.awt.geom.Point2D LED_LIGHTREFLEX_STOP = new java.awt.geom.Point2D.Double(0, LED_LIGHTREFLEX.getMaxY());

        final float[] LIGHT_REFLEX_FRACTIONS =
        {
            0.0f,
            1.0f
        };

        final java.awt.Color[] LIGHTREFLEX_COLORS =
        {
            new java.awt.Color(1.0f, 1.0f, 1.0f, 0.4f),
            new java.awt.Color(1.0f, 1.0f, 1.0f, 0.0f)
        };

        // Define light reflex gradients
        final java.awt.LinearGradientPaint LED_LIGHTREFLEX_GRADIENT = new java.awt.LinearGradientPaint(LED_LIGHTREFLEX_START, LED_LIGHTREFLEX_STOP, LIGHT_REFLEX_FRACTIONS, LIGHTREFLEX_COLORS);


        switch (STATE)
        {
            case 0:
                // LED OFF
                G2.setPaint(LED_OFF_GRADIENT);
                G2.fill(LED);
                G2.setPaint(LED_INNER_SHADOW_GRADIENT);
                G2.fill(LED);
                G2.setPaint(LED_LIGHTREFLEX_GRADIENT);
                G2.fill(LED_LIGHTREFLEX);
                break;
            case 1:
                // LED ON
                G2.setPaint(LED_ON_CORONA_GRADIENT);
                G2.fill(LED_CORONA);
                G2.setPaint(LED_ON_GRADIENT);
                G2.fill(LED);
                G2.setPaint(LED_INNER_SHADOW_GRADIENT);
                G2.fill(LED);
                G2.setPaint(LED_LIGHTREFLEX_GRADIENT);
                G2.fill(LED_LIGHTREFLEX);
                break;
            default:
                // LED OFF
                G2.setPaint(LED_OFF_GRADIENT);
                G2.fill(LED);
                G2.setPaint(LED_INNER_SHADOW_GRADIENT);
                G2.fill(LED);
                G2.setPaint(LED_LIGHTREFLEX_GRADIENT);
                G2.fill(LED_LIGHTREFLEX);
                break;
        }

        G2.dispose();

        return IMAGE;
    }
    // 
    
    // 
    /**
     * Calculates the rectangle that specifies the area that is available
     * for painting the gauge. This means that if the component has insets
     * that are larger than 0, these will be taken into account.
     */
    private void calcInnerBounds()
    {
        final java.awt.Insets INSETS = getInsets();        
        if (getWidth() - INSETS.left - INSETS.right < getHeight() - INSETS.top - INSETS.bottom)
        {                        
            INNER_BOUNDS.setBounds(INSETS.left, INSETS.top, getWidth() - INSETS.left - INSETS.right, getHeight() - INSETS.top - INSETS.bottom);                        
        }
        else
        {
            INNER_BOUNDS.setBounds(INSETS.left + (int) (((double) (getWidth() - INSETS.left - INSETS.right) - (double) (getHeight() - INSETS.top - INSETS.bottom)) / 2.0), INSETS.top, getHeight() - INSETS.top - INSETS.bottom, getHeight() - INSETS.top - INSETS.bottom);
        }        
    }
    
    @Override
    public java.awt.Dimension getMinimumSize()
    {
        return new java.awt.Dimension(16, 16);
    }

    @Override
    public void setPreferredSize(final java.awt.Dimension DIM)
    {        
        super.setPreferredSize(DIM);
        calcInnerBounds();
        init(DIM.width);        
        initialized = true;
        revalidate();
        repaint();
    }
    
    @Override
    public void setSize(final int WIDTH, final int HEIGHT)
    {
        super.setSize(WIDTH, WIDTH);
        calcInnerBounds();
        init(WIDTH);        
        initialized = true;
        revalidate();
        repaint();
    }
    
    @Override
    public void setSize(final java.awt.Dimension DIM)
    {
        super.setPreferredSize(DIM);
        calcInnerBounds();
        init(DIM.width);        
        initialized = true;
        revalidate();
        repaint();
    }
    
     @Override
    public void setBounds(final java.awt.Rectangle BOUNDS)
    {
        super.setBounds(new java.awt.Rectangle(BOUNDS.x, BOUNDS.y, BOUNDS.width, BOUNDS.width));
        calcInnerBounds();
        init(BOUNDS.width);        
        initialized = true;
        revalidate();
        repaint();
    }
    
    @Override
    public void setBounds(final int X, final int Y, final int WIDTH, final int HEIGHT)
    {
        super.setBounds(X, Y, WIDTH, WIDTH);
        calcInnerBounds();
        init(WIDTH);        
        initialized = true;
        revalidate();
        repaint();
    }
    // 
    
    // 
    @Override
    public void actionPerformed(java.awt.event.ActionEvent event)
    {
        if (event.getSource().equals(LED_BLINKING_TIMER))
        {
            currentLedImage = ledOn == true ? getLedImageOn() : getLedImageOff();
            ledOn ^= true;

            repaint();
        }
    }
    // 
    
    // 
    @Override
    public void componentResized(java.awt.event.ComponentEvent event)
    {
        

        init(getWidth());
        
        repaint(INNER_BOUNDS);
        
        //****************//
        final int SIZE = getWidth() <= getHeight() ? getWidth() : getHeight();                        
        if (getParent().getLayout() == null)
        {
            if (SIZE < getMinimumSize().width || SIZE < getMinimumSize().height)
            {
                setSize(getMinimumSize().width, getMinimumSize().height);
            }
            else
            {
                setSize(SIZE, SIZE);
            }
        }
        else
        {
            if (SIZE < getMinimumSize().width || SIZE < getMinimumSize().height)
            {            
                setPreferredSize(getMinimumSize());            
            }
            else
            {
                setPreferredSize(new java.awt.Dimension(SIZE, SIZE));
            }
        }
                
        calcInnerBounds();
                   
        init(INNER_BOUNDS.width);
        revalidate();
        repaint();
        
    }

    @Override
    public void componentMoved(java.awt.event.ComponentEvent event)
    {

    }

    @Override
    public void componentShown(java.awt.event.ComponentEvent event)
    {

    }

    @Override
    public void componentHidden(java.awt.event.ComponentEvent event)
    {

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

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy