⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imagecreator.java

📁 一段很好的绘制图形控件的java源代码!很具有学习价值!
💻 JAVA
字号:
import java.awt.*;
import java.awt.image.*;

/*
 * Created on 2005-6-17
 *
 */

/**
 * @author zhangjian
 * http://www.ismyway.com
 */

public class ImageCreator {
    public static final Color mainMidColor = new Color(0, 64, 196);
    public static final Color mainUltraDarkColor = new Color(0, 0, 64);

    //public static final Color mainMidColor = new Color(255, 128, 192);
    //public static final Color mainUltraDarkColor = new Color(66, 0, 33);
    public static final int CUBE_DIMENSION = 5;

    public ImageCreator() {
    }

    public static BufferedImage getGradientCubesImage(int width, int height,
            Color leftColor, Color rightColor, int transitionStart,
            int transitionEnd) {
        BufferedImage image = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_ARGB);

        Graphics2D graphics = (Graphics2D) image.getGraphics();
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        GradientPaint gradient = new GradientPaint(transitionStart, 0,
                leftColor, transitionEnd, 0, rightColor);
        graphics.setPaint(gradient);
        graphics.fillRect(transitionStart, 0, transitionEnd - transitionStart,
                height);

        graphics.setColor(leftColor);
        graphics.fillRect(0, 0, transitionStart, height);

        graphics.setColor(rightColor);
        graphics.fillRect(transitionEnd, 0, width - transitionEnd, height);

        int cubeCountY = height / ImageCreator.CUBE_DIMENSION;
        int cubeCountX = 1 + (transitionEnd - transitionStart)
                / ImageCreator.CUBE_DIMENSION;
        int cubeStartY = (height % ImageCreator.CUBE_DIMENSION) / 2;
        int cubeStartX = transitionStart
                - (ImageCreator.CUBE_DIMENSION - ((transitionEnd - transitionStart) % ImageCreator.CUBE_DIMENSION));

        for (int col = 0; col < cubeCountX; col++) {
            for (int row = 0; row < cubeCountY; row++) {
                // decide if we should put a cube
                if (Math.random() < 0.5) {
                    continue;
                }
                // Make semi-random choice of color. It should lie
                // close to the interpolated color, but still appear
                // random
                double coef = 1.0 - (((double) col / (double) cubeCountX) + 0.9 * (Math
                        .random() - 0.5));
                coef = Math.max(0.0, coef);
                coef = Math.min(1.0, coef);
                // Compute RGB components
                int r = (int) (coef * leftColor.getRed() + (1.0 - coef)
                        * rightColor.getRed());
                int g = (int) (coef * leftColor.getGreen() + (1.0 - coef)
                        * rightColor.getGreen());
                int b = (int) (coef * leftColor.getBlue() + (1.0 - coef)
                        * rightColor.getBlue());
                // fill cube
                graphics.setColor(new Color(r, g, b));
                graphics.fillRect(cubeStartX + col
                        * ImageCreator.CUBE_DIMENSION, cubeStartY + row
                        * ImageCreator.CUBE_DIMENSION,
                        ImageCreator.CUBE_DIMENSION,
                        ImageCreator.CUBE_DIMENSION);
                // draw cube's border in slightly brighter color
                graphics.setColor(new Color(255 - (int) (0.95 * (255 - r)),
                        255 - (int) (0.9 * (255 - g)),
                        255 - (int) (0.9 * (255 - b))));
                graphics.drawRect(cubeStartX + col
                        * ImageCreator.CUBE_DIMENSION, cubeStartY + row
                        * ImageCreator.CUBE_DIMENSION,
                        ImageCreator.CUBE_DIMENSION,
                        ImageCreator.CUBE_DIMENSION);
            }
        }

        return image;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -