Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.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 如何编写在三种颜色之间进行选择以生成特定图像的条件语句_Java_If Statement_Conditional Statements - Fatal编程技术网

Java 如何编写在三种颜色之间进行选择以生成特定图像的条件语句

Java 如何编写在三种颜色之间进行选择以生成特定图像的条件语句,java,if-statement,conditional-statements,Java,If Statement,Conditional Statements,我正在做一个实验室,准备上大学的计算机科学课程。问题是,“每个都返回一个颜色的int表示形式。每个都接受四个int参数:列索引、行索引、图像宽度和图像高度。最终,您将使用这些参数在颜色之间进行仲裁。” 特别是对于我无法理解的方法,问题是,“在方法boxColor中,编写一个条件语句,在三种颜色之间进行选择以生成此图像” 这是图像: 很明显,我的教授在这里期望的是另一个指定问题的快速示例 问题指出,“在stripesColor方法中,编写一条条件语句,在三种颜色之间进行选择以生成此图像:” 图像

我正在做一个实验室,准备上大学的计算机科学课程。问题是,“每个都返回一个颜色的int表示形式。每个都接受四个int参数:列索引、行索引、图像宽度和图像高度。最终,您将使用这些参数在颜色之间进行仲裁。”

特别是对于我无法理解的方法,问题是,“在方法boxColor中,编写一个条件语句,在三种颜色之间进行选择以生成此图像”

这是图像:


很明显,我的教授在这里期望的是另一个指定问题的快速示例

问题指出,“在stripesColor方法中,编写一条条件语句,在三种颜色之间进行选择以生成此图像:”

图像:

下面是我为完成任务而编写的代码:

  public static int stripesColor(int column, int row, int imWidth, int imHeight) {
      if (column < (imHeight / 3)) {
         return Color.red.getRGB();
      } else if ((column < 2 * (imHeight / 3))) {
         return Color.pink.getRGB();
      } else {
         return Color.orange.getRGB();
    }
  }
公共静态int-stripeColor(int列、int行、int-imWidth、int-imHeight){
如果(列<(imHeight/3)){
返回Color.red.getRGB();
}否则如果((列<2*(imHeight/3))){
返回Color.pink.getRGB();
}否则{
返回Color.orange.getRGB();
}
}

我还应该说清楚,教授为我们提供了一个课程,将使用我们编写的方法生成图像,我们只负责进行计算,以获得他提供的图像

我试着找到整个正方形(512px 512px)的对角线,并在总尺寸的1/5内制作较小的正方形,但我不确定我所做的是否是错误的,或者是否有其他方法可以做到这一点


非常感谢您的帮助。

我建议您将其分解为较小的问题并单独处理。首先只关注一个案例,也许是最简单的一个,然后处理其他案例

就在我的头顶上,别让我太在意数学

public static int stripesColor(int column, int row, int imWidth, int imHeight) {
    // Just worry about the square in the center for now.

    // If pixel is not in left/bottom or top/right quarters:
    if (imWidth / 4 < column < (imWidth * 3)/4) && 
    (imHeight / 4 < row < (imHeight * 3)/4) {

        return Color.hotpink.getRGB();

    } else if {
        // We know that any pixel in the center square is already taken care of, 
        // so the logic for the rest can completely ignore that.
        // It can be written as though the square isn't in the image at all.
    } else {
        // Last remaining color
    }
}
公共静态int-stripeColor(int列、int行、int-imWidth、int-imHeight){
//现在只需担心中心的广场。
//如果像素不在左/下四分之一或上/右四分之一:
如果(imWidth/4<列<(imWidth*3)/4)和
(imHeight/4<行<(imHeight*3)/4){
返回Color.hotpink.getRGB();
}否则如果{
//我们知道中心广场上的任何像素都已经被处理好了,
//所以其他人的逻辑可以完全忽略这一点。
//它可以写得好像正方形根本不在图像中。
}否则{
//最后剩下的颜色
}
}
我也不知道内部正方形的暗度是总尺寸的1/2还是3/5;我在这里假设了1/2,但最终这不重要。希望这能帮你摆脱困境


如果
如果
条件中的数学看起来很糟糕,那么您可以始终为这些值初始化单独的变量,这样条件就会更清晰(基本上是自文档化)。

这不是一个编码问题,而是一个如何构造组合逻辑的问题

让我们把这个盒子拆开。盒子基本上由三部分组成——左上半部分为黄色三角形,右下半部分为青色三角形,顶部为洋红正方形

嗯。让我们看看第一部分-我们如何定义图像的左上半部分? 如果我们将方形图像像素视为图形,则分割黄色和青色的中心线是从原点(0,0)到左上角(imWidth,imHeight)的直线。
这条线的斜率为1,形式为y=x。因此,左上角的一个像素是列By在3种颜色之间选择的任何位置,你是说随机选择3种颜色吗?只要颜色不一样,它们就无关紧要。颜色可以是硬编码的,不需要是随机的。不。如果有帮助,这里是指向实验室作业页面的链接。澄清一下,这个方法是指我帖子中的第一个问题?是的。这就是你想让我们关注的,对吗?好的,我都准备好了,谢谢。我真的很感激!
public static int boxColor(int column, int row, int imWidth, int imHeight) {
    final int UPPER_LEFT_COLOR = 0; // Set to upper-left color.
    final int LOWER_RIGHT_COLOR = 128; // Set to lower-right color.
    final int CENTER_SQUARE_COLOR = 255; // Set to center color.
    final double MARGIN_AMOUNT = 0.1; // Set to buffer percentage

    int return_color = 0; //Initialize the return value to something.
    // First set the return value based on the midline split.
    if (column <= row) {
        return_color = UPPER_LEFT_COLOR;
    } else {
        return_color = LOWER_RIGHT_COLOR;
    }
    // Test for the overlay and reset the return value.
    if ((imWidth * (MARGIN_AMOUNT) < row ) && (row < imWidth * (1-MARGIN_AMOUNT)) && (imHeight * (MARGIN_AMOUNT) < column) && (column < imHeight * (1-MARGIN_AMOUNT))) {
        return_color = CENTER_SQUARE_COLOR;
    }
    // Return the finally determined value.
    return return_color;
}