Advertisement
crazysaem

Custom Shape in Java based on an Image

Oct 6th, 2011
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.72 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.geom.Area;
  3. import java.awt.image.BufferedImage;
  4. import javax.imageio.ImageIO;
  5. import java.io.File;
  6. import java.io.IOException;
  7.  
  8.  
  9. /**
  10.  * CustomShape
  11.  * based on a Class from Andrew Thompson *
  12.  * Source: http://stackoverflow.com/questions/7052422/image-graphic-into-a-shape-in-java/7059497#7059497
  13.  * @author Samuel Schneider, Andrew Thompson
  14.  *
  15.  *
  16.  */
  17. class CustomShape {
  18.    
  19.     private BufferedImage image=null;
  20.    
  21.     /**
  22.      * Creates an Area with PixelPerfect precision
  23.      * @param color The color that is draws the Custom Shape
  24.      * @param tolerance The color tolerance
  25.      * @return Area
  26.      */
  27.     public Area getArea(Color color, int tolerance) {
  28.         if(image==null) return null;
  29.         Area area = new Area();
  30.         for (int x=0; x<image.getWidth(); x++) {
  31.             for (int y=0; y<image.getHeight(); y++) {
  32.                 Color pixel = new Color(image.getRGB(x,y));
  33.                 if (isIncluded(color, pixel, tolerance)) {
  34.                     Rectangle r = new Rectangle(x,y,1,1);
  35.                     area.add(new Area(r));
  36.                 }
  37.             }
  38.         }
  39.        
  40.         return area;
  41.     }
  42.    
  43.     public Area getArea_FastHack() {
  44.         //Assumes Black as Shape Color
  45.         if(image==null) return null;
  46.        
  47.         Area area = new Area();
  48.         Rectangle r;
  49.         int y1,y2;
  50.        
  51.         for (int x=0; x<image.getWidth(); x++) {
  52.             y1=99;
  53.             y2=-1;
  54.             for (int y=0; y<image.getHeight(); y++) {
  55.                 Color pixel = new Color(image.getRGB(x,y));
  56.                 //-16777216 entspricht RGB(0,0,0)
  57.                 if (pixel.getRGB()==-16777216) {
  58.                     if(y1==99) {y1=y;y2=y;}
  59.                     if(y>(y2+1)) {
  60.                         r = new Rectangle(x,y1,1,y2-y1);
  61.                         area.add(new Area(r));
  62.                         y1=y;y2=y;
  63.                     }
  64.                     y2=y;
  65.                 }              
  66.             }
  67.             if((y2-y1)>=0) {
  68.                 r = new Rectangle(x,y1,1,y2-y1);
  69.                 area.add(new Area(r));
  70.             }
  71.         }
  72.        
  73.         return area;
  74.     }
  75.  
  76.     public static boolean isIncluded(Color target, Color pixel, int tolerance) {
  77.         int rT = target.getRed();
  78.         int gT = target.getGreen();
  79.         int bT = target.getBlue();
  80.         int rP = pixel.getRed();
  81.         int gP = pixel.getGreen();
  82.         int bP = pixel.getBlue();
  83.         return(
  84.             (rP-tolerance<=rT) && (rT<=rP+tolerance) &&
  85.             (gP-tolerance<=gT) && (gT<=gP+tolerance) &&
  86.             (bP-tolerance<=bT) && (bT<=bP+tolerance) );
  87.     }
  88.    
  89.     public CustomShape(String path) {
  90.         try {
  91.             BufferedImage image = ImageIO.read(new File(path));
  92.             this.image = image;
  93.         } catch (IOException e) {
  94.             // TODO Auto-generated catch block
  95.             e.printStackTrace();
  96.         }
  97.     }
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement