tyridge77

GraphicsObjectConfiguration

Feb 20th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.00 KB | None | 0 0
  1.  
  2.  
  3. import java.awt.Color;
  4. import java.awt.Point;
  5. import java.awt.Rectangle;
  6. import java.awt.image.BufferedImage;
  7. import java.io.File;
  8. import java.io.IOException;
  9. import java.util.HashMap;
  10.  
  11. import javax.imageio.ImageIO;
  12.  
  13.  
  14. public class GraphicsObjectConfiguration {
  15.    
  16.  HashMap<String,config> configbase = new HashMap<String,config>();
  17.  HashMap<String,HashMap<Integer,HashMap<Point,Color>>> framebase = new HashMap<String,HashMap<Integer,  HashMap<Point,Color>>>();
  18.  
  19.  GraphicsObject[] GraphicsObjects;
  20.  LightSource[] LightSources;
  21.  
  22.  
  23.  public class config{
  24.   HashMap<Point, Color> configuration;
  25.   BufferedImage image;
  26.   final int[] originalcolor;
  27.   int[] modcolor;
  28.   int width;
  29.   int height;
  30.  
  31.  
  32.   config(HashMap<Point, Color> configurationdata,int[] OriginalColor,BufferedImage Image,int w,int h)
  33.   {
  34.    width=w;
  35.    height=h;
  36.    configuration=configurationdata;
  37.    image=Image;
  38.    originalcolor=OriginalColor;
  39.    modcolor=new int[width*height];
  40.   }
  41.  }
  42.  
  43.  public int getOccupied(Object[] a)
  44.  {
  45.   int count = 0;
  46.   for(int i = 0;i<a.length;i++)
  47.   {
  48.    if(a[i]!=null)
  49.    {
  50.     count++;  
  51.    }
  52.   }
  53.   return count;
  54.  }
  55.  
  56.  public void AddGraphicsObject(GraphicsObject o)
  57.  {
  58.   GraphicsObjects[getOccupied(GraphicsObjects)+1]=o;
  59.  }
  60.  public void AddLightSource(LightSource o)
  61.  {
  62.   LightSources[getOccupied(LightSources)+1]=o;
  63.  }
  64.  
  65.  
  66.  
  67.  public GraphicsObjectConfiguration(Window window) {            // We'll connect all objects to the main window's data
  68.  GraphicsObjects=window.GraphicsObjects;
  69.  LightSources=window.LightSources;
  70. }
  71.  
  72. private Object CreateGraphicsConfig(String type,File file) throws IOException
  73.  {
  74.      File img;
  75.      BufferedImage image;          
  76.      HashMap<Point,Color> configurationdata = new HashMap<Point,Color>();
  77.  
  78.      if(file==null)
  79.      {
  80.      System.out.println("\nWriting configuration for Graphics Object type "+type);
  81.      img = new File(GameMain.ImageAssets+type+".png");
  82.      }
  83.      else
  84.      {
  85.       img = file;
  86.      }
  87.      image = ImageIO.read(img);
  88.  
  89.      
  90.      Point TopLeft = null;
  91.      Point BottomRight = null;
  92.      Point CurrentPoint = null;
  93.      int pxL = 1000;
  94.      int pyL = 1000;
  95.      int pxG = 0;
  96.      int pyG = 0;
  97.      
  98.      
  99.      for(int x = 0;x<image.getWidth();x++)                  // Trim the object down to its essentials
  100.      {
  101.       for(int y = 0;y<image.getHeight();y++)
  102.       {
  103.        Color rgb = new Color(image.getRGB(x, y));
  104.        if(!rgb.equals(Color.BLACK))
  105.        {
  106.         CurrentPoint = new Point(x,y);
  107.         int px = CurrentPoint.x;
  108.         int py = CurrentPoint.y;
  109.         if(px<pxL)
  110.         {
  111.          pxL = px;
  112.         }
  113.         if(py<pyL)
  114.         {
  115.          pyL = py;
  116.         }
  117.         if(px>pxG)
  118.         {
  119.          pxG=px;
  120.         }
  121.         if(py>pyG)
  122.         {
  123.          pyG = py;
  124.         }
  125.        }
  126.       }
  127.      }
  128.      
  129.      
  130.      
  131.      TopLeft = new Point(pxL,pyL);
  132.      BottomRight = new Point(pxG,pyG);
  133.      
  134.    
  135.      int width = (BottomRight.x-TopLeft.x);
  136.      int height = (BottomRight.y-TopLeft.y);
  137.      
  138.      
  139.      image = image.getSubimage(TopLeft.x,TopLeft.y,width,height);
  140.      
  141.      int imagewidth = image.getWidth();
  142.      int imageheight = image.getHeight();
  143.  
  144.      int[] originalcolor = new int[(imagewidth*imageheight)];
  145.  
  146.      for(int x = 0;x<image.getWidth();x++)
  147.      {
  148.       for(int y = 0;y<image.getHeight();y++)
  149.       {
  150.            
  151.          
  152.          Point pixelpoint = new Point(x,y);
  153.          originalcolor[imagewidth*y+x]=image.getRGB(x, y);          // Fill its original color array
  154.  
  155.                
  156.          Color rgb = new Color(image.getRGB(x, y));                 // Get its desired color based on pixel position
  157.          configurationdata.put(pixelpoint,rgb);                 // From top left corner
  158.       }
  159.      }
  160.     if(file==null)
  161.     {
  162.      System.out.println("Finished writing configuration for Graphics Object type "+type);
  163.    
  164.      config config = new config(configurationdata,originalcolor,image,imagewidth,imageheight);
  165.    
  166.      configbase.put(type,config);
  167.      return config;
  168.     }
  169.     else
  170.     {
  171.      return configurationdata; 
  172.     }
  173.  }
  174.  
  175.  public config get(String type)               // Get the configuration for a base Graphics Object
  176.  {
  177.   config config = configbase.get(type);
  178.   if(config!=null)
  179.   {
  180.    return config;
  181.   }
  182.   else
  183.   {
  184.    try {
  185.     return (GraphicsObjectConfiguration.config) CreateGraphicsConfig(type,null);
  186.    }
  187.    catch (IOException e)
  188.    {
  189.     e.printStackTrace();
  190.    }
  191.   }
  192.   return null;
  193.  }
  194.  
  195.  
  196.  public HashMap<Integer, HashMap<Point, Color>> getframes(String type) throws IOException           // Get the frame configuration for a Static Particles
  197.  {
  198.   HashMap<Integer, HashMap<Point, Color>> frames = framebase.get(type);
  199.  
  200.   if(frames!=null)
  201.   {
  202.    return frames;
  203.   }
  204.   else
  205.   {
  206.    frames = new HashMap<Integer,HashMap<Point,Color>>();
  207.    
  208.    String lookin = GameMain.ImageAssets+"StaticParticles\\"+type;
  209.    for(int i = 1;i<100;i++)
  210.    {
  211.     File file = new File(lookin+"\\"+"Frame"+Integer.toString(i)+".png");
  212.     if(file.exists())
  213.     {
  214.      System.out.println(file);
  215.      HashMap<Point, Color> thisconfig = (HashMap<Point,Color>) CreateGraphicsConfig(type,file);
  216.      frames.put(i,thisconfig);
  217.     }
  218.     else
  219.     {
  220.      // Reached end of frame series
  221.      break;
  222.     }
  223.    }
  224.    framebase.put(type,frames);
  225.    return frames;
  226.   }
  227.  }
  228.  
  229.  
  230. }
Add Comment
Please, Sign In to add comment