Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7529363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:38:33+00:00 2026-05-30T04:38:33+00:00

Im trying to learn Android graphics & event handeling for a multiplayer game. As

  • 0

Im trying to learn Android graphics & event handeling for a multiplayer game.

As a building block Im developing some code that will draw path of points based on touch/motion events, however Im unable to draw, nothing gets drawn on the Emulator

I have 3 simple classes (MyPoint,GameCanvas,Game):

1) MyPoint class encapsulates x & y positions , draw method draws a point based on those positions

public class MyPoint  {

    private float x; 
    private float y; 
    Paint pWhite = new Paint(R.color.white);

    public MyPoint(float x, float y) {

           this.x = x;
           this.y = y;
    }//end const
    public void draw(Canvas canvas) {
        canvas.drawPoint(this.x, this.y, pWhite);
    }//end method
}//end MyPoint Class

2) GameCanvas is the View that will be drawn on , this class is responsible for
its on event handling by implementing OnTouchListener ,onThouch() method

    public class GameCanvas extends View implements OnTouchListener {

        //keep track on points created by touch events
        java.util.List<MyPoint> pointsList = null;

        public GameCanvas(Context context) {
            super(context);
            setFocusable(true);
            setFocusableInTouchMode(true);

            setOnTouchListener(this);
            pointsList = new java.util.Stack();

        }//end const 

//onDraw iterates through points that were added during motion/touch events and calls the MyPoint.draw(canvas) 

        @Override
        protected void onDraw(Canvas canvas) {

            MyPoint p = null;
            for (int i = 0; i < pointsList.size(); i++) {
                p = (MyPoint) pointsList.remove(i);
                p.draw(canvas);
            }

        }//end onDraw

        //implemented in order to handel touch events 
        public boolean onTouch(View arg0, MotionEvent event) {
            int action = event.getAction();
            float currentX = event.getX();
            float currentY = event.getY();

            //log x and y coordinates
            Log.v(this.getClass().getName().toString(), "X=" + currentX);
            //log x and y coordinates
            Log.v(this.getClass().getName().toString(), "Y=" + currentY);

            if (action == MotionEvent.ACTION_DOWN) {

                // log action down 
                Log.v(this.getClass().getName().toString(), "MotionEvent = ACTION_DOWN");

                pointsList.add(new MyPoint(currentX, currentY));
                this.invalidate();
            } else if (action == MotionEvent.ACTION_MOVE) {

                //log action move 
                Log.v(this.getClass().getName().toString(), "MotionEvent = ACTION_MOVE");

                pointsList.add(new MyPoint(currentX, currentY));
                this.invalidate();
            } else if (action == MotionEvent.ACTION_UP) {

                //log action move 
                Log.v(this.getClass().getName().toString(), "MotionEvent = ACTION_UP");

                pointsList.add(new MyPoint(currentX, currentY));
                this.invalidate();
            }
            //call invalidate in order to call trigger onDraw()

            return true;
        }//end onTouch() 
    }//end GameCanvas class

3) Game class is the Activity launches GameCanvas View

public class Game extends Activity {

GameCanvas newGameCanvas = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        //create GameCanvas object 
        newGameCanvas = new GameCanvas(this);
        super.onCreate(savedInstanceState);
        setContentView(newGameCanvas);
        //
        newGameCanvas.requestFocus();

    }//end OnCreate 
}//end Game class
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T04:38:35+00:00Added an answer on May 30, 2026 at 4:38 am

    Here is the actual working code. I only changed a few lines at most & also made the View a inner class of the Activity, so I dont have to look at different screens when testing/debugging.

    1. First change was to make View inner class , no change in logic
      just for convenience
    2. I set the canvas background color to white,
      in the onCreate()
    3. I changed my logic in onDraw() from
      pointsQ.removw(i), to just pointsQ.get(i), with remove(i) only the
      last point was being drawn
    4. Finally you get a canvas that you can
      use to draw/trace based on touch events

      public class Game extends Activity {
      
      private gameView newGameView = null;
      private MyPoint newMyPoint = null;
      
      private java.util.List<MyPoint> pointsQ = new ArrayList<MyPoint>();
      
      @Override
      public void onCreate(Bundle savedInstanceState) {
          try {
      
              newGameView = new gameView(this);
              newGameView.setBackgroundColor(Color.WHITE);
              super.onCreate(savedInstanceState);
              setContentView(newGameView);
              //initially requestFocus
              newGameView.requestFocus();
      
      
          } catch (NullPointerException exc) {
              Log.d(this.getClass().getName().toString(), exc.getMessage());
          }
      }
      
      //inner class that represents View 
      public class gameView extends View {
      
      
      private Paint wPaint = new Paint(Color.BLACK);
      
      
      public gameView(Context context) {
          super(context);
          setFocusable(true); 
          setFocusableInTouchMode(true); 
          //
          wPaint.setStyle(Paint.Style.FILL);
          wPaint.setStrokeWidth(3);
      
      }
      
      //implemented in order to handle touch events 
      @Override
      public boolean onTouchEvent(MotionEvent event) {
          int action = event.getAction();
          float currentX = event.getX();
          float currentY = event.getY();
      
          //log x and y coordinates
          Log.d(this.getClass().getName().toString(), "X=" + currentX);
          //log x and y coordinates
          Log.d(this.getClass().getName().toString(), "Y=" + currentY);
      
          if (action == MotionEvent.ACTION_DOWN) {
      
              // log action down 
              Log.d(this.getClass().getName().toString(), "MotionEvent = ACTION_DOWN");
      
              newMyPoint = new MyPoint(currentX, currentY);
              pointsQ.add(newMyPoint);
      
      
          } else if (action == MotionEvent.ACTION_MOVE) {
      
              //log action move 
              Log.d(this.getClass().getName().toString(), "MotionEvent = ACTION_MOVE");
      
              newMyPoint = new MyPoint(currentX, currentY);
              pointsQ.add(newMyPoint);
      
          } else if (action == MotionEvent.ACTION_UP) {
      
              //log action move 
              Log.d(this.getClass().getName().toString(), "MotionEvent = ACTION_UP");
      
              newMyPoint = new MyPoint(currentX, currentY);
              pointsQ.add(newMyPoint);
      
      
          }
      
          //I moved the invalidate to the end of the method se=ince all action called it anyways 
          this.invalidate(); 
          return true;
      }//end onTouch() 
      
      @Override
      protected void onDraw(Canvas canvas) {
          MyPoint p = null;
          for (int i = 0; i < pointsQ.size(); i++) {
              //get MyPoint & draw based on its x,y attributes 
              p = (MyPoint) pointsQ.get(i);
                     //
              canvas.drawCircle(p.getX(),p.getY(),2,wPaint);
              //
              Log.d(this.getClass().getName().toString(), "onDraw X= "+p.getX()+" Y= "+p.getY());    
              }
      
          } 
      }//end inner class gameView
      }//end Game activity class
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to read Android source code to learn about binder, but I
I'm writing a puzzle game to learn developing Android Apps. But now I'm stuck
I am trying to learn android so i am making some apps to play
I'm trying to learn some Android and java programming in Eclipse. I can run
I am trying to learn android app development .I am using IntelliJ Idea 10.When
I'm trying to learn how to build apps for Android. The first simple app,
I am trying to learn about Google maps API. I have a project that
I just started to learn Android, I'm trying to write a widget which is
I'm trying to learn how to make an animated sprite in android and couldn't
I am an android developer and now I am trying to learn Mango too.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.