1

这是我的代码:

 public static Picture modifyPicture (Picture p, int value)
 {
  // get width and height of the picture
  int width = p.getWidth();
  int height = p.getHeight();
  System.out.println ("Picture has width of " + width + 
                      " and height of " + height);

  Picture p2 = new Picture (width, height*value);

  int x = -1;
  int y = -1;

  for  ( x = 0 ; x < width ;  ++x )
  {
    for ( y = 0 ; y < height ; ++y )
    {
     Pixel pixel1 = p.getPixel (x,y);
     Color c1 = pixel1.getColor();

     Pixel pixel4 = p2.getPixel (x, y);
     pixel4.setColor( c1 );

     Pixel pixel5 = p2.getPixel (x, y + 1 * height);
     pixel5.setColor( c1 );

     Pixel pixel6 = p2.getPixel (x, y + 2 * height);
     pixel6.setColor( c1 );
   }
 }
 return p2; 
}  // end of method

嘿,伙计们,这似乎是一个简单的问题,但我不知道该怎么做。我想以某种方式循环 for 循环内的项目。代码所做的是将图片垂直放置在彼此之上。此代码适用于 3 张图片(值 = 3),但如果我想要更少或更多的图片,我必须不断添加/删除新行,即 Pixel pixel7 等。

我需要一种根据值中的数字循环它的方法。我不是在找你为我编写代码,只是给我一些想法,我该怎么做。感谢您的时间和帮助!

4

1 回答 1

0

您必须修改方法以循环value每个像素的时间:

public static Picture modifyPicture (Picture p, int value)
 {
  // get width and height of the picture
  int width = p.getWidth();
  int height = p.getHeight();
  System.out.println ("Picture has width of " + width + 
                      " and height of " + height);    
  Picture p2 = new Picture (width, height*value);

  for  (int x = 0 ; x < width ;  ++x )
  {
    for (int y = 0 ; y < height ; ++y )
    {
     Pixel pixel1 = p.getPixel (x,y);
     Color c1 = pixel1.getColor();

     for (int j=1; j < value; j++) {
          Pixel pixel = p2.getPixel (x, y + j * height);
          pixel.setColor( c1 );
     }
   }
 }
 return p2; 
}  // end of method
于 2013-10-31T00:08:00.210 回答