mk_89
mk_89

Reputation: 2742

Color cannot be converted to int

I'm getting a weird error, I am unable to set the color of a paint object with a Color object, this is strange seeing as how paint.setColor(Color.RED) is valid whilst paint.setColor(this.color) is not

Here is the code that I have.

public class Shape{
    protected GameView2 game_view;
    protected int x;
    protected int y;
    protected int width;
    protected int height;
    protected Color color;

    public Shape(GameView2 game_view, Color color, int x, int y, int width, int height) {
        this.game_view = game_view;
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.color = color;
    }

    public void onDraw(Canvas canvas){

        Paint paint = new Paint();
        Rect rec = new Rect(x, y, x + width, y + height);
        paint.setColor(this.color); //does not work
        paint.setStyle(Paint.Style.FILL);
        canvas.drawRect(rec, paint);        
    }
}

Edit:

the Shape object has been declared another class called GameView, its very big so I wont paste the whole class, but when creating a Shape object this is what is done:

new Shape(this, Color.BLACK, 0, 0, 100, 100)

The error I get is incompatible types: Color cannot be converted to int

Upvotes: 0

Views: 3944

Answers (3)

Nathua
Nathua

Reputation: 8836

setColor requires int type but this.color is not int, you try to set the color object instead of int value.

public native void setColor(int color);

Upvotes: 3

ToYonos
ToYonos

Reputation: 16843

How are you passing a Color to your new Shape ?

You should use

getResources().getColor(R.color.idcolor);

And in the xml :

<color name="idcolor">#123456</color>

edit

According to your edit, try to declare color as an int. A color is actually an int, main colors are declared as static fields in Color

protected int color;

Upvotes: 0

Henry
Henry

Reputation: 43798

Looking into the documentation, you see that Color is a utility class that privides static methods to deal with color ints. That you actually can instantiate a Color object seems to be a historical mistake.

Upvotes: 1

Related Questions