java - 使用 Java 中的输入创建 for 循环

标签 java image loops for-loop pixel

这是我的代码:

 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 等。

我需要一种根据 value 中的数字来循环它的方法。我不是在找你为我编写代码,只是给我一些想法如何做到这一点。感谢您的时间和帮助!

最佳答案

您必须修改方法以对每个像素循环 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

关于java - 使用 Java 中的输入创建 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19695687/

相关文章:

使用 GWT 编译器从 Java 到 JavaScript

image - 将坐标点分类为路径

java - 在Android中不断重新生成不同的随机数

java - 测试深度优先树

java - mkdirs 创建文件而不是创建文件夹 [JAVA]

html - 使用 CSS 按比例调整图像大小(警告 : preset max-width/height)

c++ - 平均两幅带掩码的图像

ios - IPA安装循环

c++ - 一个 vector 作为两个其他 vector 的拼凑而成

java - 使用谷歌应用程序引擎中托管的java程序调用facebook api(php)