Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Point;
- import java.awt.Rectangle;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import java.util.HashMap;
- import javax.imageio.ImageIO;
- public class GraphicsObjectConfiguration {
- HashMap<String,config> configbase = new HashMap<String,config>();
- HashMap<String,HashMap<Integer,HashMap<Point,Color>>> framebase = new HashMap<String,HashMap<Integer, HashMap<Point,Color>>>();
- GraphicsObject[] GraphicsObjects;
- LightSource[] LightSources;
- public class config{
- HashMap<Point, Color> configuration;
- BufferedImage image;
- final int[] originalcolor;
- int[] modcolor;
- int width;
- int height;
- config(HashMap<Point, Color> configurationdata,int[] OriginalColor,BufferedImage Image,int w,int h)
- {
- width=w;
- height=h;
- configuration=configurationdata;
- image=Image;
- originalcolor=OriginalColor;
- modcolor=new int[width*height];
- }
- }
- public int getOccupied(Object[] a)
- {
- int count = 0;
- for(int i = 0;i<a.length;i++)
- {
- if(a[i]!=null)
- {
- count++;
- }
- }
- return count;
- }
- public void AddGraphicsObject(GraphicsObject o)
- {
- GraphicsObjects[getOccupied(GraphicsObjects)+1]=o;
- }
- public void AddLightSource(LightSource o)
- {
- LightSources[getOccupied(LightSources)+1]=o;
- }
- public GraphicsObjectConfiguration(Window window) { // We'll connect all objects to the main window's data
- GraphicsObjects=window.GraphicsObjects;
- LightSources=window.LightSources;
- }
- private Object CreateGraphicsConfig(String type,File file) throws IOException
- {
- File img;
- BufferedImage image;
- HashMap<Point,Color> configurationdata = new HashMap<Point,Color>();
- if(file==null)
- {
- System.out.println("\nWriting configuration for Graphics Object type "+type);
- img = new File(GameMain.ImageAssets+type+".png");
- }
- else
- {
- img = file;
- }
- image = ImageIO.read(img);
- Point TopLeft = null;
- Point BottomRight = null;
- Point CurrentPoint = null;
- int pxL = 1000;
- int pyL = 1000;
- int pxG = 0;
- int pyG = 0;
- for(int x = 0;x<image.getWidth();x++) // Trim the object down to its essentials
- {
- for(int y = 0;y<image.getHeight();y++)
- {
- Color rgb = new Color(image.getRGB(x, y));
- if(!rgb.equals(Color.BLACK))
- {
- CurrentPoint = new Point(x,y);
- int px = CurrentPoint.x;
- int py = CurrentPoint.y;
- if(px<pxL)
- {
- pxL = px;
- }
- if(py<pyL)
- {
- pyL = py;
- }
- if(px>pxG)
- {
- pxG=px;
- }
- if(py>pyG)
- {
- pyG = py;
- }
- }
- }
- }
- TopLeft = new Point(pxL,pyL);
- BottomRight = new Point(pxG,pyG);
- int width = (BottomRight.x-TopLeft.x);
- int height = (BottomRight.y-TopLeft.y);
- image = image.getSubimage(TopLeft.x,TopLeft.y,width,height);
- int imagewidth = image.getWidth();
- int imageheight = image.getHeight();
- int[] originalcolor = new int[(imagewidth*imageheight)];
- for(int x = 0;x<image.getWidth();x++)
- {
- for(int y = 0;y<image.getHeight();y++)
- {
- Point pixelpoint = new Point(x,y);
- originalcolor[imagewidth*y+x]=image.getRGB(x, y); // Fill its original color array
- Color rgb = new Color(image.getRGB(x, y)); // Get its desired color based on pixel position
- configurationdata.put(pixelpoint,rgb); // From top left corner
- }
- }
- if(file==null)
- {
- System.out.println("Finished writing configuration for Graphics Object type "+type);
- config config = new config(configurationdata,originalcolor,image,imagewidth,imageheight);
- configbase.put(type,config);
- return config;
- }
- else
- {
- return configurationdata;
- }
- }
- public config get(String type) // Get the configuration for a base Graphics Object
- {
- config config = configbase.get(type);
- if(config!=null)
- {
- return config;
- }
- else
- {
- try {
- return (GraphicsObjectConfiguration.config) CreateGraphicsConfig(type,null);
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- return null;
- }
- public HashMap<Integer, HashMap<Point, Color>> getframes(String type) throws IOException // Get the frame configuration for a Static Particles
- {
- HashMap<Integer, HashMap<Point, Color>> frames = framebase.get(type);
- if(frames!=null)
- {
- return frames;
- }
- else
- {
- frames = new HashMap<Integer,HashMap<Point,Color>>();
- String lookin = GameMain.ImageAssets+"StaticParticles\\"+type;
- for(int i = 1;i<100;i++)
- {
- File file = new File(lookin+"\\"+"Frame"+Integer.toString(i)+".png");
- if(file.exists())
- {
- System.out.println(file);
- HashMap<Point, Color> thisconfig = (HashMap<Point,Color>) CreateGraphicsConfig(type,file);
- frames.put(i,thisconfig);
- }
- else
- {
- // Reached end of frame series
- break;
- }
- }
- framebase.put(type,frames);
- return frames;
- }
- }
- }
Add Comment
Please, Sign In to add comment