Results 1 to 14 of 14
  1. #1
    Matt198525 is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default creating a make grid method

    I need to create a method that makes a square grid, size images wide by size images wide, of duplicate copies of the original image. Every other row needs to be negated. Here's the code I have so far:

    Java Code:
      public Picture makeGrid(int size)
      {
       Picture targetPicture = new Picture(this.getHeight()*size, this.getWidth()*size);
        //declares both the source pixel and target pixel and sets it to null
        Pixel sourcePixel = null;
        Pixel targetPixel = null;
        int targetX = 0;
        int targetY = 0;
        //loops through the source picture columns
        for (int sourceX = 0; sourceX < sourcePicture.getWidth(); sourceX++)
        {
          //loops through the source picture rows
          for (int sourceY = 0; sourceY < sourcePicture.getHeight(); sourceY++)
          {
            //set sourcepixel = correct pixel
            sourcePixel = this.getPixel(sourceX,sourceY);
    //gets the 3 colors
        for (int indexX = 0; indexX < size; indexX++)
            {
              for (int indexY = 0; indexY < size; indexY++)
              {   
                               
                
                targetX = sourceX * size;
                targetY = sourceY * size;
                targetPixel = targetPicture.getPixel(targetX,targetY);
                //set pixel = correct pixel
                int red = targetPixel.getRed();
                int green = targetPixel.getGreen();
                int blue = targetPixel.getBlue();
                
                {//if row is even
                  if (sourceY%2==1 ) {
                    // negates the colors
       
                
                    targetPixel.setColor(new Color(255 - red, 255 - green, 255 - blue));
                    //gets the source and target pixels
                  }
                  else targetPixel.setColor(sourcePixel.getColor());
                  
                }
                
              }
            }
          }
        }
         return targetPicture;
    Last edited by Fubarable; 10-23-2010 at 04:05 AM. Reason: Moderator Edit: Code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Matt198525 View Post
    I need to create a method that makes a square grid, size images wide by size images wide, of duplicate copies of the original image. Every other row needs to be negated. Here's the code I have so far:
    Hello, and welcome to the forum.

    You may need to provide more information so that we'll know more about your problem and be better able to help you. For instance what does your current code do or not do? What error messages are you seeing? What functionality is still missing and how have you tried to supply this functionality? What is the Picture class? Pixel class? And the same for all other classes that are not part of standard Java. The better the question, usually the better the answer.

    Also, I have edited your code and added code tags which should help make your posted code retain its formatting and be more readable.

    To do this yourself, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]
    Best of luck, and again, welcome!

  3. #3
    Matt198525 is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default

    Thanks for the welcome.

    The current code returns a blank picture. What is supposed to be returned is duplicates of the picture depending on what number is entered into the parameter. Such as, if you enter 2 into the parameter, 4 duplicates of the picture are shown, the two on the first row are the same, but the 2 on the second row are negated. If 3 is entered into the parameter you get 3 rows of 3 pictures the middle row being negated, etc.

    I think that the rest of the code is correct. I just don't know what to put into targetX and targetY (the pixels of the target picture) to get it to work.

  4. #4
    Dan691588 is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default

    In your first loop shouldn't it be this.getWidth not sourcePicture??

  5. #5
    machiz7888 is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    I assume I'm probably at the same school as Matt because we have an identical assignments and my code looks extremely similar to his.

    Why're you multiplying the length and with by size? to my (limited) understanding the size variable is only related to the size of the finished project in how many times size said to copy the target pic.


    but yeah, I'm lost so that possibly (probably?) isn't true.

  6. #6
    Matt198525 is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default

    Are you taking CSI 201 at SUNY Albany? You're probably right about the size variable. I really don't understand how entering a number into the parameter is supposed to duplicate the images. Wouldn't it just make the picture bigger since you're multiplying it by the height and width of the picture.

  7. #7
    machiz7888 is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    Yup, thats the class. Somebody on the blackboard forums said to use the scale up method to do it. as for the inverse thing, I came down to nearly the same solution as yours. mine is slightly different; "if ((grid % 2.0) == 0)"

    I cant even test any of the code because I stupidly just bought a new laptop, and sold my new one. Dr java + 64bit apparently doesn't mix well. fail.

    I'm planning on heading to office hours tomorrow, which will hopefully make this all more clear.

  8. #8
    Dan691588 is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default

    Try new JDK update... what problems do you get with Drjava and 64bit? I'm taking 201 too. already finished the assignment but i'd rather not give you the whole code..

    What you're actually doing is making a completely NEW picture that increases in size according to the parameter (size) to tell it to.. So you're using the same amount of columns/rows in the targetpicture as the sourcepicture but increasing the amount of rows/columns because of the size parameter..

    I spent like 3 hours with my TA friend doing this last night -.-

  9. #9
    Dan691588 is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default

    And you're fine with the
    " Picture targetPicture = new Picture(this.getHeight()*size, this.getWidth()*size);"
    part cause I used it too except i made it targetPic just to make it abit shorter.

  10. #10
    machiz7888 is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    Just an infinite amount of errors with the picture.java file, my code is fine bug wise. when I compile it throws errors for stuff I haven't touched (that worked fine when i was working with 32 bit) Ill look into the new jdk.

    Im sure Ill knock this out tomorrow but im a very trial and error kind of guy and not being able to test is killing me

  11. #11
    Gpcompscinoob is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default

    im working on the same assignment and my code is almost identical all i get back when i run the program is the original image i started with. has anyone came up with a fix to this code?

  12. #12
    machiz7888 is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    Yup, got mine working perfectly. Its the scaleUp method, with a few lines to inverse the color and switching the +/* symbols at the bottom. also switching out numSwaps or whatever their variable was for our new one

  13. #13
    machiz7888 is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    Oh, and by the way, matt if you're still reading this IDK how good an idea posting the code up here is. considering other people from the same class found it there is a chance that a TA or the teacher could find it, and that probably wouldn't be very good for your grade.

  14. #14
    Brainz is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Default

    Has anyone figured out the code for this yet? I am able to get the program to spit out a picture that's the proper size but the caterpillar isn't duplicated, only the original shows up in the corner. I'm guessing that the problem lies in the coordinates for the target pictures pixel and I just can't figure out what they should be. Anybody know what to do here?

    targetPixel = targetPicture.getPixel(x,y);


    Thanks in advance.
    Last edited by Brainz; 10-29-2010 at 03:55 AM.

Similar Threads

  1. Replies: 6
    Last Post: 12-13-2009, 03:44 AM
  2. Static method cannot make new objects?
    By zerkz in forum New To Java
    Replies: 2
    Last Post: 10-15-2009, 04:17 AM
  3. Use an internal Method or Make a class file?
    By TimHuey in forum New To Java
    Replies: 2
    Last Post: 09-18-2009, 04:06 PM
  4. Creating Make File
    By kiki2009 in forum New To Java
    Replies: 7
    Last Post: 07-11-2009, 02:20 PM
  5. Help creating a changing grid
    By adlb1300 in forum New To Java
    Replies: 11
    Last Post: 10-24-2007, 02:41 PM

Posting Permissions

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