Draw rectangle in arbitrary position after touch event in canvas android


User 3353723

CanvasI'm trying to draw a rectangle (should not exceed the canvas size) anywhere in the shape, and if the user touches the shape, a rectangle should be drawn at another random location . I've managed to draw the rectangle, but need help Touch Eventand draw it in random positions.

public class DrawView extends View {
    Paint paint = new Paint();

    public DrawView(Context context) {
        super(context);            
    }

    @Override
    public void onDraw(Canvas canvas) {
        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(3);
        canvas.drawRect(30, 30, 80, 80, paint);
        paint.setStrokeWidth(0);
        paint.setColor(Color.CYAN);
        canvas.drawRect(33, 60, 77, 77, paint );
        paint.setColor(Color.YELLOW);
        canvas.drawRect(33, 33, 77, 60, paint );

    }

}

Any help would be greatly appreciated.

Daniel Hussain

You can follow the instructions below:

E.g

  public class DrawView extends View {

     // here you define hashmap for user touch points

      Paint paint = new Paint();

  public DrawView(Context context) {
      super(context);            
  }

  @Override
  public void onDraw(Canvas canvas) {
      yourRandomFunction(-99,-99);


  }

  public void yourRandomFunction(float x , float y ){
     //for first time user touch 
     // if -99 && -99 { 
    //Calculate random points here.

    paint.setColor(Color.BLACK);
    paint.setStrokeWidth(3);
    canvas.drawRect(30, 30, 80, 80, paint);
    paint.setStrokeWidth(0);
    paint.setColor(Color.CYAN);
    canvas.drawRect(33, 60, 77, 77, paint );
    paint.setColor(Color.YELLOW);
    canvas.drawRect(33, 33, 77, 60, paint );
    // add points to hashmap
}
//else check in hashmap if exist then {
  //Calculate random points here.
    paint.setColor(Color.BLACK);
    paint.setStrokeWidth(3);
    canvas.drawRect(30, 30, 80, 80, paint);
    paint.setStrokeWidth(0);
    paint.setColor(Color.CYAN);
    canvas.drawRect(33, 60, 77, 77, paint );
    paint.setColor(Color.YELLOW);
    canvas.drawRect(33, 33, 77, 60, paint );
    // add point to hashmap
 } 

}

@Override
public boolean onTouchEvent(MotionEvent event) {
      float eventX = event.getX();
       float eventY = event.getY();

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
      //check points in hashmap 

      return true;
    case MotionEvent.ACTION_MOVE:

      break;
    case MotionEvent.ACTION_UP:
      // nothing to do
      yourRandomFuncntion(eventX,eventY);
      break;
    default:
      return false;
    }

    // Schedules a repaint.
    invalidate();
    return true;
      }
    } 

Related


Canvas doesn't draw rectangle after for loop

Digital Brent I'm trying to create a news ticker that renders text in small square "pixels". I say "pixels" because they look like pixels, but the actual displayed square is larger than 1px. So far I can get all the rendered letters from the object I build (co

Canvas doesn't draw rectangle after for loop

Digital Brent I'm trying to create a news ticker that renders text in small square "pixels". I say "pixels" because they look like pixels, but the actual displayed square is larger than 1px. So far I can get all the rendered letters from the object I build (co

Canvas doesn't draw rectangle after for loop

Digital Brent I'm trying to create a news ticker that renders text in small square "pixels". I say "pixels" because they look like pixels, but the actual displayed square is larger than 1px. So far I can get all the rendered letters from the object I build (co

Canvas doesn't draw rectangle after for loop

Digital Brent I'm trying to create a news ticker that renders text in small square "pixels". I say "pixels" because they look like pixels, but the actual displayed square is larger than 1px. So far I can get all the rendered letters from the object I build (co

How to draw partial rounded rectangle on android canvas?

Rocky I want to draw a rounded rectangle on canvas that is filled with color (or bitmap) and has a stroke , but without some selected corners and without borders on some selected sides . Any ideas how I can do this? 7 heaven You can divide the drawing process

How to draw partial rounded rectangle on android canvas?

Zeus I want to draw a rounded rectangle on canvas that is filled with color (or bitmap) and has a stroke , but without some selected corners and without borders on some selected sides . Any ideas how I can do this? 7 heaven You can divide the drawing process i

Kivy Canvas redraws after touch event

Pibibuco I want to make a small game but I need some help... I am new to both python and Kivy. I am using python 3.4 and kivy 1.8.0. The game will contain some drawable elements that can be dragged and/or broken down: - If you click on a point, you can drag it

Kivy Canvas redraws after touch event

Pibibuco I want to make a small game but I need some help... I am new to both python and Kivy. I am using python 3.4 and kivy 1.8.0. The game will contain some drawn elements that will be draggable and/or scatterable: - If you click on a point, you can drag it

Kivy Canvas redraws after touch event

Pibibuco I want to make a small game but I need some help... I am new to both python and Kivy. I am using python 3.4 and kivy 1.8.0. The game will contain some drawn elements that will be draggable and/or scatterable: - If you click on a point, you can drag it

draw rectangle in canvas

125fur I can draw rectangle on video on canvas. But it's hidden behind the video but can't suggest how I should draw the rectangle on the video through the side.Pls like I can draw the code on the image. : <p>Video to use:</p> <video id="video1" controls wi

draw rectangle in canvas

125fur I can draw rectangle on video on canvas. But it's hidden behind the video but can't suggest how I should draw the rectangle on the video through the side.Pls like I can draw the code on the image. : <p>Video to use:</p> <video id="video1" controls wi

Draw resizable rectangle on canvas

Inde · 穆里罗 (Masinde Muliro) I am trying to draw a resizable rectangle on a canvas element. I check the position of the mouse relative to the rectangle on the screen and then update the width/height of the triangle the mouse falls into. So far I've managed to s

draw rectangle on canvas

Avsana Khan I'm trying to develop a ping pong game in Android where I will have two bats to hit the ball. I drew a bat on the canvas and it was placed at the far left of the screen. I need another bat on the far right of the screen, but every time the paddle i

draw rectangle on canvas

Avsana Khan I'm trying to develop a ping pong game in Android where I will have two bats to hit the ball. I drew a bat on the canvas and it was placed at the far left of the screen. I need another bat on the far right of the screen, but every time the paddle i

Draw resizable rectangle on canvas

Inde · 穆里罗 (Masinde Muliro) I am trying to draw a resizable rectangle on a canvas element. I check the position of the mouse relative to the rectangle on the screen and then update the width/height of the triangle the mouse falls into. So far I've managed to s

draw rectangle in canvas

125fur I can draw rectangle on video on canvas. But it's hidden behind the video but can't suggest how I should draw the rectangle on the video through the side.Pls like I can draw the code on the image. : <p>Video to use:</p> <video id="video1" controls wi

canvas touch event js

Rafinha 187 Hi guys, I need some help with my canvas, I've tried multiple ways to make it work on mobile but I can't. I try to start the created method to start and stop the click event. But I don't think this is the strict way. class Canvas { constructor(em

canvas touch event js

Rafinha 187 Hi guys, I need some help with my canvas, I've tried multiple ways to make it work on mobile but I can't. I try to start the created method to start and stop the click event. But I don't think this is the strict way. class Canvas { constructor(em

canvas touch event js

Rafinha 187 Hi guys, I need some help with my canvas, I've tried multiple ways to make it work on mobile but I can't. I try to start the created method to start and stop the click event. But I don't think this is the strict way. class Canvas { constructor(em

canvas touch event js

Rafinha 187 Hi guys, I need some help with my canvas, I've tried multiple ways to make it work on mobile but I can't. I try to start the created method to start and stop the click event. But I don't think this is the strict way. class Canvas { constructor(em

canvas touch event js

Rafinha 187 Hi guys, I need some help with my canvas, I've tried multiple ways to make it work on mobile but I can't. I try to start the created method to start and stop the click event. But I don't think this is the strict way. class Canvas { constructor(em