Home>

I need help. We need to draw the recursion using canvas. We were given an example of how to do this, but I can't figure out how to make a square instead of a circle. Program code:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (new MyDraw (this));
    }
    public class MyDraw extends View {
        Paint paint= new Paint ();
        int w, h;
        public MyDraw (Context context) {
            super (context);
            paint= new Paint ();
            paint.setColor (Color.BLACK);
            paint.setStyle (Paint.Style.STROKE);
            paint.setStrokeWidth (3);
        }
        @Override
        protected void onDraw (Canvas canvas) {
            drawCircles (canvas, w /2, h /2, 200);
        }
        public void drawCircles (Canvas canvas, int x, int y, int r) {
            canvas.drawCircle (x, y, r, paint);
            if (r >
 fifty) {
                drawCircles (canvas, x, y -r, r /2);
                drawCircles (canvas, x + r, y, r /2);
                drawCircles (canvas, x, y + r, r /2);
                drawCircles (canvas, x -r, y, r /2);
            }
        }
        @Override
        protected void onSizeChanged (int w, int h, int oldw, int oldh) {
            this.w= w;
            this.h= h;
        }
    }
}

It should look like this:

Instead of drawCircle (), you need to use drawRect (), but I cannot set the input parameters correctly.

Артем Пчёлкин2021-02-23 13:29:06
  • Answer # 1

    No more help with the program. I did everything.

    You are great, that you figured out the problem yourself :) Formulate in this how you achieved this;)

    gil9red2021-02-23 14:46:48

    Doesn't answer the question. To leave your comments or ask the author for clarifications, leave a comment on the corresponding message. -from the check queue

    Stanislav Volodarskiy2021-02-23 17:24:38