ShapedWidget.java :  » Game » applet-game-framework » org » applet » game » framework » component » Java Open Source

Java Open Source » Game » applet game framework 
applet game framework » org » applet » game » framework » component » ShapedWidget.java
package org.applet.game.framework.component;

import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;

import org.applet.game.framework.util.ImageLoader;

public class ShapedWidget extends ImageWidget {
  
  int shapeBegin = 0;
  int[] shapeSX;
  int[] shapeDX;

  
  static final boolean isBlock(byte[] data, int width, int height, int h, int w)
  {
    byte d = data[h*(width+1)*4+w*4];
    if(d == 0)
      return true;
    else return false;
  }
  
  static final boolean isNode(byte[] data, int width, int height, int h, int w)
  {
    if(data[h*(width+1)*4+w*4] == 0)
      return false;
    if(h<=0 || h>=height || w<=0 || w>=width)
      return true;
    if(
      //left
      isBlock(data, width, height, h, w-1) ||
      //right
      isBlock(data, width, height, h, w+1) ||
      //up
      isBlock(data, width, height, h-1, w) ||
      //down
      isBlock(data, width, height, h+1, w)
    )
      return true;
    else
      return false;
  }
  
  public void buildShape(BufferedImage image, int width, int height)
  {  
    DataBufferByte buffer = (DataBufferByte) image.getRaster().getDataBuffer();
    byte[] data = buffer.getData();

    shapeSX = new int[height];
    shapeDX = new int[height];
    for(int i=0;i<shapeSX.length;i++)
      shapeSX[i] = -1;
    for(int i=0;i<shapeDX.length;i++)
      shapeDX[i] = -1;
    
    for(int h=0;h<height;h++)
    {
      for(int w=0;w<width;w++)
      {//data[h*width*4+w*4]
        if(isNode(data, width-1, height-1, h, w))
        {
          if(shapeSX[h] == -1)
          {
            shapeSX[h] = w;
            shapeDX[h] = w;
          }
          else
          {
            if(w < shapeSX[h])
              shapeSX[h] = w;
            else if(w > shapeDX[h])
              shapeDX[h] = w;
          }
          System.out.print("o");
        }
        else
        {
          System.out.print("_");
        }
      }
      System.out.println();
    }
//    for(int i=0;i<height;i++)
//    {
////      for(int j=0;j<shapeSX[i];j++)
////        System.out.print("_");
//      System.out.println(shapeSX[i]+","+shapeDX[i]);
//    }
  }

  
  public static void main(String[] args) {
    BufferedImage image2 = ImageLoader.loadImage("rogue.png");
    new ShapedWidget().buildShape(image2, 70, 124);
    //System.out.println(image2.getWidth());
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.