Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用嵌套for循环绘制大小为*的正方形的N*N网格?_Java_Image_Oop_Colors - Fatal编程技术网

Java 如何使用嵌套for循环绘制大小为*的正方形的N*N网格?

Java 如何使用嵌套for循环绘制大小为*的正方形的N*N网格?,java,image,oop,colors,Java,Image,Oop,Colors,使用颜色类在可以插入到n*n网格的[]]颜色数组中绘制随机彩色正方形,并且每个正方形都是size*size此外,图片类采用x、y和颜色参数 我只在网格上设法drawSquare(),但需要用随机颜色的方块填充整个网格 import java.awt.Color; public class ColorSquares { public static void main (String[]args){ int N = Integer.parseInt(args[0]); //

使用颜色类在可以插入到
n*n
网格的
[]]
颜色数组中绘制随机彩色正方形,并且每个正方形都是
size*size
此外,图片类采用x、y和颜色参数

我只在网格上设法
drawSquare()
,但需要用随机颜色的方块填充整个网格

import java.awt.Color;

public class ColorSquares {
    public static void main (String[]args){
        int N = Integer.parseInt(args[0]); //the n*n grid
        int size = Integer.parseInt(args[1]); // the size * size in a square
        Picture p = create(N, size);
        p.show();
        // Picture p=  Picture(N, size);

        // p.show();
    }
        public static void drawSquare(Picture p, int x0, int y0, int size, Color c)  {
        for (int x = x0; x < x0+size; x++)
            for (int y = y0; y < y0+size; y++)
                p.set(x,y,c);
    }


    /* Create a Picture which is an NxN grid of color squares
     * each square is size*size pixels */
    public static Picture create(int N, int size){
        Picture p = new Picture(N * size, N * size);

       Color[][] createColors = createColors(N);
        // the N represents the number of squares for the row to go through
       for (int r = 0 ; r < N  ; r++) {
           // the N also represents the number of squares the column goes through
           for (int c = 0; c < N ; c++) {
            drawSquare(p,r, c , size, createColors[r][c]);

            // drawSquare.set(r,c, createColors[r][c]);
             p.set(r,c,createColors[r][c]);
         }
        }

        return p ;
    }

    /* Create a two-dimensional NxN array of random Colors*/
    public static Color[][] createColors(int N){
        Color [][] colors = new Color [N][N]; // N*N board
     //   Color ranColor = (Color) generateRandomColor(Color);


        for ( int r = 0 ; r < N ; r++ ) {
            for ( int  c = 0; c < N ; c++) {
            colors[r][c] = generateRandomColor();



            }
        }

        return colors;
    }

    /* Generate a random Color (R,G,B each between 0 and 255) */
    public static   Color generateRandomColor(){

        //int colorValue = 0 + (int)(Math.random() * ((255 - 0) + 1));
       // int  colorValue = (int) Math.random (color);
        //return new  Color( colorValue,colorValue, colorValue);
        int red   = (int) (Math.random() * 256);
        int green = (int) (Math.random() * 256);
        int blue  = (int) (Math.random() * 256);

        Color randomColor = new Color( red, green, blue );
            return randomColor;
    }

}
导入java.awt.Color;
公共类彩色方块{
公共静态void main(字符串[]args){
int N=Integer.parseInt(args[0]);//N*N网格
int size=Integer.parseInt(args[1]);//大小*正方形中的大小
图片p=创建(N,大小);
p、 show();
//图片p=图片(N,大小);
//p.show();
}
公共静态空心正方形(图片p,整数x0,整数y0,整数大小,颜色c){
对于(int x=x0;x
很抱歉,您的问题是什么?我如何使用嵌套for循环绘制N*N网格和由createColors[]随机着色的方格。我只能在网格中用随机颜色绘制一个规格。如果你这样做了,你做的有什么不对?我猜你在改变每个像素的颜色?是的,没错,但我需要那个正方形内的每个像素都是相同的颜色,然后,下一个正方形是不同的随机颜色,但所有像素都相同,以此类推,直到网格填充N*N个正方形,而不是以
one
的步长递增,以更大的值递增