shannon
shannon

Reputation: 589

Why is the rectangle not shown when I used drawRect()?

Why is the rectangle not shown when I used drawRect() on canvas object, and also declared it inside onCreate method.

Code

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activitymain);
    Chronometer stopWatch = (Chronometer)findViewById(R.id.chrono);
    mDrawingActivity = (DrawingActivity)findViewById(R.id.the_canvas);
    Button b = (Button)findViewById(R.id.startButton);
    b.setText("start");
    b.setOnClickListener(this);
}

OnDraw() Method

protected void onDraw(Canvas Square) 
    {
        super.onDraw(Square);
            Paint squareColor = new Paint();
            squareColor.setColor(Color.CYAN); // change the box color to cyan
        Square.drawRect(100,100,100,100, squareColor); 
return;
    }

Clarification: Even the button and chronometer are not shown too and the program is forced closed.

Upvotes: 0

Views: 3404

Answers (1)

Shakti Malik
Shakti Malik

Reputation: 2407

You are drawing an point rectangle. Change line

Square.drawRect(100,100,100,100, squareColor);

to

Square.drawRect(100, 100, 200, 200, squareColor)

Here is the definition from doc.

drawRect(float left, float top, float right, float bottom, Paint paint)

Draw the specified Rect using the specified paint. The rectangle will be filled or framed based on the Style in the paint.

Parameters left The left side of the rectangle to be drawn top The top side of the rectangle to be drawn right The right side of the rectangle to be drawn bottom The bottom side of the rectangle to be drawn paint The paint used to draw the rect

Upvotes: 7

Related Questions