Results 1 to 2 of 2
  1. #1

    Unhappy Please Help......Can't Resolve error during Android Programming

    Hello friends,
    I am developing one android application on Android table 3.2.In that I have to use Buttons and virtual joystick.I successfully added Buttons and Joystick.But problem is In joystick.now I will describe about joystick ...In surfaceview I drawn one circle and at the mid of that circle I took one small solid circle...this solid circle should move till the outer circle boundary when we touch it...but this solid circle is not moving...I don't know what is the problem...please help me...I know problem is with thread and surfaceview...please help me..I am Android biggener...I am pasting my code..
    1.Surfacetestactivity(Starting activity)
    2.MySurfaceView
    3.GameControls
    4.GameThread
    5.main.xml

    Code:
    *********************Surfacetestactivity(Starting activity)*******************************
    
    package hitech.droid.dinesh;
    import android.app.Activity;
    import android.os.Bundle;
    public class SurfaceTestActivity extends Activity {
     	public MySurfaceView btn_joystick;
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
              setContentView(R.layout.main); 
            btn_joystick=(MySurfaceView) findViewById(R.id.btn_joystick);
            }
      }
    
    ****************2.MySurfaceView****************************************************
    
    package hitech.droid.dinesh;
    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.os.Handler;
    import android.util.AttributeSet;
    import android.util.Log;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    
    public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback
    {
    	private Context _context;               
    	private GameControls _control;        				//instance of GameControls class 
    	private GameThread thread;			  				//instance of GameThread class 	
        private Bitmap joystickicon;
    	private Paint paint_circle = new Paint(Paint.ANTI_ALIAS_FLAG);
    
      public MySurfaceView(Context context) {
    	super(context);
    	_context = context;
    	init();
       }
      
    	public MySurfaceView(Context context, AttributeSet attrs)
    	{
    		super(context, attrs);
    		_context = context;
    		init();
    		
    	}
    	
    	public MySurfaceView(Context context, AttributeSet attrs, int defStyle) {
    		super(context, attrs, defStyle);
    		_context = context;
    		init();
    		
    	}
    
      private void init()
      {
    	SurfaceHolder holder = getHolder();//get the holder which provides control over the SurfaceView
    	holder.addCallback( this);//add CallBack interface to this holder
        
    	thread = new GameThread(holder, _context, new Handler(),this);
    	setFocusable(true);
    
        joystickicon = (Bitmap)BitmapFactory.decodeResource(getResources(), R.drawable.icon); 
        
        paint_circle.setStyle(Paint.Style.STROKE);//Set the Stroke
        paint_circle.setStrokeWidth(1);//set Stroke Width
        paint_circle.setTextSize(15);//set circle width
        paint_circle.setColor(Color.RED);//set circle color
        _control=new GameControls();
        setOnTouchListener(_control);//Set OnClickListener on _control when we click on SurFaceView
     }
    
    protected void onDraw(Canvas canvas) 
    {
    
    	canvas.drawCircle(180,180,30, paint_circle);
    	canvas.drawCircle(180,180,60, paint_circle);
    	canvas.drawCircle(180,180,90, paint_circle);
    	canvas.drawCircle(180,180,120, paint_circle);
    	canvas.drawBitmap(joystickicon,130,130,null);
    
    	if(_control._touchingPoint.x>950 && _control._touchingPoint.x<1200)
    	{
    		if(_control._touchingPoint.y>350 && _control._touchingPoint.y<650)
    		{
    			
    			canvas.drawBitmap(joystickicon,_control._touchingPoint.x-26,_control._touchingPoint.y-26,null);
    		
            }
    	}
    
    }
    
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) 
    {
    
    }
    
    //When surface Destroyed
    private boolean retry;
    public void surfaceDestroyed(SurfaceHolder arg0) 
    {
    	retry = true;
    	//code to end gameloop
    	thread.state = GameThread.STOPED;
    	while (retry) 
    	{
    		try 
    		{
    			//code to kill Thread
    			thread.join();
    			retry = false;
    		} 
    		catch (InterruptedException e)
    		{
    		}
    	}
    
    }
    
    //When Surface First time created
    public void surfaceCreated(SurfaceHolder holder)
    {
    	//When thread paused
    	if(thread.state==GameThread.PAUSED)
    	{
    		//When game(application) is opened again in the Android OS
    		thread = new GameThread(getHolder(), _context, new Handler(),this);
    		thread.start();
    	}else{
    		//creating the game(application) Thread for the first time
    		thread.start();
    	}
    }
    
    public void Update() {
    	// TODO Auto-generated method stub
    
    }
    }
    
    
    *****************************3.GameControls*************************************
    
    package hitech.droid.dinesh;
    import android.graphics.Point;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.View.OnTouchListener;
    public class GameControls implements OnTouchListener{
    
    	public float initx = 1100;
    	public float inity = 500;
    	public Point _touchingPoint = new Point(1100,500);//Co-ordinate for _touchingPoint
    	private Boolean _dragging = false;               //flag for detecting _dragging
    
    	public boolean onTouch(View v, MotionEvent event) 
    	{
    		update(event);
    		return true;
    	}
    	private MotionEvent lastEvent;
    	public void update(MotionEvent event){
    
    		if (event == null && lastEvent == null)
    		{
    			return;
    		}else if(event == null && lastEvent != null){
    			event = lastEvent;
    		}else{
    			lastEvent = event;
    		}
    		if ( event.getAction() == MotionEvent.ACTION_DOWN)
    		{
    			_dragging = true;
    		}else if ( event.getAction() == MotionEvent.ACTION_UP){
    			_dragging = false;
    		}
    		if ( _dragging){
    		 
    			_touchingPoint.x = (int)event.getX();
    			_touchingPoint.y = (int)event.getY();
    		}else if (!_dragging)
    		{
    			_touchingPoint.x = (int) initx;
    			_touchingPoint.y = (int) inity;		}
    	}
    }
    
    ******************************4.GameThread****************************************
    
    package hitech.droid.dinesh;
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.os.Handler;
    import android.util.Log;
    import android.view.SurfaceHolder;
    
    public class GameThread extends Thread 
    {
    
    	private SurfaceHolder mSurfaceHolder;
    	private Handler mHandler;
    	private Context mContext;
    	private Paint mLinePaint;
    	private Paint blackPaint;
    
    	private long sleepTime;
    	private long delay=70;
           int state = 1;
    	public final static int RUNNING = 1;
    	public final static int PAUSED = 2;
    	public final static int STOPED = 3;
    	MySurfaceView gEngine;
    
    	public GameThread(SurfaceHolder surfaceHolder, Context context, Handler handler,MySurfaceView gEngineS){
    		mSurfaceHolder = surfaceHolder;
    		mHandler = handler;
    		mContext = context;
    		gEngine=gEngineS;
    	}
    
    	private long beforeTime;
    	public void run() {
    		while (state==RUNNING) {
    			Log.d("State","Thread is runnig");
    			//time before update
    			beforeTime = System.nanoTime();
    			//This is where we update the game engine
    			gEngine.Update();
    
    			//DRAW
    			Canvas c = null;
    			try {
    				//lock canvas so nothing else can use it
    				c = mSurfaceHolder.lockCanvas(null);
    				synchronized (mSurfaceHolder) {
    					//clear the screen with the white painter.
    					//reset the canvas
    					c.drawColor(Color.WHITE);
    					//This is where we draw the game engine.
    					gEngine.onDraw(c);
    				}
    			} finally {
    				// do this in a finally so that if an exception is thrown
    				// during the above, we don't leave the Surface in an
    				// inconsistent state
    				if (c != null) {
    					mSurfaceHolder.unlockCanvasAndPost(c);
    				}
    			}
    
    			try {
    				//actual sleep code
    				if(sleepTime>0){
    					this.sleep(sleepTime);
    				}
    			} catch (InterruptedException ex) {
    				Logger.getLogger(GameThread.class.getName()).log(Level.SEVERE, null, ex);
    			}
    			
    			//When thread paused
    			while (state==PAUSED){
    				Log.d("State","Thread is pausing");
    				try {
    					this.sleep(1000);
    				} catch (InterruptedException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    			}
    		}
    	}}
    
    ***********************************5.Main.xml*****************************************
    
    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:background="@color/white">
    <AbsoluteLayout
        
        android:id="@+id/ocu"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal" >
    
        
        <TextView
            android:id="@+id/textview_status"
            android:layout_width="500dp"
            android:layout_height="50dp"
            android:layout_x="390dp"
            android:layout_y="600dp"
            android:gravity="center"
            android:textSize="15sp"
            android:textStyle="bold"
            android:typeface="sans" />
    
        <ToggleButton
            android:id="@+id/toggle_estop"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_x="75dp"
            android:layout_y="50dp"
            android:textOn="estop_on" 
            android:textOff="estop_off" 
            android:textStyle="bold"
            android:textSize="24sp"
            android:typeface="sans" />
        
      	<ToggleButton
            android:id="@+id/toggle_connect"
            android:layout_width="150dp"
            android:layout_height="50dp"
            android:layout_x="75dp"
            android:layout_y="250dp"
            android:textOn="connect"
            android:textOff="disconnect"        
            android:textStyle="bold"
            android:typeface="sans" />
    
       
        <ToggleButton
            android:id="@+id/toggle_taillight"
            android:layout_width="150dp"
            android:layout_height="50dp"
            android:layout_x="80dp"
            android:layout_y="484dp"
            android:textOff="tail_off"
            android:textOn="tail_on"
            android:textStyle="bold"
            android:typeface="sans" />
    
        <ToggleButton
            android:id="@+id/toggle_headlight"
            android:layout_width="150dp"
            android:layout_height="50dp"
            android:layout_x="75dp"
            android:layout_y="364dp"
            android:textOff="head_off"
            android:textOn="head_on"
            android:textStyle="bold"
            android:typeface="sans" />
    
         <hitech.droid.dinesh.MySurfaceView
             android:id="@+id/btn_joystick"
             android:layout_width="350dp"
             android:layout_height="340dp"
             android:layout_x="923dp"
             android:layout_y="336dp"
             android:background="@color/green" />  
     </AbsoluteLayout>  
     
    </FrameLayout>
    Last edited by dinalhat; 08-04-2012 at 01:37 AM. Reason: Device was not mention

  2. #2



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •