Results 1 to 7 of 7
  1. #1
    Brainz is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Default Invoking a method

    I have the following code in a method in a .java file. The method is called scaleUp and takes a Picture object and returns another picture object. I'm trying to invoke or call on this method in a new .java file but I'm not sure how to do so:

    Java Code:
    Picture scaleUp(int numTimes)
    {
      Picture targetPicture =
        new Picture(this.getWidth() * numTimes,
                    this.getHeight() * numTimes);
      Pixel sourcePixel = null;
      Pixel targetPixel = null;
      int targetX = 0;
      int targetY = 0;
    
      for (int sourceX = 0;
           sourceX < this.getWidth();
           sourceX++)
    {
    
        for (int sourceY=0;
            sourceY < this.getHeight();
            sourceY++)
        {
    
          sourcePixel = this.getPixel(sourceX,sourceY);
    
    
          for (int indexY = 0; indexY < numTimes; indexY++)
          {
    
            for (int indexX = 0; indexX < numTimes; indexX++)
            {
              targetX = sourceX * numTimes + indexX;
              targetY = sourceY * numTimes + indexY;
              targetPixel = targetPicture.getPixel(targetX,
                                                   targetY);
              targetPixel.setColor(sourcePixel.getColor());
            }
          }
        }
      }
    
      return targetPicture;
    
    }
    }
    This is what I've tried so far:
    Java Code:
    public class Lab7
    {      
      
      public static void main(String[] args) 
      {
         int numTimes;
         String fileName = FileChooser.pickAFile();
         Picture somePic = new Picture(fileName);
         somePic = somePic.scaleUp(numTimes);
      }
      
      public Picture scaleUp(int numTimes)
      {
       
       this.scaleUp(numTimes);
          return //some value
      }
    }
    I know you have to somehow use an object (picture in this case) to call on the method but I'm pretty lost.

  2. #2
    Join Date
    Jul 2010
    Location
    India
    Posts
    58
    Rep Power
    0

    Default

    Java Code:
    public class Lab7
    {      
      
      public static void main(String[] args) 
      {
         int numTimes;
         String fileName = FileChooser.pickAFile();
         Picture somePic = new Picture(fileName);
         somePic = somePic.scaleUp(numTimes);
          //Your class Name here 
         Picture pic = new className().scaleUp(int numTimes); 
      }
     
    }
    Same as you did with somePic.......
    Happy Coding ...
    Last edited by Vinod Mukundan; 12-06-2010 at 12:27 PM.
    _______________________________________________
    give me beans .........

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

    Default

    Thanks for the quick reply but I'm still a bit confused.

    Java Code:
          //Your class Name here 
         Picture pic = new className().scaleUp(int numTimes);

    Do you mean that I have to create another class and if so what would go inside of it?

  4. #4
    Join Date
    Jul 2010
    Location
    India
    Posts
    58
    Rep Power
    0

    Default

    Quote Originally Posted by Brainz View Post
    Thanks for the quick reply but I'm still a bit confused.

    Java Code:
          //Your class Name here 
         Picture pic = new className().scaleUp(int numTimes);

    Do you mean that I have to create another class and if so what would go inside of it?
    Oops sorry my wrong ...

    Picture pic = new className().scaleUp(int numTimes);

    Here the className is the name of your class(.java file) in which the method scaleUp(int numTimes) is there. Say if the class name is editPicture.java then

    Picture pic = new editpicture().scaleUp(int numTimes);
    Last edited by Vinod Mukundan; 12-06-2010 at 01:13 PM.
    _______________________________________________
    give me beans .........

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default

    What class is scaleUp() part of (that is the full method you posted)?

    If it's part of Picture, as seems to be the case, then surely this bit of your code is correct and you don't need a version of scaleUp in the Lab7 class?
    Java Code:
      public static void main(String[] args) 
      {
         int numTimes;
         String fileName = FileChooser.pickAFile();
         Picture somePic = new Picture(fileName);
         somePic = somePic.scaleUp(numTimes);
      }
    That there calls scaleUp on somePic, and assigns the result back to somePic.

    I have no idea what Vinod is suggesting...

  6. #6
    Join Date
    Jul 2010
    Location
    India
    Posts
    58
    Rep Power
    0

    Default

    Thats exactly I had suggested to him .... but just with a simple example .. :p I dont know the class name that he needs to call so gave an example.. If you see I already told him
    Same as you did with somePic.......
    Last edited by Vinod Mukundan; 12-06-2010 at 01:56 PM.
    _______________________________________________
    give me beans .........

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default

    Not really.
    I was pointing out he was already doing it.
    I suspect the OP is simply confused about what methods represent against a class.

Similar Threads

  1. Invoking method from String
    By Supamagier in forum Advanced Java
    Replies: 12
    Last Post: 05-29-2009, 09:21 AM
  2. Invoking a superclass version of a overridden method
    By CyberFrog in forum New To Java
    Replies: 3
    Last Post: 05-25-2009, 01:33 PM
  3. invoking javascript on in jsp
    By pankaj_salwan in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 12-12-2008, 04:54 PM
  4. Invoking superclass methods... how?
    By rhobincu in forum New To Java
    Replies: 7
    Last Post: 08-09-2007, 03:10 PM
  5. invoking method and output questions
    By bluekswing in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 06:36 AM

Posting Permissions

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