Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.deadendgine.utils;
- import java.awt.AlphaComposite;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.GraphicsConfiguration;
- import java.awt.GraphicsDevice;
- import java.awt.GraphicsEnvironment;
- import java.awt.RenderingHints;
- import java.awt.Transparency;
- import java.awt.geom.AffineTransform;
- import java.awt.image.BufferedImage;
- import java.awt.image.VolatileImage;
- import java.io.File;
- import javax.imageio.ImageIO;
- /**
- *
- * @author Troy
- * @version 1.0
- *
- */
- public class ImageUtils {
- private static final ImageUtils instance = new ImageUtils();
- public static GraphicsEnvironment graphicsEnviroment = GraphicsEnvironment.getLocalGraphicsEnvironment();
- public static GraphicsDevice graphicsDevice = graphicsEnviroment.getDefaultScreenDevice();
- public static GraphicsConfiguration graphicsConfiguration = graphicsDevice.getDefaultConfiguration();
- private static AffineTransform affineTransform = new AffineTransform();
- private static BufferedImage bimg;
- private static BufferedImage cimg;
- private static BufferedImage imgs[];
- private static BufferedImage imgss[][];
- private static VolatileImage rt;
- private static Graphics g;
- private static Graphics2D g2;
- private static int w;
- private static int h;
- private static int num;
- private static int imageType;
- /**
- * Load a image.
- *
- * Note: Use loadCompatibleImage where possible.
- *
- * @param ref
- * @return BufferedImage
- */
- public static BufferedImage loadBufferedImage(String ref){
- bimg = null;
- try{
- if(ref.startsWith("res"))
- bimg = ImageIO.read(instance.getClass().getClassLoader().getResource(ref));
- else
- bimg = ImageIO.read(new File(ref));
- }catch(Exception e){e.printStackTrace();}
- return bimg;
- }
- /**
- * Load a image from resources and save it to a compatible
- * image for best performance from the graphics card.
- *
- * @param ref
- * @return BufferedImage
- */
- public static BufferedImage loadCompatibleBufferedImage(String ref){
- bimg = null;
- cimg = null;
- try{
- if(ref.startsWith("res"))
- bimg = ImageIO.read(instance.getClass().getClassLoader().getResource(ref));
- else
- bimg = ImageIO.read(new File(ref));
- cimg = getCompatibleBufferedImage(bimg.getWidth(), bimg.getHeight());
- g = cimg.getGraphics();
- g.drawImage(bimg, 0, 0, null);
- g.dispose();
- }catch(Exception e){e.printStackTrace();}
- return cimg;
- }
- /**
- * Load a image from resources and save it to a compatible
- * image for best performance from the graphics card.
- *
- * @param ref
- * @return VolatileImage
- */
- public static VolatileImage loadCompatibleVolatileImage(String ref){
- bimg = null;
- rt = null;
- try{
- bimg = ImageIO.read(instance.getClass().getClassLoader().getResource(ref));
- rt = getCompatibleVolatileImage(bimg.getWidth(), bimg.getHeight());
- g = rt.getGraphics();
- g.drawImage(bimg, 0, 0, null);
- g.dispose();
- }catch(Exception e){e.printStackTrace();}
- return rt;
- }
- /**
- * This will create a compatible image to be used
- * by the graphics card.
- *
- * @param width
- * @param height
- * @return BufferedImage
- */
- public static BufferedImage getCompatibleBufferedImage(int width, int height){
- return graphicsConfiguration.createCompatibleImage(width, height, Transparency.TRANSLUCENT);
- }
- /**
- * This will create a compatible Volatile image to be used
- * by the graphics card.
- *
- * @param width
- * @param height
- * @return BufferedImage
- */
- public static VolatileImage getCompatibleVolatileImage(int width, int height){
- return graphicsConfiguration.createCompatibleVolatileImage(width, height, Transparency.TRANSLUCENT);
- }
- public static BufferedImage getSubImage(BufferedImage img, int x, int y){
- img.getSubimage(x * 32 + x, y * 32 + y, 32, 32);
- return null;
- }
- /**
- * Load a image array.
- *
- * @param img
- * @param cols
- * @param rows
- * @return BufferedImage[]
- */
- public static BufferedImage[] splitImage(BufferedImage img, int cols, int rows) {
- w = img.getWidth()/cols;
- h = img.getHeight()/rows;
- num = 0;
- imageType = img.getType();
- if(imageType == 0) imageType = 5;
- imgs = new BufferedImage[w*h];
- for(int y = 0; y < rows; y++) {
- for(int x = 0; x < cols; x++) {
- imgs[num] = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
- g2 = imgs[num].createGraphics();
- g2.setComposite(AlphaComposite.Src);
- g2.drawImage(img, 0, 0, w, h, w*x, h*y, w*x+w, h*y+h, null);
- g2.dispose();
- num++;
- }
- }
- return imgs;
- }
- /**
- * Load a image array.
- *
- * @param img
- * @param cols
- * @param rows
- * @return BufferedImage[]
- */
- public static BufferedImage[][] splitImage2D(BufferedImage img, int cols, int rows) {
- w = img.getWidth()/cols;
- h = img.getHeight()/rows;
- imageType = img.getType();
- if(imageType == 0) imageType = 5;
- imgss = new BufferedImage[w][h];
- for(int y = 0; y < rows; y++) {
- for(int x = 0; x < cols; x++) {
- imgss[x][y] = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
- g2 = imgss[x][y].createGraphics();
- g2.setComposite(AlphaComposite.Src);
- g2.drawImage(img, 0, 0, w, h, w*x, h*y, w*x+w, h*y+h, null);
- g2.dispose();
- }
- }
- return imgss;
- }
- /**
- * Make's a certain colour transparent.
- * @param source
- * @param color
- * @return BufferedImage
- */
- public static BufferedImage makeColorTransparent(String ref, Color color) {
- bimg = loadBufferedImage(ref);
- cimg = new BufferedImage(bimg.getWidth(), bimg.getHeight(), BufferedImage.TYPE_INT_ARGB);
- g2 = cimg.createGraphics();
- g2.setComposite(AlphaComposite.Src);
- g2.drawImage(bimg, null, 0, 0);
- g2.dispose();
- for(int i = 0; i < cimg.getHeight(); i++) {
- for(int j = 0; j < cimg.getWidth(); j++) {
- if(cimg.getRGB(j, i) == color.getRGB()) {
- cimg.setRGB(j, i, 0x8F1C1C);
- }
- }
- }
- return cimg;
- }
- /**
- * Load a image with a certain transparency.
- *
- * @param url
- * @param transperancy
- * @return BufferedImage
- */
- public static BufferedImage loadTranslucentImage(String url, float transperancy) {
- bimg = loadBufferedImage(url);
- cimg = new BufferedImage(bimg.getWidth(), bimg.getHeight(), BufferedImage.TRANSLUCENT);
- g2 = cimg.createGraphics();
- g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, transperancy));
- g2.drawImage(bimg, null, 0, 0);
- g2.dispose();
- return cimg;
- }
- /**
- * Change a image with a certain transparency.
- *
- * @param url
- * @param transperancy
- * @return BufferedImage
- */
- public static BufferedImage changeTranslucentImage(BufferedImage img, float transperancy) {
- bimg = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TRANSLUCENT);
- g2 = bimg.createGraphics();
- g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, transperancy));
- g2.drawImage(img, null, 0, 0);
- g2.dispose();
- return bimg;
- }
- /**
- * Flips a given image horizontally.
- * @param source The source image.
- * @return BufferedImage
- */
- public static BufferedImage flipHorizontally(BufferedImage source) {
- w = source.getWidth();
- h = source.getHeight();
- bimg = new BufferedImage(w, h, source.getType());
- g2 = bimg.createGraphics();
- g2.drawImage(source, 0, 0, w, h, w, 0, 0, h, null);
- g2.dispose();
- return bimg;
- }
- /**
- * Flips a given image vertically.
- * @param source The source image.
- * @return BufferedImage
- */
- public static BufferedImage flipVertically(BufferedImage source) {
- w = source.getWidth();
- h = source.getHeight();
- bimg = new BufferedImage(w, h, source.getType());
- g2 = bimg.createGraphics();
- g2.drawImage(source, 0, 0, w, h, 0, h, w, 0, null);
- g2.dispose();
- return bimg;
- }
- /**
- * Rotates a given image to a given angle.
- * @param source The source image.
- * @param angle The angle to rotate by (in degrees).
- * @return BufferedImage
- */
- public static BufferedImage rotate(BufferedImage source, int angle) {
- w = source.getWidth();
- h = source.getHeight();
- bimg = new BufferedImage(w, h, source.getType());
- g2 = bimg.createGraphics();
- g2.rotate(Math.toRadians(angle), w / 2, h / 2);
- g2.drawImage(source, null, 0, 0);
- return bimg;
- }
- /**
- * Resizes a given image to a given width and height.
- * @param source The source image.
- * @param width The new width.
- * @param height The new height.
- * @return BufferedImage
- */
- public static BufferedImage resize(BufferedImage source, int width, int height) {
- w = source.getWidth();
- h = source.getHeight();
- bimg = new BufferedImage(width, height, source.getType());
- g2 = bimg.createGraphics();
- g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
- g2.drawImage(source, 0, 0, width, height, 0, 0, w, h, null);
- g2.dispose();
- return bimg;
- }
- public static AffineTransform getAffineTransform() {
- return affineTransform;
- }
- public static void setAffineTransform(AffineTransform affineTransform) {
- ImageUtils.affineTransform = affineTransform;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement