Friday 6 July 2012

Move Object on Android Canvas 2

2:- Create Class for Object which move:-

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Picture;
import android.graphics.Point;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.widget.Button;


public class Droid {
    private static int i=-1;
private Bitmap bitmap;
private Bitmap bitmap1;// the actual bitmap
private Bitmap bitmap2;
private Bitmap bitmap3;// the actual bitmap
private int x; // the X coordinate
private int y; // the Y coordinate
private boolean touched; // if droid is touched/picked up
private Speed speed; // the speed with its directions
private  Point[] po ;
public Droid(Bitmap bitmap,Bitmap bitmap1,Bitmap bitmap2,Bitmap bitmap3, int x, int y) {
this.bitmap = bitmap;
this.bitmap1=bitmap1;
this.bitmap2=bitmap2;
this.bitmap3=bitmap3;
this.x = x;
this.y = y;
this.speed = new Speed();
Point p=new Point();
p.set(x/2-50,y/2-30);
getNPointsOnCircle(p,x*30/100,200);
}

public Bitmap getBitmap() {
return bitmap;
}
public void setBitmap(Bitmap bitmap) {
this.bitmap = bitmap;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}


public boolean isTouched() {
return touched;
}


public void setTouched(boolean touched) {
this.touched = touched;
}

public Speed getSpeed() {
return speed;
}


public void setSpeed(Speed speed) {
this.speed = speed;
}


public void draw(Canvas canvas,int h,int w) {
   canvas.setDensity(120);
   Rect r = new Rect();
   r.left=w/2-w*25/100;
   r.right=w/2+w*25/100;
   r.top=h*5/100;
   r.bottom=r.top+w*50/100;
if(i%2==0)
   canvas.drawBitmap(bitmap2, null, r,null);
else
canvas.drawBitmap(bitmap3,null,r,null);
r.top=h*10/100;
r.bottom=h*65/100;
r.left=w*20/100;
r.right=w*80/100;
canvas.drawBitmap(bitmap1, null, r, null);
canvas.drawBitmap(bitmap, po[i].x, po[i].y, null);

}


public void getNPointsOnCircle( Point p, int r, int n )   {


   double Number = Math.PI * 2 / n;
  po= new Point[n];
            
   int i = -1;
   while( ++i < n )
   {
       double theta = Number * i;
       Point pointOnCircle = new Point( );
       pointOnCircle.x=(int) (Math.cos( theta ) * r)+p.x;
       pointOnCircle.y=(int) (Math.sin( theta ) * r)+p.y;
       System.out.println("X:"+ pointOnCircle.x+ " and Y:"+ pointOnCircle.y);
       po[ i ] = pointOnCircle ;
   }
   
 //  return points;
   
   } 
/**
* Method which updates the droid's internal state every tick
*/
public void update() {
if (!touched) {
//x += (speed.getXv() * speed.getxDirection()); 
//y += (speed.getYv() * speed.getyDirection());
if(i<195)
{
i=i+5;
}
else
i=0;
}
}


public void handleActionDown(int eventX, int eventY) {
if (eventX >= (x - bitmap.getWidth() / 2) && (eventX <= (x + bitmap.getWidth()/2))) {
if (eventY >= (y - bitmap.getHeight() / 2) && (y <= (y + bitmap.getHeight() / 2))) {
// droid touched
setTouched(true);
} else {
setTouched(false);
}
} else {
setTouched(false);
}


}
}
  

No comments:

Post a Comment