package com.orbekk;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class GameView extends SurfaceView implements SurfaceHolder.Callback {
    private Logger logger = LoggerFactory.getLogger(getClass());
    private GameThread thread;
    
    static class GameThread extends Thread {
        private int height = 0;
        private int width = 0;
        private int posX;
        private int posY;
        private SurfaceHolder holder;
        private Context context;
        private Paint paint;
        
        public GameThread(SurfaceHolder holder, Context context) {
            this.holder = holder;
            this.context = context;
            paint = new Paint();
            paint.setAntiAlias(true);
            paint.setARGB(255, 255, 0, 0);
        }
        
        public void setSize(int width, int height) {
            synchronized(holder) {
                this.width = width;
                this.height = height;
            }
        }
        
        private void doDraw(Canvas c) {
            c.drawCircle(100.0f, 50.0f, 20.0f, paint);
        }
        
        @Override public void run() {
            Canvas c = null;
            try {
                c = holder.lockCanvas();
                synchronized(holder) {
                    doDraw(c);
                }
            } finally {
                holder.unlockCanvasAndPost(c);
            }
        }
    }
    
    public GameView(Context context) {
        super(context);
        getHolder().addCallback(this);
        thread = new GameThread(getHolder(), context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Paint paint = new Paint();
        paint.setARGB(255, 255, 0, 0);
        canvas.drawCircle(50.0f, 50.0f, 50.0f, paint);
    }
    
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        logger.info("SurfaceChanged(w={}, h={})", width, height);
        thread.setSize(width, height);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        logger.info("SurfaceCreated()");
        thread.start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        logger.info("SurfaceDestroyed()");
        // TODO: Stop thread.
    }

}

@@ -167,16 +167,49 @@
 .Paint;%0A
+import android.view.MotionEvent;%0A
 import a
@@ -231,24 +231,24 @@
 faceHolder;%0A
-
 import andro
@@ -568,26 +568,28 @@
     private 
-in
+floa
 t posX;%0A    
@@ -600,18 +600,20 @@
 private 
-in
+floa
 t posY;%0A
@@ -675,32 +675,66 @@
 ontext context;%0A
+        private Paint background;%0A
         private 
@@ -884,32 +884,80 @@
 text = context;%0A
+            posX = 100;%0A            posY = 100;%0A
             pain
@@ -1046,32 +1046,116 @@
 55, 255, 0, 0);%0A
+            background = new Paint();%0A            background.setARGB(255, 0, 0, 0);%0A
         %7D%0A      
@@ -1414,28 +1414,98 @@
 draw
-Circle(100.0f, 50.0f
+Rect(0.0f, 0.0f, width+1.0f, height+1.0f, background);%0A            c.drawCircle(posX, posY
 , 20
@@ -1841,24 +1841,159 @@
 %7D%0A        %7D%0A
+        %0A        private void setPosition(float x, float y) %7B%0A            posX = x;%0A            posY = y;%0A            run();%0A        %7D%0A
     %7D%0A    %0A 
@@ -2827,32 +2827,32 @@
 eDestroyed()%22);%0A
-
         // TODO:
@@ -2870,12 +2870,155 @@
 d.%0A    %7D
+%0A    %0A    @Override%0A    public boolean onTouchEvent(MotionEvent e) %7B%0A        thread.setPosition(e.getX(), e.getY());%0A        return true;%0A    %7D
 %0A%0A%7D%0A

Implement circle location and controls.