Set red 2d array values using for loop

后端 未结 3 402
别那么骄傲
别那么骄傲 2021-01-27 19:13

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 thi

相关标签:
3条回答
  • 2021-01-27 19:24

    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.

    0 讨论(0)
  • 2021-01-27 19:29

    Looping in 2d array is easy But i don't think this is the right way.

    private int[][] red = new int[1000][1000];
    
    for (int i = 0; i < 1000; i++) {
        for(int j = 0; j < 1000; j++) {
            System.out.println(red[i][j]);
        }
    }
    
    0 讨论(0)
  • 2021-01-27 19:46

    I did not understand very well your question, but, if you have 3 2D arrays, one for each RGB:

    int[][] red = new int[1000][1000];
    int[][] green = new int[1000][1000];
    int[][] blue = new int[1000][1000];
    

    and you only want the red color in the image, just set to zero all other colors:

    public class SimpleRGB
    {
        private int width;
        private int height;
        private int[][] red = new int[1000][1000];
        private int[][] green = new int[1000][1000];
        private int[][] blue = new int[1000][1000];
    
        public SimpleRGB(int[][] red, int[][] green, int[][] blue) {
            this.red = red;
            this.green = green;
            this.blue = blue;
        }
    
        // some methods
    
        public int[][] getRed() {
            return red;
        }
    
        public int[][] getGreen() {
            return green;
        }
    
        public int[][] getBlue() {
            return blue;
        }
    
        // the method that interests you
    
        public SimpleRGB getRedImage() {
            return new SimpleRGB(red, new int[1000][1000], new int[1000][1000]);
        }
    }
    

    Creating a new array, all its elements are initialized by default to 0.

    0 讨论(0)
提交回复
热议问题