gpt4 book ai didi

java - 如何在Java中映射Paint中的颜色数组?

转载 作者:行者123 更新时间:2023-12-02 03:55:41 25 4
gpt4 key购买 nike

下面是代码。我在这一行遇到错误

Paint[] p=new Paint[]{cols};

但是如果我使用

Paint[] p=new Paint[]{cols[1]}; 

它不会给出错误。

    Color[] cols = new Color[n];

for (int i = 0; i < n; i++)
{
cols[i] = Color.getHSBColor((float) i / n, 1, 1);

}
Paint[] p=new Paint[]{cols};
return cols;

最佳答案

p 是一个 Paint 数组。 cols 是另一个数组。 p 不能包含 cols,因为 p 中的对象必须是 Paint,而不是数组。

如果要将cols的内容放入p中,可以这样做:

Paint[] p = new Paint[cols.length]; // create a new array with the same length as `cols`
System.arraycopy(cols, 0, p, 0, cols.length); // copy the contents

这相当于迭代数组的长度并复制每个元素。

但如果您确实想要一个 Paint 数组,我不确定为什么要使用 cols 数组。你可以这样做:

Paint[] p = new Paint[n];

for (int i = 0; i < n; i++) {
p[i] = Color.getHSBColor((float) i / n, 1, 1);
}

关于java - 如何在Java中映射Paint中的颜色数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35489959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com