draw Image - Java java.awt

Java examples for java.awt:Graphics2D

Description

draw Image

Demo Code


//package com.java2s;

import java.awt.Component;

import java.awt.Graphics;

import java.awt.Image;

public class Main {
    public static void drawImage(Graphics pad, Image img, int width,
            int height, Component parent) {
        drawImage(pad, img, 0, 0, width, height, parent);
    }//from   w ww.  ja  v  a  2s . co  m

    public static void drawImage(Graphics pad, Image img, int x, int y,
            int width, int height, Component parent) {
        if (img == null)
            return;

        int imgWidth = img.getWidth(parent);
        int imgHeight = img.getHeight(parent);

        if (imgWidth < 0 || imgHeight < 0)
            return;

        int startX = x;
        int startY = y;
        for (startY = x; startY < height; startY += imgHeight) {
            for (startX = x; startX < width; startX += imgWidth)
                pad.drawImage(img, startX, startY, imgWidth, imgHeight,
                        parent);
            startX -= imgWidth;
            pad.drawImage(img, startX, startY, imgWidth, imgHeight, parent);
        }

        startY -= imgHeight;
        for (startX = x; startX < width; startX += imgWidth)
            pad.drawImage(img, startX, startY, imgWidth, imgHeight, parent);
        startX -= imgWidth;
        pad.drawImage(img, startX, startY, imgWidth, imgHeight, parent);
    }
}

Related Tutorials