안드로이드 초보인데요.


그냥 화면에서 그림이 랜덤으로 왔다갔다거리는데요  (책에 있 는예제임)


버튼을 누르면 초시계가 시작되고 아이콘을 누르면 잡은 횟수가 증가하게 만들고 싶은데


버튼이 안만들어지는데 어떻게 만들어야하는건가요?..


xml파일을 쓰지 않아서 어디에 만드는지 모르겠어요 ㅠㅠ


package com.android.surfaceviewdemoa2;


import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.widget.Button;

import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.Rect;

import android.view.MotionEvent;

import android.view.SurfaceHolder;

import android.view.SurfaceView;


public class Main extends Activity {

 //   Button Btnstart;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(new MySurfaceView(this));

 //       Btnstart= (Button)findViewById(R.id.Btnstart); 



    }

}


class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback,

         Runnable {

private SurfaceHolder holder;

private Paint paint;

private Canvas canvas;

private Thread thread;

private boolean isRun;


MyIcon myIcon;

public MySurfaceView(Context context) {

super(context);

holder = getHolder();

holder.addCallback(this);

paint = new Paint();

paint.setAntiAlias(true);

}


      @Override

      public void surfaceChanged(SurfaceHolder holder, int format, int width, 

     int height){  

      }

      

      @Override

      public void surfaceCreated(SurfaceHolder arg0) {

     

     thread = new Thread(this);

     thread.start();

      }

      

      @Override

      public void surfaceDestroyed(SurfaceHolder arg0) {

     isRun = false;

      }

      

      @Override

      public boolean onTouchEvent(MotionEvent event) {

     int touchX = (int) event.getX();

     int touchY = (int) event.getY();

     

     if (event.getAction() == MotionEvent.ACTION_DOWN) {

     myIcon.isHit(touchX, touchY);

     }

     

     return true;

    }

      

      @Override

      public void run() {

     myIcon = new MyIcon();

     

     isRun = true;

     

     while (isRun) {

     canvas = holder.lockCanvas();

     

     fillBackground(Color.WHITE);

     

     myIcon.moveAuto(canvas);

     

     holder.unlockCanvasAndPost(canvas);

     

     try {

     Thread.sleep(600);

     } catch (InterruptedException e) {

     e.printStackTrace();

     }

     }

      }

      

      private void fillBackground(int bg) {

     paint.setColor(bg);

     paint.setStyle(Paint.Style.FILL);

     canvas.drawRect(new Rect(0, 0, getWidth(), getHeight()), paint);

      }

      

      

      class MyIcon {

     

     private static final int LEFT = 0;

     private static final int UP = 1;

     private static final int RIGHT = 2;

     private static final int DOWN = 3;

     

     private Bitmap icon;

          private Bitmap icon2;

          private int androidWidth;

          private int androidHeight;

          

          int x = 0;

          int y = 0;

          

          int maxX;

          int maxY;

          

          int speed = 150;

          

          boolean isHit = false;

          

          MyIcon() {

         

         icon = BitmapFactory

         .decodeResource(getResources(), R.drawable.img1);

         icon2 = BitmapFactory.decodeResource(getResources(),

         R.drawable.img2);

         

         androidWidth = icon.getWidth();

         androidHeight = icon.getHeight();

         

         maxX = getWidth() - androidWidth;

         maxY = getHeight() - androidHeight;

          }

          

          MyIcon(int speed) {

         this();

         this.speed = speed;

          }

          

          public void isHit(int touchX, int touchY) {

         if (touchX >= x && touchX <= x + androidWidth && touchY >= y

         && touchY <= y + androidHeight) {

         isHit = true;

         }

          }

          

          public void setNextPosition() {

         int direction = (int) (Math.random() * 4);

         

         if(direction == LEFT) {

         

         x -= speed;

         }

         

         if(direction == RIGHT) {

         

         x += speed;

         }

         

         if(direction == UP) {

           

         y -= speed;

         }

         

         if(direction == DOWN) {

           

         y += speed;

         }

         

         if(x<0) {

         x = 0;

         }

         

         if(x > maxX) {

         x = maxX;

         }

         

         if(y < 0) {

         y = 0;

         }

         

         if(y > maxY) {

         y = maxY;

         }

          }

          

          public void moveAuto(Canvas canvas) {

         setNextPosition();

         

         if(isHit) {

         

         canvas.drawBitmap(icon2, x, y, paint);

         isHit = false;

         

         }else{

         canvas.drawBitmap(icon, x, y, paint);

         }

         

          }

         

      }

}