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:net.sourceforge.msscodefactory.cflib.v1_11.CFLib.Swing.CFInt64ColumnCellRenderer.java

public void paint(Graphics g) {
    if (g == null) {
        return;/*ww w.  ja  v  a2 s. 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 Long) {
        Long val = (Long) value;
        Format fmt = getDefaultFormat();
        str = fmt.format(val);
    } 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();
        int width = fm.stringWidth(firstLine);
        int x = (bounds.width - 8) - width;
        g.drawString(firstLine, x, leading + ascent + 4);
    }
}

From source file:Main.java

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    FontMetrics fm = g.getFontMetrics(font);
    wordWidth = fm.stringWidth(word);//w  w w.  j  a v  a  2s  . c o  m
    wordHeight = fm.getAscent();

    g.setFont(new Font("impact", Font.PLAIN, 28));
    g.setColor(Color.BLUE);
    g.drawString(word, x, y);
}

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

public void paint(Graphics g) {
    if (g == null) {
        return;/*www.  ja  va 2 s  .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 Double) {
        Double val = (Double) value;
        Format fmt = getDefaultFormat();
        str = fmt.format(val);
    } 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();
        int width = fm.stringWidth(firstLine);
        int x = (bounds.width - 8) - width;
        g.drawString(firstLine, x, leading + ascent + 4);
    }
}

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

public void paint(Graphics g) {
    if (g == null) {
        return;//from  w  ww  .  j  a v  a  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 Float) {
        Float val = (Float) value;
        Format fmt = getDefaultFormat();
        str = fmt.format(val);
    } 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();
        int width = fm.stringWidth(firstLine);
        int x = (bounds.width - 8) - width;
        g.drawString(firstLine, x, leading + ascent + 4);
    }
}

From source file:CopyAreaPerformance.java

private void drawSmiley(Graphics g, Color faceColor, int x, int y) {
    // fill face color
    g.setColor(faceColor);
    g.fillOval(x, y, SMILEY_SIZE, SMILEY_SIZE);
    g.setColor(Color.BLACK);/*from  ww  w. j  a v  a2  s  .  c  o m*/
    // draw head
    g.drawOval(x, y, SMILEY_SIZE, SMILEY_SIZE);
    // draw smile
    g.drawArc(x + (int) ((SMILEY_SIZE * .2)), (int) (y + (SMILEY_SIZE * .2)), (int) (SMILEY_SIZE * .6),
            (int) (SMILEY_SIZE * .6), 200, 140);
    // draw eyes
    int eyeSize = Math.max(2, (int) (SMILEY_SIZE * .1));
    g.fillOval(x + (int) ((SMILEY_SIZE * .5) - (SMILEY_SIZE * .1) - eyeSize), y + (int) (SMILEY_SIZE * .3),
            eyeSize, eyeSize);
    g.fillOval(x + (int) ((SMILEY_SIZE * .5) + (SMILEY_SIZE * .1)), y + (int) (SMILEY_SIZE * .3), eyeSize,
            eyeSize);
}

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

public void paint(Graphics g) {
    if (g == null) {
        return;/*from w  w  w .ja  va 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 BigDecimal) {
        BigDecimal val = (BigDecimal) value;
        Format fmt = getDefaultFormat();
        str = fmt.format(val);
    } 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();
        int width = fm.stringWidth(firstLine);
        int x = (bounds.width - 8) - width;
        g.drawString(firstLine, x, leading + ascent + 4);
    }
}

From source file:Main.java

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Dimension size = this.getSize();
    int d = Math.min(size.width, size.height) - 10;
    int x = (size.width - d) / 2;
    int y = (size.height - d) / 2;
    g.fillOval(x, y, d, d);//  w  w  w.j a  v  a 2s . c o m
    g.setColor(Color.blue);
}

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

public void paint(Graphics g) {
    if (g == null) {
        return;/*  w w w  .j av  a  2  s.  c  o  m*/
    }
    Rectangle bounds = getBounds();
    g.setColor(getBackground());
    g.fill3DRect(0, 0, bounds.width, bounds.height, true);
    g.setColor(getForeground());
    int xoffbox = (bounds.width - 16) / 2;
    int yoffbox = (bounds.height - 16) / 2;
    g.drawRect(xoffbox, yoffbox, 16, 16);
    Boolean useval;
    if (value instanceof Boolean) {
        useval = (Boolean) value;
    } else if (value instanceof String) {
        String s = (String) value;
        if (s.equals("true")) {
            useval = new Boolean(true);
        } else if (s.equals("false")) {
            useval = new Boolean(false);
        } else {
            useval = null;
        }
    } else {
        useval = null;
    }
    String str;
    if (useval == null) {
        str = "?";
    } else if (useval.booleanValue()) {
        str = "X";
    } else {
        str = null;
    }
    if (str != null) {
        FontMetrics fm = g.getFontMetrics();
        Rectangle sb = fm.getStringBounds(str, g).getBounds();
        int ascent = fm.getAscent();
        int leading = fm.getLeading();
        int sxoff = xoffbox + ((16 - sb.width) / 2);
        int syoff = yoffbox + ((16 - (leading + ascent)) / 2) + leading + ascent;
        g.drawString(str, sxoff, syoff);
    }
}

From source file:com.krawler.esp.handlers.genericFileUpload.java

private BufferedImage toBufferedImage(Image image) {
    image = new ImageIcon(image).getImage();
    BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null),
            BufferedImage.TYPE_INT_RGB);
    Graphics g = bufferedImage.createGraphics();
    g.setColor(Color.white);
    g.fillRect(0, 0, image.getWidth(null), image.getHeight(null));
    g.drawImage(image, 0, 0, null);//from w ww. j a  v  a2 s  . c om
    g.dispose();

    return bufferedImage;
}

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

public void paint(Graphics g) {
    if (g == null) {
        return;//from   ww  w .j  a v  a 2 s. 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 Calendar) {
        Calendar cal = (Calendar) value;
        Format fmt = getDefaultFormat();
        Calendar withoutDate = Calendar.getInstance();
        withoutDate.clear();
        withoutDate.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY));
        withoutDate.set(Calendar.MINUTE, cal.get(Calendar.MINUTE));
        withoutDate.set(Calendar.SECOND, cal.get(Calendar.SECOND));
        str = fmt.format(withoutDate.getTime());
    } 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);
    }
}