Skip to content

Instantly share code, notes, and snippets.

Created October 12, 2010 03:32
Show Gist options
  • Select an option

  • Save anonymous/621625 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/621625 to your computer and use it in GitHub Desktop.
package com.zeroeightysix.android.bluegreen;
import java.util.ArrayList;
//import com.zeroeightysix.android.bluegreen.Point.Coordinates;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class DrawView extends SurfaceView implements SurfaceHolder.Callback {
ArrayList<Point> points = new ArrayList<Point>();
Paint paint = new Paint();
private DrawViewThread _thread;
//Interaction is handled by the DrawView class
public DrawView(Context context) {
super(context);
getHolder().addCallback(this);//add DrawView to SurfaceHolder
_thread = new DrawViewThread(getHolder(), this);
setFocusable(true);
//setFocusableInTouchMode(true);
//this.setOnTouchListener(this);
paint.setColor(Color.WHITE);
paint.setAntiAlias(true);
}
@Override
public void onDraw(Canvas canvas) {
for (Point point : points) {
if(point.x <150){
paint.setColor(Color.BLUE);
}
else if(point.x <300 && point.x > 150){
paint.setColor(Color.GREEN);
}
canvas.drawCircle(point.x, point.y, 5, paint);
}
}
public boolean onTouchEvent(MotionEvent event){
//Synchronization with the SurfaceHolder to avoid changing
//the number in the list while reading the list
synchronized(_thread.getSurfaceHolder()){
Point point = new Point();
point.x = event.getX();
point.y = event.getY();
invalidate();
return points.add(point);
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
_thread.setRunning(true);
_thread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
_thread.setRunning(false);
while(retry){
try {
_thread.join();
retry = false;
} catch (InterruptedException e) {
// keep trying to join
}
}
}
public void UpdatePhysics(){
Point.Speed speed;
for(Point point : points){
speed = point.getSpeed();
//X and Y DIRECTION//
if(speed.getXDirection() == Point.Speed.X_DIRECTION_RIGHT){
point.x = point.x + point.getSpeed().getX();
}else{
point.x = point.x - point.getSpeed().getX();
}
if(speed.getYDirection() == Point.Speed.Y_DIRECTION_UP){
point.y = point.y + point.getSpeed().getY();
}else{
point.y = point.y + point.getSpeed().getY();
}
//BOARDERS//
if(point.x > getWidth()){
speed.toggleXDirection();
}else if(point.x < 0){
speed.toggleXDirection();
}
if(point.y > getHeight()){
speed.toggleYDirection();
}else if(point.y < 0){
speed.toggleYDirection();
}
}
}
//Thread to control drawings
class DrawViewThread extends Thread {
private SurfaceHolder _surfaceHolder;
private DrawView _drawView;
private boolean _run = false;
public SurfaceHolder getSurfaceHolder(){
return _surfaceHolder;
}
public DrawViewThread(SurfaceHolder surfaceHolder, DrawView drawView) {
_surfaceHolder = surfaceHolder;
_drawView = drawView;
}
public void setRunning(boolean run) {
_run = run;
}
@Override
public void run() {
Canvas c;
while (_run) {
c = null;
try {
c = _surfaceHolder.lockCanvas(null);
synchronized (_surfaceHolder) {
_drawView.UpdatePhysics();
_drawView.onDraw(c);
}
} finally {
// do this in a finally so that if an exception is thrown
// during the above, it doesn't leave the Surface in an
// inconsistent state
if (c != null) {
_surfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
}
}
package com.zeroeightysix.android.bluegreen;
public class Point {
float x, y;
@Override
public String toString() {
return x + ", " + y;
}
/*
* SPEED CLASS
*/
public class Speed{
public static final int X_DIRECTION_LEFT = -1;
public static final int X_DIRECTION_RIGHT = 1;
public static final int Y_DIRECTION_UP = 1;
public static final int Y_DIRECTION_DOWN = -1;
private int _x = 1;
private int _y = 1;
private int _xDirection = X_DIRECTION_RIGHT;
private int _yDirection = Y_DIRECTION_DOWN;
public int getXDirection(){
return _xDirection;
}
public int getYDirection(){
return _yDirection;
}
//set direction
public void setXDirection(int direction){
_xDirection = direction;
}
public void setYDirection(int direction){
_yDirection = direction;
}
//toggle direction
public void toggleXDirection(){
if(_xDirection == X_DIRECTION_RIGHT){
_xDirection = X_DIRECTION_LEFT;
}else{
_xDirection = X_DIRECTION_RIGHT;
}
}
public void toggleYDirection(){
if(_yDirection == Y_DIRECTION_DOWN){
_yDirection = Y_DIRECTION_UP;
}else{
_yDirection = Y_DIRECTION_DOWN;
}
}
//getters
public int getX(){
return _x;
}
public int getY(){
return _y;
}
//setters
public void setX(int speed){
_x = speed;
}
public void setY(int speed){
_y = speed;
}
public String toString(){
String xDirection;
String yDirection;
if (_yDirection == Y_DIRECTION_UP){
yDirection = "up";
}else{
yDirection = "down";
}
if (_xDirection == X_DIRECTION_RIGHT){
xDirection = "right";
}else{
xDirection = "left";
}
//Speed x: 5 | 6 xDirection: left
return "Speed x: "+_x+" | y: "+_y+"| xDirection: "+xDirection+" | yDirection: "+yDirection;
}
}
/*
* Getters and Setters for Speed
*/
private Speed _speed;
public Point(){
_speed = new Speed();
}
public Speed getSpeed(){
return _speed;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment