Example usage for java.awt Graphics fillRect

List of usage examples for java.awt Graphics fillRect

Introduction

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

private static ByteBuffer convertImageData(BufferedImage bufferedImage) {
    ByteBuffer imageBuffer;/*from   w  ww .j av  a 2 s. c o  m*/
    WritableRaster raster;
    BufferedImage texImage;

    // for a texture
    if (bufferedImage.getColorModel().hasAlpha()) {
        raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, bufferedImage.getWidth(),
                bufferedImage.getHeight(), 4, null);
        texImage = new BufferedImage(glAlphaColorModel, raster, false, new Hashtable<Object, Object>());
    } else {
        raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, bufferedImage.getWidth(),
                bufferedImage.getHeight(), 3, null);
        texImage = new BufferedImage(glColorModel, raster, false, new Hashtable<Object, Object>());
    }

    // copy the source image into the produced image
    Graphics g = texImage.getGraphics();
    g.setColor(new Color(0f, 0f, 0f, 0f));
    g.fillRect(0, 0, bufferedImage.getWidth(), bufferedImage.getHeight());
    g.drawImage(bufferedImage, 0, 0, null);

    // build a byte buffer from the temporary image
    // that be used by OpenGL to produce a texture.
    byte[] data = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer()).getData();

    imageBuffer = ByteBuffer.allocateDirect(data.length);
    imageBuffer.order(ByteOrder.nativeOrder());
    imageBuffer.put(data, 0, data.length);
    imageBuffer.flip();

    return imageBuffer;
}

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 av  a  2s  .co  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:WaterMark.java

public static String execute(String src, String dest, String text, Color color, Font font) throws Exception {
    BufferedImage srcImage = ImageIO.read(new File(src));

    int width = srcImage.getWidth(null);
    int height = srcImage.getHeight(null);
    BufferedImage destImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics g = destImage.getGraphics();

    g.drawImage(srcImage, 0, 0, width, height, null);
    g.setColor(color);/*  w  w w .j  av  a2  s . c o  m*/
    g.setFont(font);
    g.fillRect(0, 0, 50, 50);
    g.drawString(text, width / 5, height - 10);
    g.dispose();

    ImageIO.write(destImage, DEFAULT_FORMAT, new File("dest.jpg"));
    return dest;
}

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);//from   www  .ja v a 2 s. c  om
    g.fillRect(0, 0, WIDTH, HEIGHT);

    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: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);//ww w.ja v  a 2 s .co m
    dest.createGraphics().drawImage(src, 0, 0, null);
    return dest;
}

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

/**
 * //from w  w  w. j  a  v a 2  s .c  om
 * @param request
 * @param response
 * @param cacheManagerForResource 
 */
public static void getRandcode(HttpServletRequest request, HttpServletResponse response,
        CacheManagerForResource cacheManagerForResource) {
    // BufferedImageImage,Image????
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
    Graphics g = image.getGraphics(); // ImageGraphics,?????
    g.fillRect(0, 0, width, height);
    g.setFont(new Font("Times New Roman", Font.ROMAN_BASELINE, 18));
    g.setColor(getRandColor(110, 133));
    // 
    for (int i = 0; i <= lineSize; i++) {
        drowLine(g);
    }
    // ?
    String randomString = "";
    for (int i = 1; i <= stringNum; i++) {
        randomString = drowString(g, randomString, i);
    }
    String key = null;
    if (request.getCookies() != null) {
        for (Cookie tmp : request.getCookies()) {
            if (tmp.getName().equals(Constants.RANDOMCODEKEY)) {
                key = tmp.getName();
                cacheManagerForResource.removeFromCache(key);
                break;
            }
        }
    }
    if (StringUtils.isEmpty(key)) {
        key = String.valueOf(System.nanoTime());
    }
    cacheManagerForResource.setToCache(key, randomString);
    final Cookie cookie = new Cookie(Constants.RANDOMCODEKEY, key);
    cookie.setPath(Constants.COOKIE_PATH);
    response.addCookie(cookie);
    g.dispose();
    try {
        ImageIO.write(image, "JPEG", response.getOutputStream()); // ??
    } catch (Exception e) {
        LOG.info(e.getMessage());
    }
}

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);// www  . j  a va 2s  .c o m
    g.fillRect(0, 0, 15, 15);
    g.setColor(Color.RED);
    g.fillOval(5, 3, 6, 6);

    //Clean up.
    g.dispose();

    //Return it.
    return bi;
}

From source file: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);//w  w  w.  ja  v  a 2  s. c o  m
    g.fillRect(0, 0, 15, 15);
    g.setColor(Color.RED);
    g.fillOval(5, 3, 6, 6);

    // Clean up.
    g.dispose();

    // Return it.
    return bi;
}

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 av a2 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:org.tolven.security.bean.DocProtectionBean.java

/**
 * Place the resulting scaled image into the output buffer
 * @param image The scaled image/*  ww w  .j  av a2 s.  c o  m*/
 * @param windowWidth The desired window width to return
 * @param windowHeight The desired window hight to return
 * @return A Buffered image, ready for output
 */
static public BufferedImage toBufferedImage(Image image, int windowWidth, int windowHeight) {
    image = new ImageIcon(image).getImage();
    BufferedImage bufferedImage = new BufferedImage(windowWidth, windowHeight, BufferedImage.TYPE_INT_RGB);
    Graphics g = bufferedImage.createGraphics();
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, windowWidth, windowHeight);
    // Center image in window
    int hOffset = (windowWidth - image.getWidth(null)) / 2;
    int vOffset = (windowHeight - image.getHeight(null)) / 2;
    g.drawImage(image, hOffset, vOffset, null);
    g.dispose();
    return bufferedImage;
}