使用循环设置红二维数组值 [英] Set red 2d array values using for loop

查看:181
本文介绍了使用循环设置红二维数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建,将得到所有的红色值的图像中,仅显示红色值的方法。我有我的getRedImage()方法的麻烦。我是新来这个和任何帮助将非常AP preciated!

 公共类SimpleRGB
{
    私人诠释宽度,高度;
    私人INT [] []红色=新INT [1000] [1000];

这部分得到红色值,并将其设置为我的2D冲阵的指定位置:

 公共无效setRed(INT X,INT Y,INT ARED)
    {
        红色的[X] [Y] = ARED;
    }

这部分得到的坐标是红色值(x,y)和返回它:

 公众诠释getRed(INT X,int y)对
    {
        INT thisr =红色[X] [Y]
        返回thisr;
    }

我不确定如何词这一部分。我知道我需要创建一个新的SimpleRGB对象返回,然后使用嵌套的for循环设置我的新的简单的RGB oject这个simpleRGB对象的红色二维数组的红二维数组,然后使用嵌套的for循环同时设置了绿色和蓝色的二维数组值都为零。我只是不确定如何做到这一点。

 公共SimpleRGB getRedImage()
    {
        //这是我很困惑。
    }
}


解决方案

我终于想通了:

 公共SimpleRGB getRedImage()
{
    SimpleRGB redImage =新SimpleRGB(宽度,高度);    的for(int i = 0; I<宽度;我++)
    {
        对于(INT J = 0; J<高度; J ++)
        {
            redImage.setRed(I,J,getRed(I,J));
            redImage.setGreen(I,J,getGreen(I,J,0);
            redImage.setBlue(I,J,GetBlue进行(I,J,0));
        }
    }返回redImage;
}

我有正确的基本结构,但我改变这条红/绿/蓝而不是添加redImage.setRed修改新对象。

I am trying to create a method that will get all of the red values in an image and display only the red values. I am having trouble with my getRedImage() method. I am new to this and any help would be much appreciated!

public class SimpleRGB
{
    private int width, height;
    private int[][] red = new int[1000][1000];

This part gets the red value and sets it to the specified position of my 2D red array:

    public void setRed(int x, int y, int aRed)
    {
        red[x][y] = aRed;
    }

This part gets the red value at coordinate (x,y) and returns it:

    public int getRed(int x, int y)
    {
        int thisr = red[x][y];
        return thisr;
    }

I am unsure of how to phrase this part. I know I need to create a new SimpleRGB object to return, and then use nested for-loops to set the red 2D array of my new simple RGB oject to the red 2D array of this simpleRGB object, and then use a nested for loop to set both the green and blue 2D array values to all zero. I am just unsure of how to do this.

    public SimpleRGB getRedImage()
    {
        // This is where I am confused.
    }
}

解决方案

I finally figured it out:

public SimpleRGB getRedImage()
{
    SimpleRGB redImage = new SimpleRGB(width, height);

    for (int i = 0; i < width; i++)
    {
        for (int j = 0; j < height; j++)
        {
            redImage.setRed(i, j, getRed(i, j));
            redImage.setGreen(i, j, getGreen(i, j, 0);
            redImage.setBlue(i, j, getBlue(i, j, 0));
        }
    }

return redImage;
}

I had the basic structure correct but I was altering THIS red/green/blue instead of adding redImage.setRed to modify the NEW object.

这篇关于使用循环设置红二维数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆