Search code examples
javaimagergbpixel

Displaying RGB numbers of an image


I need to display all of the RGB numbers for each pixel in a chosen picture, with each number separated by a space, and use println (break) after every row of the picture. I've figured out and written the code to return all of the RGB numbers, but I don't know how to break after each row. Here is my code so far..

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

Solution

  • You'd need to put the line break outside of the innermost for loop because you want it to run after each row is done.

    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