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

📄 imageutil.java

📁 一个java的免费游戏引擎,里面有文档,和例子
💻 JAVA
字号:
// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi 

package com.golden.gamedev.util;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.io.*;
import java.net.URL;
import javax.imageio.ImageIO;

public class ImageUtil
{

    public static final GraphicsConfiguration CONFIG = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
    private static final MediaTracker b;

    public static BufferedImage getImage(URL url, int j)
    {
        BufferedImage bufferedimage;
        Image image;
        i(image = Toolkit.getDefaultToolkit().getImage(url));
        Graphics2D graphics2d;
        (graphics2d = (bufferedimage = createImage(image.getWidth(null), image.getHeight(null), j)).createGraphics()).setComposite(AlphaComposite.Src);
        graphics2d.drawImage(image, 0, 0, null);
        graphics2d.dispose();
        return bufferedimage;
        Exception exception;
        exception;
        System.err.println("ERROR: Unable to load Image = " + url);
        exception.printStackTrace();
        return createImage(50, 50);
    }

    public static BufferedImage getImage(URL url)
    {
        return getImage(url, 1);
    }

    public static BufferedImage getImage(URL url, Color color)
    {
        Image image;
        i(image = Toolkit.getDefaultToolkit().getImage(url));
        return applyMask(image, color);
        Exception exception;
        exception;
        System.err.println("ERROR: Unable to load Image = " + url);
        exception.printStackTrace();
        return createImage(50, 50);
    }

    public static BufferedImage[] getImages(URL url, int j, int k, int l)
    {
        return splitImages(getImage(url, l), j, k);
    }

    public static BufferedImage[] getImages(URL url, int j, int k)
    {
        return splitImages(getImage(url), j, k);
    }

    public static BufferedImage[] getImages(URL url, int j, int k, Color color)
    {
        return splitImages(getImage(url, color), j, k);
    }

    public static BufferedImage createImage(int j, int k, int l)
    {
        return CONFIG.createCompatibleImage(j, k, l);
    }

    public static BufferedImage createImage(int j, int k)
    {
        return createImage(j, k, 1);
    }

    private static void i(Image image)
    {
        if(image == null)
            throw new NullPointerException();
        try
        {
            b.addImage(image, 0);
            b.waitForAll();
            if((b.statusID(0, true) & 0x4) != 0)
                throw new RuntimeException();
            else
                return;
        }
        catch(Exception exception)
        {
            b.removeImage(image, 0);
            throw exception;
        }
    }

    public static BufferedImage applyMask(Image image, Color color)
    {
        BufferedImage bufferedimage;
        Graphics2D graphics2d;
        (graphics2d = (bufferedimage = createImage(image.getWidth(null), image.getHeight(null), 2)).createGraphics()).setComposite(AlphaComposite.Src);
        graphics2d.drawImage(image, 0, 0, null);
        graphics2d.dispose();
        for(int j = 0; j < bufferedimage.getHeight(); j++)
        {
            for(int k = 0; k < bufferedimage.getWidth(); k++)
            {
                int l;
                if((l = bufferedimage.getRGB(k, j)) == color.getRGB())
                    bufferedimage.setRGB(k, j, l & 0xffffff);
            }

        }

        return bufferedimage;
    }

    public static BufferedImage[] splitImages(BufferedImage bufferedimage, int j, int k)
    {
        int l = j * k;
        int i1 = 0;
        int l1 = bufferedimage.getWidth() / j;
        int i2 = bufferedimage.getHeight() / k;
        BufferedImage abufferedimage[] = new BufferedImage[l];
        for(int k1 = 0; k1 < k; k1++)
        {
            for(int j1 = 0; j1 < j; j1++)
            {
                int j2 = bufferedimage.getColorModel().getTransparency();
                abufferedimage[i1] = createImage(l1, i2, j2);
                Graphics2D graphics2d;
                (graphics2d = abufferedimage[i1].createGraphics()).drawImage(bufferedimage, 0, 0, l1, i2, j1 * l1, k1 * i2, (j1 + 1) * l1, (k1 + 1) * i2, null);
                graphics2d.dispose();
                i1++;
            }

        }

        return abufferedimage;
    }

    public static BufferedImage rotate(BufferedImage bufferedimage, int j)
    {
        int k = bufferedimage.getWidth();
        int l = bufferedimage.getHeight();
        int i1 = bufferedimage.getColorModel().getTransparency();
        BufferedImage bufferedimage1;
        Graphics2D graphics2d;
        (graphics2d = (bufferedimage1 = createImage(k, l, i1)).createGraphics()).setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2d.rotate(Math.toRadians(j), k / 2, l / 2);
        graphics2d.drawImage(bufferedimage, 0, 0, null);
        graphics2d.dispose();
        return bufferedimage1;
    }

    public static BufferedImage resize(BufferedImage bufferedimage, int j, int k)
    {
        int l = bufferedimage.getColorModel().getTransparency();
        BufferedImage bufferedimage1;
        Graphics2D graphics2d;
        (graphics2d = (bufferedimage1 = createImage(j, k, l)).createGraphics()).setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2d.drawImage(bufferedimage, 0, 0, j, k, 0, 0, bufferedimage.getWidth(), bufferedimage.getHeight(), null);
        graphics2d.dispose();
        return bufferedimage1;
    }

    public static BufferedImage flip(BufferedImage bufferedimage)
    {
        int j = bufferedimage.getWidth();
        int k = bufferedimage.getHeight();
        BufferedImage bufferedimage1;
        Graphics2D graphics2d;
        (graphics2d = (bufferedimage1 = createImage(j, k, bufferedimage.getColorModel().getTransparency())).createGraphics()).drawImage(bufferedimage, 0, 0, j, k, j, 0, 0, k, null);
        graphics2d.dispose();
        return bufferedimage1;
    }

    public static void saveImage(BufferedImage bufferedimage, File file)
    {
        try
        {
            ImageIO.write(bufferedimage, "png", file);
            return;
        }
        catch(IOException ioexception)
        {
            System.err.println("ERROR: unable to save = " + file);
            ioexception.printStackTrace();
            return;
        }
    }

    public static byte[] toByteArray(BufferedImage bufferedimage, String s)
    {
        if(bufferedimage != null)
        {
            ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream(1024);
            try
            {
                ImageIO.write(bufferedimage, s, bytearrayoutputstream);
            }
            catch(IOException ioexception)
            {
                throw new IllegalStateException(ioexception.toString());
            }
            return bytearrayoutputstream.toByteArray();
        } else
        {
            return new byte[0];
        }
    }

    public static BufferedImage fromByteArray(byte abyte0[])
    {
        if(abyte0 != null && abyte0.length > 0)
            return ImageIO.read(new ByteArrayInputStream(abyte0));
        return null;
        IOException ioexception;
        ioexception;
        throw new IllegalArgumentException(ioexception.toString());
    }

    static 
    {
        Canvas canvas = new Canvas();
        b = new MediaTracker(canvas);
    }
}

⌨️ 快捷键说明

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