显示图像的 RGB 编号

Displaying RGB numbers of an image

我需要显示所选图片中每个像素的所有 RGB 数字,每个数字由 space 分隔,并在图片的每一行之后使用 println(中断)。我已经想出并将代码写入 return 所有 RGB 数字,但我不知道如何在每一行之后中断。到目前为止,这是我的代码..

public void getColor()
{
  System.out.println("This picture's dimensions are: "+this.getWidth()+" by "+this.getHeight());
   for (int row=0; row < this.getHeight(); row++) // gives the number of rows
   {
     for (int col=0; col < this.getWidth(); col++)// gives the number of columns
     {
       Pixel pix = this.getPixel(col,row);        
       System.out.print(pix.getRed()+" "+pix.getGreen()+" "+pix.getBlue()+" ");
     } // end of inner loop
   }// end of outer loop
} // end of method

通过在内循环和外循环之间插入打印语句,只需在每一行的末尾添加一个换行符。

public void getColor()
{
  System.out.println("This picture's dimensions are: "+this.getWidth()+" by     "+this.getHeight());
   for (int row=0; row < this.getHeight(); row++) // gives the number of rows
   {
     for (int col=0; col < this.getWidth(); col++)// gives the number of columns
     {
       Pixel pix = this.getPixel(col,row);        
       System.out.print(pix.getRed()+" "+pix.getGreen()+" "+pix.getBlue()+" ");
     } // end of inner loop
     System.out.print("\n"); //added line break to end of line.
   }// end of outer loop
} // end of method

把它放在你的 col 循环之后:

System.out.print(System.getProperty("line.separator"));

查看此页面: http://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html

您需要将换行符放在最内层的 for 循环之外,因为您希望它在每一行完成后 运行。

public void getColor()
{
  System.out.println("This picture's dimensions are: "+this.getWidth()+" by "+this.getHeight());
   for (int row=0; row < this.getHeight(); row++) // gives the number of rows
   {

     for (int col=0; col < this.getWidth(); col++)// gives the number of columns
     {
       Pixel pix = this.getPixel(col,row);        
       System.out.print(pix.getRed()+" "+pix.getGreen()+" "+pix.getBlue()+" ");
     } // end of inner loop

    //After this ^^^ for loop runs, you've gone over the whole row.
    System.out.println();
   }// end of outer loop
} // end of method