简体   繁体   English

Java绘图颜色随机颜色?

[英]Java drawing tiles with random color size?

Well I've read some code and came across this: 好吧,我已经阅读了一些代码,并发现了这个:

private int[] tiles = new int [64 * 64];

Thats the tiles array. 这是瓷砖阵列。

And that sets a random colors to the tiles array. 并且为tile数组设置了随机颜色。

    for (int i = 0; i < 64 * 64; i++) {
        this.tiles[i] = rand.nextInt(0xffffff);
    }

And now rendering 现在渲染

public void render() {
    for (int i = 0; i < this.height; i++) {
        for (int j = 0; j < this.width; j++) {
            int index = (j / 50) * 64;
            pixels[j + i * this.width] = tiles[index];
        }
    }
}

(Rendering outside of that class, but it doesnt matter). (在该类之外渲染,但无关紧要)。

Output: 输出:

在此输入图像描述 but if I change j / 50 to j / 16, the tiles gets smaller and becomes this: 但如果我将j / 50更改为j / 16,则瓷砖会变小并变为:

在此输入图像描述 Why does this happen? 为什么会这样? after all the variable index only gets the color from the array? 毕竟变量索引只从数组中获取颜色? why does it make it big? 它为什么会变大? j changes everytime, shouldnt the colors be different everytime? j每次都会改变,每次颜色都不一样吗?

Can someone explain this please? 有人可以解释一下吗? I am kinda confused. 我有点困惑。

Thanks! 谢谢!

It is calculating an index into the list of colours, you have only created 64 different random colours. 它正在计算颜色列表中的index ,您只创建了64种不同的随机颜色。

The loop works out the index of which of those 64 colours to use by converting the x co-ordinate into the index using (j / 50) * 64 . 环路工作了index的要使用的这些64种颜色由x坐标转换为index使用(j / 50) * 64

Because j is an integer j/50 will give you steps, 0-49 becomes 0, 50-99 becomes 1, etc. 因为j是一个整数j/50会给你步骤,0-49变为0,50-99变为1等。

When you then multiply by 64 you are still keeping those steps. 然后当你乘以64时,你仍然保持这些步骤。

If you change the divide from being a 50 to being a 16 then you are making the steps 16 pixels wide rather than 50 pixels wide. 如果将分数从50更改为16,那么您将使步长为16像素宽而不是50像素宽。

Just be careful that you don't run off the edge of your random colours array! 小心你不要跑掉随机颜色阵列的边缘!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM