Example usage for java.awt Graphics setColor

List of usage examples for java.awt Graphics setColor

Introduction

In this page you can find the example usage for java.awt Graphics setColor.

Prototype

public abstract void setColor(Color c);

Source Link

Document

Sets this graphics context's current color to the specified color.

Usage

From source file:IconDisplayer.java

protected void paintComponent(Graphics g) {
    if (isOpaque()) { //paint background
        g.setColor(getBackground());
        g.fillRect(0, 0, getWidth(), getHeight());
    }//from ww w.  jav  a  2  s. c  om

    if (icon != null) {
        //Draw the icon over and over, right aligned.
        Insets insets = getInsets();
        int iconWidth = icon.getIconWidth();
        int iconX = getWidth() - insets.right - iconWidth;
        int iconY = insets.top;
        boolean faded = false;

        //Copy the Graphics object, which is actually
        //a Graphics2D object. Cast it so we can
        //set alpha composite.
        Graphics2D g2d = (Graphics2D) g.create();

        //Draw the icons, starting from the right.
        //After the first one, the rest are faded.
        //We won't bother painting icons that are clipped.
        g.getClipBounds(clipRect);
        while (iconX >= insets.left) {
            iconRect.setBounds(iconX, iconY, iconWidth, icon.getIconHeight());
            if (iconRect.intersects(clipRect)) {
                icon.paintIcon(this, g2d, iconX, iconY);
            }
            iconX -= (iconWidth + pad);
            if (!faded) {
                g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.1f));
                faded = true;
            }
        }

        g2d.dispose(); //clean up
    }
}

From source file:net.sourceforge.msscodefactory.cflib.v1_11.CFLib.Swing.CFHeaderColumnCellRenderer.java

public void paint(Graphics g) {
    if (g == null) {
        return;//from w w w . j a  v  a2s.  c o  m
    }
    Rectangle bounds = getBounds();
    g.setColor(getBackground());
    g.fill3DRect(0, 0, bounds.width, bounds.height, true);
    g.setColor(getForeground());
    String str;
    if (value instanceof String) {
        str = (String) value;
    } else {
        str = null;
    }
    if (str != null) {
        int firstNewline = str.indexOf('\n');
        if (firstNewline < 0) {
            firstNewline = str.indexOf('\r');
            if (firstNewline < 0) {
                firstNewline = str.indexOf('\f');
                if (firstNewline < 0) {
                    firstNewline = str.length();
                }
            }
        }
        String firstLine = str.substring(0, firstNewline);
        FontMetrics fm = g.getFontMetrics();
        int width = fm.stringWidth(firstLine);
        int x = (bounds.width - width) / 2;
        int ascent = fm.getAscent();
        int leading = fm.getLeading();
        g.drawString(firstLine, x, leading + ascent + 4);
    }
}

From source file:org.accada.hal.impl.sim.graphic.Antenna.java

/**
 * paints the rfid antenna//from ww  w .  j  a v  a 2  s . c o  m
 * 
 * @param g the graphic representation of the component
 */
protected void paintComponent(Graphics g) {
    icon.paintIcon(this, g, 0, 0);
    g.setColor(Color.BLACK);
    g.setFont(new Font(simulator.getProperties().getString("AntennaLabelFont"), 0,
            simulator.getProperty("AntennaLabelSize")));
    g.drawString(id, (15 - id.length()) * 7 / 2, simulator.getProperty("AntennaHeight"));
}

From source file:CustomIconDemo.java

public void paintIcon(Component c, Graphics g, int x, int y) {
    if (c.isEnabled()) {
        g.setColor(c.getForeground());
    } else {/* ww  w.  ja v  a2  s. c  o  m*/
        g.setColor(Color.gray);
    }

    g.translate(x, y);
    g.fillPolygon(xPoints, yPoints, xPoints.length);
    g.translate(-x, -y); // Restore graphics object
}

From source file:net.sourceforge.msscodefactory.cflib.v1_11.CFLib.Swing.CFNmTokenColumnCellRenderer.java

public void paint(Graphics g) {
    if (g == null) {
        return;/*from  w  w  w .j a v  a 2s .  co m*/
    }
    Rectangle bounds = getBounds();
    g.setColor(getBackground());
    g.fill3DRect(0, 0, bounds.width, bounds.height, true);
    g.setColor(getForeground());
    String str;
    if (value instanceof String) {
        str = (String) value;
    } else {
        str = null;
    }
    if (str != null) {
        int firstNewline = str.indexOf('\n');
        if (firstNewline < 0) {
            firstNewline = str.indexOf('\r');
            if (firstNewline < 0) {
                firstNewline = str.indexOf('\f');
                if (firstNewline < 0) {
                    firstNewline = str.length();
                }
            }
        }
        String firstLine = str.substring(0, firstNewline);
        FontMetrics fm = g.getFontMetrics();
        int ascent = fm.getAscent();
        int leading = fm.getLeading();
        g.drawString(firstLine, 4, leading + ascent + 4);
    }
}

From source file:org.accada.hal.impl.sim.graphic.Reader.java

/**
 * paints the reader//from  w  w w. j  av a2s .c  o  m
 * 
 * @param g the graphic representation of the component
 */
protected void paintComponent(Graphics g) {
    icon.paintIcon(this, g, 0, 0);
    g.setColor(Color.BLACK);
    g.setFont(new Font(simulator.getProperties().getString("ReaderLabelFont"), 0,
            simulator.getProperty("ReaderLabelSize")));
    g.drawString(id, (18 - id.length()) * 9 / 2, simulator.getProperty("ReaderHeight"));
}

From source file:CheckBoxIcon.java

public void paintIcon(Component component, Graphics g, int x, int y) {
    AbstractButton abstractButton = (AbstractButton) component;
    ButtonModel buttonModel = abstractButton.getModel();

    Color color = buttonModel.isSelected() ? Color.BLUE : Color.RED;
    g.setColor(color);

    g.drawRect(1, 1, 20, 20);//from   w w w. jav  a  2s  .  com

}

From source file:net.sourceforge.msscodefactory.cflib.v2_1.CFLib.Swing.CFReferenceColumnCellRenderer.java

public void paint(Graphics g) {
    if (g == null) {
        return;// ww w  .  j a  va  2  s . c  om
    }
    Rectangle bounds = getBounds();
    g.setColor(getBackground());
    g.fill3DRect(0, 0, bounds.width, bounds.height, true);
    g.setColor(getForeground());
    String str;
    if (value instanceof ICFLibAnyObj) {
        ICFLibAnyObj anyObj = (ICFLibAnyObj) value;
        str = anyObj.getObjQualifiedName();
    } else if (value instanceof String) {
        str = (String) value;
    } else {
        str = null;
    }
    if (str != null) {
        int firstNewline = str.indexOf('\n');
        if (firstNewline < 0) {
            firstNewline = str.indexOf('\r');
            if (firstNewline < 0) {
                firstNewline = str.indexOf('\f');
                if (firstNewline < 0) {
                    firstNewline = str.length();
                }
            }
        }
        String firstLine = str.substring(0, firstNewline);
        FontMetrics fm = g.getFontMetrics();
        int ascent = fm.getAscent();
        int leading = fm.getLeading();
        g.drawString(firstLine, 4, leading + ascent + 4);
    }
}

From source file:net.sourceforge.msscodefactory.cflib.v1_11.CFLib.Swing.CFReferenceColumnCellRenderer.java

public void paint(Graphics g) {
    if (g == null) {
        return;/*from   w ww  . j ava  2s.  c  o  m*/
    }
    Rectangle bounds = getBounds();
    g.setColor(getBackground());
    g.fill3DRect(0, 0, bounds.width, bounds.height, true);
    g.setColor(getForeground());
    String str;
    if (value instanceof ICFLibAnyObj2) {
        ICFLibAnyObj2 anyObj = (ICFLibAnyObj2) value;
        str = anyObj.getObjQualifiedName();
    } else if (value instanceof String) {
        str = (String) value;
    } else {
        str = null;
    }
    if (str != null) {
        int firstNewline = str.indexOf('\n');
        if (firstNewline < 0) {
            firstNewline = str.indexOf('\r');
            if (firstNewline < 0) {
                firstNewline = str.indexOf('\f');
                if (firstNewline < 0) {
                    firstNewline = str.length();
                }
            }
        }
        String firstLine = str.substring(0, firstNewline);
        FontMetrics fm = g.getFontMetrics();
        int ascent = fm.getAscent();
        int leading = fm.getLeading();
        g.drawString(firstLine, 4, leading + ascent + 4);
    }
}

From source file:net.sourceforge.msscodefactory.cflib.v1_11.CFLib.Swing.CFTZDateColumnCellRenderer.java

public void paint(Graphics g) {
    if (g == null) {
        return;/*from www  .  j  a va2s  .c  om*/
    }
    Rectangle bounds = getBounds();
    g.setColor(getBackground());
    g.fill3DRect(0, 0, bounds.width, bounds.height, true);
    g.setColor(getForeground());
    String str;
    if (value instanceof Calendar) {
        Calendar cal = (Calendar) value;
        str = CFLibXmlUtil.formatTZDate(cal);
    } else if (value instanceof String) {
        str = (String) value;
    } else {
        str = null;
    }
    if (str != null) {
        int firstNewline = str.indexOf('\n');
        if (firstNewline < 0) {
            firstNewline = str.indexOf('\r');
            if (firstNewline < 0) {
                firstNewline = str.indexOf('\f');
                if (firstNewline < 0) {
                    firstNewline = str.length();
                }
            }
        }
        String firstLine = str.substring(0, firstNewline);
        FontMetrics fm = g.getFontMetrics();
        int ascent = fm.getAscent();
        int leading = fm.getLeading();
        g.drawString(firstLine, 4, leading + ascent + 4);
    }
}