SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: Help with a certain java problem.

  1. #1
    SitePoint Addict Procode's Avatar
    Join Date
    Dec 2006
    Location
    New York
    Posts
    371
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with a certain java problem.

    So for my class, I have to take a picture and swap colors from red to green, blue to red, and green to blue for each pixel.

    This is what I have:
    Code Java:
    public void swapRGB()
      {
        Pixel[] pixelArray = this.getPixels();
        Pixel pic = null;
        int value = 0;
     
        for(int index = 0; index < pixelArray.length; index = index + 1)
        {
          pic = pixelArray[index];
     
          int colorGreen = pic.getGreen();
          int colorBlue = pic.getBlue();
          int colorRed = pic.getRed();
     
          pic.setRed(colorGreen);
          pic.setBlue(colorRed);
          pic.setGreen(colorBlue);
        }
      }

    Which works great, now for the second part I have to create another method that allows me to input a number of how many swaps I want. What is wrong is that the picture for the second method doesn't change colors at all when I use my second method which is:
    Code Java:
    public void swapRGB2(int numSwaps)
      {
        for(int num = 0; num <= numSwaps; num = num + 1)
        {
         this.swapRGB(); 
        }
      }

    When I use the first method it works flawlessly. Second method leaves the pictures unchanged. What am I doing wrong?

  2. #2
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Is numSwaps divisible by 3?

  3. #3
    SitePoint Addict Procode's Avatar
    Join Date
    Dec 2006
    Location
    New York
    Posts
    371
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rushiku View Post
    Is numSwaps divisible by 3?
    numSwaps is the number of how many times the program should swap the colors, so it can be whatever the user wants it too.

  4. #4
    SiteP0int Weazle hooknc's Avatar
    Join Date
    Dec 2004
    Location
    Socialist Republic of Boulder
    Posts
    937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Check out your for loop in your swapRGB method.

    Perhaps 'refactoring' the behavior our of that for loop into its own method called swapRGB might be beneficial. I would then consider renaming the original swapRGB method to be named swapAllPixels or something like that.
    baby steps... baby steps...

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •