DerDingens
DerDingens

Reputation: 382

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?

Upvotes: 1

Views: 52

Answers (2)

DerDingens
DerDingens

Reputation: 382

Like josef.adamcik statet, i was wrong about the constructors i used for the paint objects. Changing the code to

private void initPaints()
    {
        waypointPaint = new Paint();
        waypointPaint.setColor(Color.GREEN);
        waypointPaint.setStyle(Paint.Style.FILL);
        currentCoordinatePaint = new Paint();
        currentCoordinatePaint.setColor(Color.RED);
        currentCoordinatePaint.setStyle(Paint.Style.FILL);
        linePaint = new Paint();
        linePaint.setColor(Color.BLACK);
        linePaint.setStyle(Paint.Style.STROKE);
        startCoordinatePaint = new Paint();
        startCoordinatePaint.setColor(Color.BLUE);
        startCoordinatePaint.setStyle(Paint.Style.FILL);
    }

did the trick.

Upvotes: 0

Josef Adamcik
Josef Adamcik

Reputation: 5780

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);

Upvotes: 2

Related Questions