Example usage for java.awt Graphics2D setColor

List of usage examples for java.awt Graphics2D setColor

Introduction

In this page you can find the example usage for java.awt Graphics2D 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:Main.java

public static BufferedImage highlightRegions(Image img, int[][] regions, int regionId, Color fgColour) {
    BufferedImage canvas = new BufferedImage(img.getWidth(null), img.getHeight(null),
            BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = canvas.createGraphics();
    g2d.drawImage(img, 0, 0, null);/* ww  w  .jav  a 2s.  c  om*/
    g2d.setColor(fgColour);
    for (int y = 0; y < regions.length; y++) {
        for (int x = 0; x < regions[y].length; x++) {
            if (regions[y][x] == regionId) {
                g2d.drawRect(x, y, 1, 1);
            }
        }
    }

    return canvas;
}

From source file:Main.java

private static BufferedImage dye(BufferedImage image, Color color) {
    int w = image.getWidth();
    int h = image.getHeight();
    BufferedImage dyed = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = dyed.createGraphics();
    g.drawImage(image, 0, 0, null);//from   w  w  w  . j a  v  a2 s.com
    g.setComposite(AlphaComposite.SrcAtop);
    g.setColor(color);
    g.fillRect(0, 0, w, h);
    g.dispose();
    return dyed;
}

From source file:com.liusoft.dlog4j.servlet.DLOG_RandomImageServlet.java

/**
 * ???,,?16,//from  ww w.j av a  2  s .c om
 * @param num   ??
 * @param out   ?
 * @throws IOException
 */
protected static void render(String num, boolean gif, OutputStream out) throws IOException {
    if (num.getBytes().length > 4)
        throw new IllegalArgumentException("The length of param num cannot exceed 4.");
    int width = 40;
    int height = 15;
    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = (Graphics2D) bi.getGraphics();
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, width, height);
    Font mFont = new Font("Tahoma", Font.PLAIN, 14);
    g.setFont(mFont);
    g.setColor(Color.BLACK);
    g.drawString(num, 2, 13);
    if (gif) {
        AnimatedGifEncoder e = new AnimatedGifEncoder();
        e.setTransparent(Color.WHITE);
        e.start(out);
        e.setDelay(0);
        e.addFrame(bi);
        e.finish();
    } else {
        ImageIO.write(bi, "png", out);
    }
}

From source file:Main.java

/**
 * Clears the given area of the specified graphics object with the given
 * color or makes the region transparent.
 *///from w w  w .  j a v  a2s  .  c  o m
public static void clearRect(Graphics2D g, Rectangle rect, Color background) {
    if (background != null) {
        g.setColor(background);
        g.fillRect(rect.x, rect.y, rect.width, rect.height);
    } else {
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
        g.fillRect(rect.x, rect.y, rect.width, rect.height);
        g.setComposite(AlphaComposite.SrcOver);
    }
}

From source file:Main.java

public static BufferedImage drawBoundingBoxes(BufferedImage canvas, List<Rectangle> boxes, Color fgColour,
        Color bgColour) {/*from   w  w w  .j a  va  2 s.com*/
    Graphics2D g2d = canvas.createGraphics();
    for (Rectangle r : boxes) {
        g2d.setColor(bgColour);
        g2d.drawRect(r.x - 1, r.y - 1, r.width + 2, r.height + 2);
        g2d.drawRect(r.x + 1, r.y + 1, r.width - 2, r.height - 2);
        g2d.setColor(fgColour);
        g2d.drawRect(r.x, r.y, r.width, r.height);
    }

    return canvas;
}

From source file:Main.java

public static BufferedImage joinBufferedImage(BufferedImage img1, BufferedImage img2) {
    int offset = 2;
    int width = img1.getWidth() + img2.getWidth() + offset;
    int height = Math.max(img1.getHeight(), img2.getHeight()) + offset;
    BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = newImage.createGraphics();
    Color oldColor = g2.getColor();
    g2.setPaint(Color.BLACK);// w  w w.  ja v  a2  s . c  o  m
    g2.fillRect(0, 0, width, height);
    g2.setColor(oldColor);
    g2.drawImage(img1, null, 0, 0);
    g2.drawImage(img2, null, img1.getWidth() + offset, 0);
    g2.dispose();
    return newImage;
}

From source file:irille.pub.verify.RandomImageServlet.java

/**
 * ???,,?16,//from ww w .j av a 2s. com
 * @param num   ??
 * @param out   ?
 * @throws IOException
 */
protected static void render(String num, boolean gif, OutputStream out) throws IOException {
    if (num.getBytes().length > 4)
        throw new IllegalArgumentException("The length of param num cannot exceed 4.");
    int width = 50;
    int height = 18;
    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = (Graphics2D) bi.getGraphics();
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, width, height);
    Font mFont = new Font("Tahoma", Font.BOLD | Font.ITALIC, 16);
    g.setFont(mFont);
    g.setColor(Color.BLACK);
    g.drawString(num, 2, 15);
    if (gif) {
        AnimatedGifEncoder e = new AnimatedGifEncoder();
        e.setTransparent(Color.WHITE);
        e.start(out);
        e.setDelay(0);
        e.addFrame(bi);
        e.finish();
    } else {
        ImageIO.write(bi, "png", out);
    }
}

From source file:Main.java

public static BufferedImage drawBoundingBoxes(BufferedImage canvas, Rectangle[] boxes, Color fgColour,
        Color bgColour) {//from   w  w w.j a v a2  s .c o m
    Graphics2D g2d = canvas.createGraphics();
    for (int i = 0; i < boxes.length; i++) {
        g2d.setColor(bgColour);
        g2d.drawRect(boxes[i].x - 1, boxes[i].y - 1, boxes[i].width + 2, boxes[i].height + 2);
        g2d.drawRect(boxes[i].x + 1, boxes[i].y + 1, boxes[i].width - 2, boxes[i].height - 2);
        g2d.setColor(fgColour);
        g2d.drawRect(boxes[i].x, boxes[i].y, boxes[i].width, boxes[i].height);
    }

    return canvas;
}

From source file:net.noday.core.utils.Captcha.java

public static BufferedImage gen(String text, int width, int height) throws IOException {
    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
    Graphics2D g = (Graphics2D) bi.getGraphics();
    g.setColor(Color.GRAY);
    g.fillRect(0, 0, width, height);// ww  w  .ja v a  2  s.  c  o m
    for (int i = 0; i < 10; i++) {
        g.setColor(randColor(150, 250));
        g.drawOval(random.nextInt(110), random.nextInt(24), 5 + random.nextInt(10), 5 + random.nextInt(10));
        Font f = new Font("Arial", Font.ITALIC, 20);
        g.setFont(f);
        g.setColor(randColor(10, 240));
        g.drawString(text, 4, 24);
    }
    return bi;
}

From source file:QRCode.java

public static String createQRCode(String arg) {
    int size = 125;
    String fileType = "png";
    File myFile = null;//from  w  ww .j ava  2  s  .  com
    UUID uuid = UUID.nameUUIDFromBytes(arg.getBytes());

    try {
        myFile = File.createTempFile("temp-file-name", ".png");

        Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
        hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix byteMatrix = qrCodeWriter.encode(uuid.toString(), BarcodeFormat.QR_CODE, size, size, hintMap);
        int CrunchifyWidth = byteMatrix.getWidth();
        BufferedImage image = new BufferedImage(CrunchifyWidth, CrunchifyWidth, BufferedImage.TYPE_INT_RGB);
        image.createGraphics();

        Graphics2D graphics = (Graphics2D) image.getGraphics();
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, CrunchifyWidth, CrunchifyWidth);
        graphics.setColor(Color.BLACK);

        for (int i = 0; i < CrunchifyWidth; i++) {
            for (int j = 0; j < CrunchifyWidth; j++) {
                if (byteMatrix.get(i, j)) {
                    graphics.fillRect(i, j, 1, 1);
                }
            }
        }
        ImageIO.write(image, fileType, myFile);
        //System.out.println("\n\nYou have successfully created QR Code " + myFile.getCanonicalPath());
        return myFile.getCanonicalPath();
    } catch (WriterException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}