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 8368985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:31:44+00:00 2026-06-09T13:31:44+00:00

My problem is pretty simple, I guess. I want to draw a dot to

  • 0

My problem is pretty simple, I guess.

I want to draw a dot to the canvas as soon I touch it. The dot will be drawn to the position I touch. Next I want to draw another dot to the screen, but not on the position of the first dot. Means I want to prevent drawing an image over another image.

I tried to archieve that with the following code:

Activity Class

    public class Draw extends Activity {
    DrawView drawView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set full screen view
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                                         WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        drawView = new DrawView(this);
        setContentView(drawView);
        drawView.requestFocus();
    }
}

Canvas Class

    public class DrawView extends View implements OnTouchListener {
    private static final String TAG = "DrawView";

    List<Point> points = new ArrayList<Point>();
    Vector<Bitmap> bitmaps = new Vector<Bitmap>();
    Paint paint = new Paint();


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

        this.setOnTouchListener(this);
    }

    @Override
    public void onDraw(Canvas canvas) {
        canvas.drawColor(Color.WHITE);
        for (Point point : points) {
            canvas.drawBitmap(bitmaps.get(point.i), point.x, point.y, paint);
        }
    }

    public boolean onTouch(View view, MotionEvent event) {
        Log.d("Touch","Touch");
        Log.d("Points", "" + points.size());
        Log.d("Bitmaps", "" + bitmaps.size());

        int i = 0;
        for (Point point : points) {
            int height = bitmaps.get(i).getHeight();
            int width = bitmaps.get(i).getWidth();
            //Log.d("dimensions", height + ", " + width);

            for (int x = 0; x <= width; x++) {
                if (Math.floor(point.x + x) == Math.floor(event.getX())) {
                    points.get(i).isTaken = true;
                    Log.d("isTakenX", "" + point.isTaken);
                    break;
                }
                for (int y = 0; y <= height; y++) {
                    if (Math.floor(point.y + y) == Math.floor(event.getY())) {
                        points.get(i).isTaken = true;
                        Log.d("isTakenY", "" + point.isTaken);
                        break;
                    }
                }
            }
            i++;
        }
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            Log.d("EventDown","DOWN");
            boolean isTaken = false;
            for (Point point : points) {
                if (point.isTaken) {
                    isTaken = true;
                }
            }
            if (!isTaken) {
                bitmaps.add(BitmapFactory.decodeResource(getResources(), R.drawable.greenstar));
                Point point = new Point();
                point.x = event.getX();
                point.y = event.getY();
                point.i = bitmaps.size() - 1;
                points.add(point);
                //Log.d("pointClick",point.toString());
                invalidate();
                return true;
            }
        }
        return false ;
    }
}

    class Point {
        float x, y;
        int i;
        boolean isTaken = false;

        @Override
        public String toString() {
            return i + ": " + x + ", " + y;
        }
    }

In the onTouch Method you can see how I’m figuring out the position of the image that I dont want to overdraw. I’m saving all images and their points I draw to lists to be able to modify them. Next I check how much space the image takes on the screen and if I just clicked on that space.

However, after the first image got drew to the canvas, I can’t draw another one. Seems like the program is stuck on “isTaken”. Probabbly my logarithm on how to check if the space is taken is wrong. Can’t figure out what is wrong, though.

Do u have an idea?

THanks in advance

  • 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-06-09T13:31:45+00:00Added an answer on June 9, 2026 at 1:31 pm

    ok several points. One logical error and several enhancements. (code and idea)

    First: When you use the foreach construct use the variable from the loop (e.g. point) to access the instance from the list you are looping over. So change your points.get(i) with a simple point.

    Second: As you have circle you don’t have to check if the point is in the rectangle (I suppose that is what you want to achieve). Do a check if the distance of the centers of the two point (the ones already drawn and the new one to be drawn) is big enough. Use the Pythagoras equation for this (c^2 = a^2 * b^2).

    Third(the actual error):
    You logic of the two for loops is wrong you are treating x and y separately but you have to check them together. You don’t need for loops to achieve what you want.

    Change your for loops to:

    if(Math.pow(radius*2,2) > Math.pow(event.getX-point.x,2)+Math.pow(event.getY-point.Y, 2))
            point.isTaken = true;
    

    Where the radius is the radius of drawn circle.
    Try it with that and report back if you have more problems.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Guess this is a pretty simple problem. I've got a regex and php that
I have a pretty simple problem. Basically I have an array called $list that
I'm having a problem with a pretty simple setup in NHibernate. (I'm using Fluent
The problem is that, shaders (pretty simple ones, as I'm learning OpenGL) fail to
Following code is a pretty simple and complete JQuery Dialog. Everything works. Problem is
I have a problem with WCF. My testing code is pretty simple. I call
My problem is pretty common. I want to show some loading stuff while the
My problem is perhaps pretty simple, but I just started programming in C#. My
?Problem is pretty simple, I extend Button to create ImageButton, and I extend ButtonSkin
I have what I hope to be a pretty simple problem. I'm very simply

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.