Java Utililty Methods Draw Image

List of utility methods to do Draw Image

Description

The list of methods to do Draw Image are organized into topic(s).

Method

voiddraw(BufferedImage image, Consumer action)
Draw onto an image
Graphics2D g = image.createGraphics();
try {
    action.accept(g);
} finally {
    g.dispose();
voiddraw(BufferedImage image, Rectangle rectangle, BufferedImage backgroundImg)
draw
Graphics2D g = backgroundImg.createGraphics();
if (rectangle == null) {
    g.drawImage(image, 0, 0, null);
} else {
    g.drawImage(image, rectangle.x, rectangle.y, null);
g.dispose();
BufferedImagedraw(final Image img, final int type)
draw
final BufferedImage imgCopy = new BufferedImage(img.getWidth(null), img.getHeight(null), type);
imgCopy.createGraphics().drawImage(img, 0, 0, null);
return imgCopy;
voiddraw4on1(BufferedImage[] src, BufferedImage dst)
drawon
Graphics2D g2 = dst.createGraphics();
g2.setColor(java.awt.Color.WHITE);
g2.fillRect(0, 0, dst.getWidth(), dst.getHeight());
int dxi;
int dyi = 0;
int x0 = dst.getWidth() - 5;
int y0 = 5;
int x = x0;
...
voiddraw9Patch(BufferedImage image, Integer[] patches, float ratio)
draw Patch
int width = image.getWidth();
int height = image.getHeight();
int wStart = (int) (patches[0] * ratio) + 1; 
int wEnd = (int) (width - patches[1] * ratio) - 1;
int hStart = (int) (patches[2] * ratio) + 1; 
int hEnd = (int) (height - patches[3] * ratio) - 1;
for (int i = wStart; i < wEnd; i++) {
    image.setRGB(i, 0, 0xFF000000);
...
voiddrawBorder(BufferedImage buffImage)
draws a black border around the image
Graphics2D g2D = buffImage.createGraphics();
int width = buffImage.getWidth();
int height = buffImage.getHeight();
g2D.setColor(Color.BLACK);
g2D.drawLine(0, height - 1, width - 1, height - 1);
g2D.drawLine(width - 1, 0, width - 1, height - 1);
g2D.drawLine(0, 0, width - 1, 0);
g2D.drawLine(0, 0, 0, height - 1);
...
BufferedImagedrawBorder(BufferedImage originalImage, Color borderColor, int borderWidth)
draw Border
BufferedImage bi = new BufferedImage(originalImage.getWidth() + (2 * borderWidth),
        originalImage.getHeight() + (2 * borderWidth), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bi.createGraphics();
g.setColor(borderColor);
g.drawImage(originalImage, borderWidth, borderWidth, null);
BasicStroke stroke = new BasicStroke(borderWidth * 2);
g.setStroke(stroke);
g.drawRect(0, 0, bi.getWidth(), bi.getHeight());
...
voiddrawBorders(final BufferedImage image, final int currentX, final int currentY, final int width, final int height, final int subImageWidth, final int subImageHeight, final Color c)
Colors the borders of a certain rectangle.
int x, y;
for (int a = 0; a < subImageWidth; a++) {
    x = currentX * width + a;
    y = currentY * height;
    colorPixel(image, x, y, c);
    y = currentY * height + subImageHeight - 1;
    colorPixel(image, x, y, c);
for (int b = 1; b < subImageHeight - 1; b++) {
    x = currentX * width;
    y = currentY * height + b;
    colorPixel(image, x, y, c);
    x = currentX * width + subImageWidth - 1;
    colorPixel(image, x, y, c);
BufferedImagedrawBoundingPolygon(BufferedImage canvas, Polygon p, Color fgColour)
draw Bounding Polygon
Graphics2D g2d = canvas.createGraphics();
g2d.setColor(fgColour);
g2d.drawPolygon(p);
return canvas;
voiddrawBubble(BufferedImage img, Graphics g, Component component)
draw Bubble
g.drawImage(img.getSubimage(0, 0, 31, 31), 0, 0, 14, 14, null);
g.drawImage(img.getSubimage(31, 0, 10, 31), 14, 0, component.getWidth() - 14 - 14, 14, null);
g.drawImage(img.getSubimage(img.getWidth() - 31, 0, 31, 31), component.getWidth() - 14, 0, 14, 14, null);
g.drawImage(img.getSubimage(0, 31, 31, img.getHeight() - 31 - 25), 0, 14, 14,
        component.getHeight() - 14 - 14, null);
g.drawImage(img.getSubimage(31, 31, 5, img.getHeight() - 31 - 25), 14, 14, component.getWidth() - 14 - 14,
        component.getHeight() - 14 - 14, null);
g.drawImage(img.getSubimage(img.getWidth() - 25, 31, 25, img.getHeight() - 31 - 25),
...