Search code examples
androidcanvaspaintondraw

android customview overriden onDraw(): canvas draws all rectangles black


I've got a problem with a custom view i use. It draws a grid that i use to represent a floorplan, with a start and current position on it (colored rectangles). (Code here: https://pastebin.com/8SExmtAp).

In short, i initialize different paints like this:

 private void initPaints()
{
    waypointPaint = new Paint(Color.parseColor("#800080"));
    currentCoordinatePaint = new Paint(Color.RED);
    linePaint = new Paint(Color.BLACK);
    startCoordinatePaint = new Paint(Color.BLUE);
}

and use them in onDraw() like this:

    // color the current coordinates
    Coordinates currentCoords = Model.getCurrentCoordinates();
    if (currentCoords != null)
    {
                canvas.drawRect((float) currentCoords.getX() * cellWidth, (float) currentCoords.getY() * cellHeight,
                        (float) (currentCoords.getX() + 1) * cellWidth, (float) (currentCoords.getY() + 1) * cellHeight,
                        currentCoordinatePaint);

    }

    Coordinates startCoordinate = Model.startCoordinate;
    if (startCoordinate != null && startCoordinate != currentCoords)
    {
        canvas.drawRect((float) startCoordinate.getX() * cellWidth, (float) startCoordinate.getY() * cellHeight,
                (float) (startCoordinate.getX() + 1) * cellWidth, (float) (startCoordinate.getY() + 1) * cellHeight,
                startCoordinatePaint);
    }

However, instead of getting a blue one for the startposition and a red one for the current position, both of them are black, see: Screenshot of app

The documentation on the drawRect(...) Method i use just states the following:

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

So..i don't really see where the code is wrong and why i am getting the result i get. Maybe someone of you knows why?


Solution

  • Paint constructor you are using expects int flags as a parameter, not the fill color.

    Try:

    currentCoordinatePaint = new Paint();
    currentCoordinatePaint.setStyle(Paint.Style.FILL);
    currentCoordinatePaint.setColor(Color.RED);