Java 怎么绘制图像

2019年8月20日22:29:33 评论 23

1.简介

Java 怎么绘制图像?——请看下文。

2.代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */



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);
    }

    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);
    }
}

 

3.总结

示例代码演示了 Java 怎么绘制图像,具体步骤参见示例代码。

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: