Im trying to learn Android graphics & event handeling for a multiplayer game.
As a building block Im developing some code that will draw path of points based on touch/motion events, however Im unable to draw, nothing gets drawn on the Emulator
I have 3 simple classes (MyPoint,GameCanvas,Game):
1) MyPoint class encapsulates x & y positions , draw method draws a point based on those positions
public class MyPoint {
private float x;
private float y;
Paint pWhite = new Paint(R.color.white);
public MyPoint(float x, float y) {
this.x = x;
this.y = y;
}//end const
public void draw(Canvas canvas) {
canvas.drawPoint(this.x, this.y, pWhite);
}//end method
}//end MyPoint Class
2) GameCanvas is the View that will be drawn on , this class is responsible for
its on event handling by implementing OnTouchListener ,onThouch() method
public class GameCanvas extends View implements OnTouchListener {
//keep track on points created by touch events
java.util.List<MyPoint> pointsList = null;
public GameCanvas(Context context) {
super(context);
setFocusable(true);
setFocusableInTouchMode(true);
setOnTouchListener(this);
pointsList = new java.util.Stack();
}//end const
//onDraw iterates through points that were added during motion/touch events and calls the MyPoint.draw(canvas)
@Override
protected void onDraw(Canvas canvas) {
MyPoint p = null;
for (int i = 0; i < pointsList.size(); i++) {
p = (MyPoint) pointsList.remove(i);
p.draw(canvas);
}
}//end onDraw
//implemented in order to handel touch events
public boolean onTouch(View arg0, MotionEvent event) {
int action = event.getAction();
float currentX = event.getX();
float currentY = event.getY();
//log x and y coordinates
Log.v(this.getClass().getName().toString(), "X=" + currentX);
//log x and y coordinates
Log.v(this.getClass().getName().toString(), "Y=" + currentY);
if (action == MotionEvent.ACTION_DOWN) {
// log action down
Log.v(this.getClass().getName().toString(), "MotionEvent = ACTION_DOWN");
pointsList.add(new MyPoint(currentX, currentY));
this.invalidate();
} else if (action == MotionEvent.ACTION_MOVE) {
//log action move
Log.v(this.getClass().getName().toString(), "MotionEvent = ACTION_MOVE");
pointsList.add(new MyPoint(currentX, currentY));
this.invalidate();
} else if (action == MotionEvent.ACTION_UP) {
//log action move
Log.v(this.getClass().getName().toString(), "MotionEvent = ACTION_UP");
pointsList.add(new MyPoint(currentX, currentY));
this.invalidate();
}
//call invalidate in order to call trigger onDraw()
return true;
}//end onTouch()
}//end GameCanvas class
3) Game class is the Activity launches GameCanvas View
public class Game extends Activity {
GameCanvas newGameCanvas = null;
@Override
public void onCreate(Bundle savedInstanceState) {
//create GameCanvas object
newGameCanvas = new GameCanvas(this);
super.onCreate(savedInstanceState);
setContentView(newGameCanvas);
//
newGameCanvas.requestFocus();
}//end OnCreate
}//end Game class
Here is the actual working code. I only changed a few lines at most & also made the View a inner class of the Activity, so I dont have to look at different screens when testing/debugging.
just for convenience
in the onCreate()
pointsQ.removw(i), to just pointsQ.get(i), with remove(i) only the
last point was being drawn
Finally you get a canvas that you can
use to draw/trace based on touch events