Graphic2D.java :  » Game » douball » tw » edu » ntu » csie » r403 » douball » android » Android Open Source

Android Open Source » Game » douball 
douball » tw » edu » ntu » csie » r403 » douball » android » Graphic2D.java
package tw.edu.ntu.csie.r403.douball.android;

import java.util.ArrayList;

import tw.edu.ntu.csie.r403.MyMap;
import tw.edu.ntu.csie.r403.MyRect;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;

public class Graphic2D 
{
  private static Bitmap texture1 = null;
  //private static int TEXTURE_W
  public static void init(Activity activity)
  {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inTargetDensity = 1; 
    options.inDensity = 1;
    
    texture1 = BitmapFactory.decodeResource(activity.getResources(), R.drawable.texture4, options);
  }
  public static Bitmap createMap(MyMap map)
  {
    Paint paint = new Paint();
    Bitmap bitmap = Bitmap.createBitmap(map.getWidth(), map.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    ArrayList<MyRect> roads = map.getRoads();
    for(int i=0; i<roads.size(); i++)
    {
      
      MyRect rect = roads.get(i);
      int x = ( rect.rightBottom.getX() - rect.leftTop.getX() ) / texture1.getWidth();  
      int y = ( rect.rightBottom.getY() - rect.leftTop.getY() ) / texture1.getHeight();
      
      int px = 0;
      int py = 0;
      for(int p=0; p<x; p++)
      {
        for(int q=0; q<y; q++)
        {
          px = rect.leftTop.getX() + p * texture1.getWidth();
          py = rect.leftTop.getY() + q * texture1.getHeight();
          Rect target = new Rect(px, py, px + texture1.getWidth(), py + texture1.getHeight());
          canvas.drawBitmap(texture1, null, target, paint);
        }
      }
    }
    return bitmap;
  }

}
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.