java - 颜色无法转换为int

标签 java android colors

我收到一个奇怪的错误,我无法使用 Color 对象设置绘制对象的颜色,这很奇怪,因为 paint.setColor(Color.RED) 是如何有效的而 paint.setColor(this.color) 则不是

这是我的代码。

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

编辑:

Shape 对象已声明为另一个名为 GameView 的类, 它非常大,所以我不会粘贴整个类,但是在创建 Shape 对象时,这样做是这样的:

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

我得到的错误是不兼容的类型:颜色无法转换为int

最佳答案

setColor 需要 int 类型,但 this.color 不是 >int,您尝试设置颜色对象而不是 int 值。

public native void setColor(int color);

关于java - 颜色无法转换为int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27067114/

相关文章:

java - 不明白为什么我的程序不起作用

java - 如何将 'spring boot 2.1 with elasticsearch 6.6' 与集群节点 'https' 连接?

java - 一些 Recyclerview 项目相互影响

java - Android 性能 : Strings vs. 枚举与静态最终整数

Java Color(int rgba) 构造函数和 int 溢出

java - 在pom.xml或manifest.yml中指定JDK的版本?

java - 未捕获的异常未使用 log4j 记录到日志文件中

javascript - 根据用户输入更改 Accordion 面板的颜色

Android 从 sqlite db 获取包含今天或之前日期的项目

r - 更改绘图中某些点的颜色