Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: problem with @Override in onDraw

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default problem with @Override in onDraw

    I can not figure out why I am getting a "The method OnDraw(Canvas) of type DrawStuffActivity.MyView must override or implement a supertype method" error, any help would be greatly appreciated



    import android.app.Activity;
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Rect;
    import android.os.Bundle;
    import android.util.DisplayMetrics;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.View.OnTouchListener;
     
    public class DrawStuffActivity extends Activity implements OnTouchListener
    {
    	MyView myView;
    	int numberOfFingers = 0; 
    	float oldX[] = new float[2],
    			oldY[] = new float[2];
    	Rect rectangle = new Rect(0, 0, 100, 100);
    	DisplayMetrics metrics = new DisplayMetrics();
     
    	@Override
    	protected void onCreate(Bundle savedInstanceState) 
    	{
    		super.onCreate(savedInstanceState);
     
    		myView = new MyView(this);
    		setContentView(myView);
    		myView.setOnTouchListener(this);
     
    		getWindowManager().getDefaultDisplay().getMetrics(metrics);
    	}
     
     
    	@Override
    	public boolean onTouch(View view, MotionEvent event) 
    	{
    		switch (event.getActionMasked() )
    		{
    		case MotionEvent.ACTION_DOWN:
    			numberOfFingers = 1;
    			oldX[0] = event.getX(0);
    			oldY[0] = event.getY(0);
    			break;
    		case MotionEvent.ACTION_POINTER_DOWN:
    			numberOfFingers = 2;
    			oldX[1] = event.getX(1);
    			oldY[1] = event.getY(1);
    			break; 
    		case MotionEvent.ACTION_MOVE:
    			handleMove(event);
    			break;
    		case MotionEvent.ACTION_POINTER_UP:
    		case MotionEvent.ACTION_UP:
    			numberOfFingers--;
    			break;
    		}
     
    		view.invalidate();
    		return true;
    	}
     
     
    	float newX[] = new float[2], newY[] = new float[2];
    	int xChange[] = new int[2], yChange[] = new int[2];
     
    	int diffX, diffY;
    	int newLeft = rectangle.left, 
    			newTop = rectangle.top, 
    			newRight = rectangle.right,
    			newBottom = rectangle.bottom;
     
     
     
     
    	void handleMove(MotionEvent event)
    	{
    		newX[0] = Math.round(event.getX(0) );
    		newY[0] = Math.round(event.getY(0) );
    		xChange[0] = Math.round(newX[0] - oldX[0] );
    		yChange[0] = Math.round(newY[0] - oldY[0] );
    		oldX[0] = newX[0];
    		oldY[0] = newY[0];
     
    		switch (numberOfFingers)
    		{
    		case 1:
    			newLeft = rectangle.left + xChange[0];
    			newTop = rectangle.top + yChange[0];
    			newRight = rectangle.right + xChange[0];
    			newBottom = rectangle.bottom + yChange[0];
     
    			if (newLeft < 0 || newRight > metrics.widthPixels)
    			{
    				newLeft = rectangle.left;
    				newRight = rectangle.right;
    			}
    			if (newTop > 0 || newTop > metrics.heightPixels)
    			{
    				newTop = rectangle.top;
    				newBottom = rectangle.bottom;
    			}
     
    			rectangle = new Rect(newLeft, newTop, newRight, newBottom);
     
    			break;
     
    		case 2:
    			newX[1] = Math.round(event.getX(1) );
    			newY[1] = Math.round(event.getY(1) );
     
    			diffX = Math.abs(Math.round(newX[1] - newX[0])) - Math.abs(Math.round(oldX[1] - oldX[0]));
    			diffY = Math.abs(Math.round(newY[1] - newY[0])) - Math.abs(Math.round(oldY[1] - oldY[0]));
     
    			oldX[1] = newX[1];
    			oldY[1] = newY[1];
     
    			newLeft = rectangle.left - diffX / 2;
    			newTop = rectangle.top - diffY / 2;
    			newRight = rectangle.right - diffX / 2;
    			newBottom = rectangle.bottom - diffY / 2;
    			rectangle = new Rect(newLeft, newTop, newRight, newBottom);
     
    			break;
    		}
     
     
     
    	}
     
    	class MyView extends View
    	{
    		Paint whitePaint = new Paint();
     
    		MyView(Context context)
    		{
    			super(context);
    			whitePaint.setColor(Color.WHITE);
    		}
     
    		@Override
    		public void OnDraw(Canvas canvas) 
    		{
    			canvas.drawRect(rectangle, whitePaint);
    		}
    	}
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: problem with @Override in onDraw

    Check the API for the correct method signature - hint, java is case sensitive

Similar Threads

  1. Replies: 0
    Last Post: July 19th, 2012, 09:02 AM
  2. Classpath override not working correctly in command prompt?
    By MeteoricDragon in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 20th, 2011, 12:05 PM
  3. GUI error: is not abstract and does not override abstract method
    By djl1990 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 21st, 2011, 01:26 PM
  4. override list add method?
    By ober0330 in forum Collections and Generics
    Replies: 10
    Last Post: July 17th, 2011, 07:34 PM
  5. Override class method
    By mekie in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 31st, 2010, 07:06 PM