Example usage for java.awt Graphics2D fillRect

List of usage examples for java.awt Graphics2D fillRect

Introduction

In this page you can find the example usage for java.awt Graphics2D fillRect.

Prototype

public abstract void fillRect(int x, int y, int width, int height);

Source Link

Document

Fills the specified rectangle.

Usage

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

/**
 * ???,,?16,//from  ww w  .  j a v a  2  s  .co m
 * @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  ww . j a va  2s  .  co 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:irille.pub.verify.RandomImageServlet.java

/**
 * ???,,?16,/* w w w.  j a va2s.  c o  m*/
 * @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: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);//from w  ww  .ja  v a2 s  .c  o  m
    g.fillRect(0, 0, width, height);
    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:SplashDemo.java

static void renderSplashFrame(Graphics2D g, int frame) {
    final String[] comps = { "foo", "bar", "baz" };
    g.setComposite(AlphaComposite.Clear);
    g.fillRect(120, 140, 200, 40);
    g.setPaintMode();/*from   ww  w  .ja  v  a 2s.  c o  m*/
    g.setColor(Color.BLACK);
    g.drawString("Loading " + comps[(frame / 5) % 3] + "...", 120, 150);
}

From source file:org.jfree.graphics2d.demo.ImageTest.java

private static void drawClipTest(Graphics2D g2) {
    g2.translate(10, 20);/*w  w w  . j a v a 2 s  .c o  m*/
    g2.setColor(Color.RED);
    g2.fillRect(10, 10, 100, 100);
    g2.clip(new Rectangle(0, 0, 60, 60));
    g2.setPaint(Color.BLUE);
    g2.fillRect(10, 10, 100, 100);
    g2.setClip(null);
    g2.setPaint(Color.GREEN);
    g2.fillRect(60, 60, 50, 50);
}

From source file:QRCode.java

public static String createQRCode(String arg) {
    int size = 125;
    String fileType = "png";
    File myFile = null;//from   w ww .  j av  a2s.  c o  m
    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;
}

From source file:org.polymap.styler.helper.ImageHelper.java

public static BufferedImage createColorRectBufferedImage(java.awt.Color color) {
    BufferedImage image = new BufferedImage(10, 10, BufferedImage.TYPE_INT_ARGB);
    Graphics2D gr2d = image.createGraphics();
    // draw the image
    gr2d.setColor(color);/*from  www. ja  v  a 2 s  . com*/
    gr2d.fillRect(0, 0, 10, 10);

    return image;
}

From source file:Main.java

/**
 * Fills the given {@link BufferedImage} with a {@link Paint}, preserving its alpha channel.
 *
 * @param source The source image.//  w w  w.  jav  a 2 s.c om
 * @param paint  The paint to fill with.
 * @return A new, painted/filled image.
 */
public static BufferedImage filledImage(BufferedImage source, Paint paint) {
    BufferedImage newImage = newArgbBufferedImage(source.getWidth(), source.getHeight());
    Graphics2D g = (Graphics2D) newImage.getGraphics();
    g.drawImage(source, 0, 0, null);
    g.setComposite(AlphaComposite.SrcAtop);
    g.setPaint(paint);
    g.fillRect(0, 0, source.getWidth(), source.getHeight());
    return newImage;
}

From source file:org.jfree.graphics2d.demo.ImageTest.java

private static void drawGradientPaintTest(Graphics2D g2) {
    g2.setPaint(new GradientPaint(10f, 10f, Color.RED, 10f, 60f, Color.YELLOW));
    g2.fillRect(10, 10, 50, 50);
    g2.setPaint(Color.BLUE);/*from   ww  w .ja  v  a  2  s.  c  om*/
    g2.fillRect(60, 10, 50, 50);
}