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:FontAlgo.java

private static TextualChar getTextualChar(char a_char) throws Throwable {
    BufferedImage bImg = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
    Graphics g = bImg.getGraphics();
    g.setColor(Color.green);
    g.fillRect(0, 0, WIDTH, HEIGHT);/*from   www  . j  av a2 s . c o m*/

    g.setFont(appliedFont);
    g.setColor(Color.black);
    g.drawString(new String(new char[] { a_char }), 10, g.getFontMetrics().getHeight());
    PixelGrabber p = new PixelGrabber(bImg, 0, 0, WIDTH, HEIGHT, true);

    if (p.grabPixels()) {
        char[][] pattern = new char[WIDTH][HEIGHT];
        int baseColourPixel = 0, contrastColourPixel = 0, x1 = 0, x2 = 0, y1 = 0, y2 = 0;
        int[] pixels = (int[]) p.getPixels();
        baseColourPixel = pixels[0];
        // System.out.println("base: " + base);
        int xCounter = 0, yCounter = 0;
        for (int iPixel : pixels) {
            // System.out.println(iX + " - " + iY);
            if (isReverse) {
                pattern[xCounter][yCounter] = iPixel == baseColourPixel ? CHAR_TO_PATTERN : ' ';
            } else {
                pattern[xCounter][yCounter] = iPixel != baseColourPixel ? CHAR_TO_PATTERN : ' ';
            }

            yCounter++;
            if (yCounter > 49) {
                xCounter++;
                yCounter = 0;
            }

            if (contrastColourPixel == 0 && iPixel != baseColourPixel) {
                contrastColourPixel = iPixel;
                x1 = xCounter - 2;
                y1 = yCounter - 3;
                y2 = yCounter + 3;
            }

            if (contrastColourPixel == iPixel) {
                x2 = xCounter + 3;

                if (y1 > (yCounter - 3)) {
                    y1 = yCounter - 3;
                }

                if (y2 < (yCounter + 3)) {
                    y2 = yCounter + 3;
                }
            }
        }
        return new TextualChar(x1, x2, y1, y2, pattern);
    }
    return null;
}

From source file:Main.java

public static void drawGroove(Graphics g, int x, int y, int w, int h, Color shadow, Color highlight) {
    Color oldColor = g.getColor(); // Make no net change to g
    g.translate(x, y);// w ww.  j av a2  s .co m

    g.setColor(shadow);
    g.drawRect(0, 0, w - 2, h - 2);

    g.setColor(highlight);
    g.drawLine(1, h - 3, 1, 1);
    g.drawLine(1, 1, w - 3, 1);

    g.drawLine(0, h - 1, w - 1, h - 1);
    g.drawLine(w - 1, h - 1, w - 1, 0);

    g.translate(-x, -y);
    g.setColor(oldColor);
}

From source file:com.spstudio.common.image.ImageUtils.java

public static BufferedImage zoom(BufferedImage sourceImage, int width, int height) {
    BufferedImage zoomImage = new BufferedImage(width, height, sourceImage.getType());
    Image image = sourceImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
    Graphics gc = zoomImage.getGraphics();
    gc.setColor(Color.WHITE);
    gc.drawImage(image, 0, 0, null);//  w w  w  .java  2s.  com
    return zoomImage;
}

From source file:PaintUtils.java

private static void drawColors(Color[] colors, Graphics g, int x1, int y1, int x2, int y2, int direction) {
    for (int a = 0; a < colors.length; a++) {
        g.setColor(colors[colors.length - a - 1]);
        if (direction == SwingConstants.SOUTH) {
            g.drawLine(x1, y1 - a, x2, y2 - a);
        } else if (direction == SwingConstants.NORTH) {
            g.drawLine(x1, y1 + a, x2, y2 + a);
        } else if (direction == SwingConstants.EAST) {
            g.drawLine(x1 - a, y1, x2 - a, y2);
        } else if (direction == SwingConstants.WEST) {
            g.drawLine(x1 + a, y1, x2 + a, y2);
        }//from  w  w w  . j  av  a2s  . c  o  m
    }
}

From source file:at.gv.egiz.pdfas.common.utils.ImageUtils.java

public static BufferedImage convertRGBAToIndexed(BufferedImage src) {
    BufferedImage dest = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_INDEXED);
    Graphics g = dest.getGraphics();
    g.setColor(new Color(231, 20, 189));
    g.fillRect(0, 0, dest.getWidth(), dest.getHeight()); // fill with a
    // hideous color
    // and make it
    // transparent
    dest = makeTransparent(dest, 0, 0);/*from  ww  w  .j a  va 2 s.com*/
    dest.createGraphics().drawImage(src, 0, 0, null);
    return dest;
}

From source file:com.baidu.rigel.biplatform.ma.auth.resource.RandomValidateCode.java

private static String drowString(Graphics g, String randomString, int i) {
    g.setFont(getFont());/*  w w w.  j ava  2  s. com*/
    g.setColor(new Color(random.nextInt(101), random.nextInt(111), random.nextInt(121)));
    String rand = String.valueOf(getRandomString(random.nextInt(randString.length())));
    randomString += rand;
    g.translate(random.nextInt(3), random.nextInt(3));
    g.drawString(rand, 13 * i, 16);
    return randomString;
}

From source file:org.b3log.symphony.model.Character.java

/**
 * Creates an image with the specified content (a character).
 *
 * @param content the specified content//from   w  ww  . j a  v  a2 s. c  o m
 * @return image
 */
public static BufferedImage createImage(final String content) {
    final BufferedImage ret = new BufferedImage(500, 500, Transparency.TRANSLUCENT);
    final Graphics g = ret.getGraphics();
    g.setClip(0, 0, 50, 50);
    g.fillRect(0, 0, 50, 50);
    g.setFont(new Font(null, Font.PLAIN, 40));
    g.setColor(Color.BLACK);
    g.drawString(content, 5, 40);
    g.dispose();

    return ret;
}

From source file:Main.java

public static void fillInverseVerGradient(Graphics g, Color[] colors, int x, int y, int w, int h) {
    int steps = colors.length;
    double dx = (double) w / (double) steps;
    int x1 = x;//from www  .  j a v a 2s.c om
    for (int i = 0; i < steps; i++) {
        int x2 = x + (int) Math.round((double) i * dx);
        g.setColor(colors[colors.length - i - 1]);
        if (i == (steps - 1)) {
            g.fillRect(x1, y, x + w - x1, h);
        } else {
            g.fillRect(x1, y, x2 - x1, h);
        }
        x1 = x2;
    }
}

From source file:Main.java

public static void fillVerGradient(Graphics g, Color[] colors, int x, int y, int w, int h) {
    int steps = colors.length;
    double dx = (double) w / (double) steps;
    int x1 = x;//from   w  w  w. j a  va  2 s  .  c o m
    for (int i = 0; i < steps; i++) {
        int x2 = x + (int) Math.round((double) i * dx);
        g.setColor(colors[i]);
        if (i == (steps - 1)) {
            g.fillRect(x1, y, x + w - x1, h);
        } else {
            g.fillRect(x1, y, x2 - x1, h);
        }
        x1 = x2;
    }
}

From source file:components.FrameDemo2.java

protected static Image createFDImage() {
    //Create a 16x16 pixel image.
    BufferedImage bi = new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB);

    //Draw into it.
    Graphics g = bi.getGraphics();
    g.setColor(Color.BLACK);
    g.fillRect(0, 0, 15, 15);/*  www  .ja  v a  2  s .  c  om*/
    g.setColor(Color.RED);
    g.fillOval(5, 3, 6, 6);

    //Clean up.
    g.dispose();

    //Return it.
    return bi;
}