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

voiddrawImageInRect(Graphics2D gr, Image img, Rectangle r, boolean noShrink)
draw an image in a rectangle
gr.drawImage(img, r.x, r.y, r.width, r.height, null);
if (noShrink) {
    int imgwidth = img.getWidth(null);
    int imgheight = img.getHeight(null);
    if (imgwidth > r.width && imgheight > r.height) {
        Shape oldclip = gr.getClip();
        Rectangle2D clip = new Rectangle2D.Double(r.getX(), r.getY(), r.getWidth(), r.getHeight());
        gr.clip(clip);
...
voiddrawImageWithClipping(Graphics g, BufferedImage img)
draw Image With Clipping
assert img != null;
Rectangle clipBounds = g.getClipBounds();
int clipX = (int) clipBounds.getX();
int clipY = (int) clipBounds.getY();
int clipWidth = (int) clipBounds.getWidth();
int clipHeight = (int) clipBounds.getHeight();
int clipX2 = clipX + clipWidth;
int clipY2 = clipY + clipHeight;
...
voiddrawInMiddle(Graphics g, Image image, String text)
Draws the text in the center of the image.
int width = image.getWidth(null);
int height = image.getHeight(null);
FontMetrics fm = g.getFontMetrics();
int midX = (width - fm.stringWidth(text)) / 2;
int midY = (height + fm.getHeight() / 2) / 2;
g.drawString(text, midX, midY);
BufferedImagedrawLabel(BufferedImage image, String text)
draw Label
Graphics2D g = (Graphics2D) image.getGraphics();
g.setColor(new Color(255, 255, 255, 255));
int w1 = image.getWidth() * 2 / 100;
int y1 = 10;
int labelSize = 30;
g.fillRect(w1, y1, image.getWidth() - (w1 + w1), labelSize);
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g.setColor(Color.BLACK);
...
voiddrawNormalImage(Rectangle2D bounds, Graphics2D g2, Image image, ImageObserver observer, int imageWidth, int imageHeight)
draw Normal Image
int x = (int) bounds.getX();
int y = (int) bounds.getY();
int w = Math.min(imageWidth, (int) bounds.getWidth());
int h = Math.min(imageHeight, (int) bounds.getHeight());
g2.drawImage(image, x, y, x + w, y + h, 0, 0, w, h,  observer);
voiddrawOnto(final BufferedImage pDestination, final Image pSource)
Draws the source image onto the buffered image, using AlphaComposite.Src and coordinates 0, 0 .
Graphics2D g = pDestination.createGraphics();
try {
    g.setComposite(AlphaComposite.Src);
    g.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE);
    g.drawImage(pSource, 0, 0, null);
} finally {
    g.dispose();
voiddrawPixel(BufferedImage img, int x, int y, int color)
draw Pixel
img.setRGB(x, y, color);
voiddrawPixel(int x, int y, Color color, BufferedImage displayMap)
draw Pixel
if (x < 0 || y < 0 || x >= displayMap.getWidth() || y >= displayMap.getHeight()) {
    return;
displayMap.setRGB(x, y, color.getRGB());
voiddrawScaledImage(Graphics graphics, BufferedImage image, int x, int y, int w, int h, int left, int right)
draw Scaled Image
int tw = image.getWidth();
int th = image.getHeight();
graphics.drawImage(image, x, y, x + left, y + h, 0, 0, left, th, null);
graphics.drawImage(image, x + left, y, x + w - right, y + h, left, 0, tw - right, th, null);
graphics.drawImage(image, x + w - right, y, x + w, y + h, tw - right, 0, tw, th, null);
voiddrawScaledImage(Image image, Component canvas, Graphics g)
draw Scaled Image
int imgWidth = image.getWidth(null);
int imgHeight = image.getHeight(null);
double imgAspect = (double) imgHeight / imgWidth;
int canvasWidth = canvas.getWidth();
int canvasHeight = canvas.getHeight();
double canvasAspect = (double) canvasHeight / canvasWidth;
int x1 = 0; 
int y1 = 0; 
...