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

    Default Creating a main method?

    I'm creating a simple .java file that has a method called scaleUp, which just increases the size of a picture. The problem I'm having is in creating the main method. The following is what I've come up with:

    public class ScaleUp
    {

    public static Picture main(String[] args)
    {

    public 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;

    }
    }
    }

    When I try to compile the above, I got the following message
    3 errors found:
    File: C:\Users\Umar\Desktop\ScaleUp.java [line: 7]
    Error: C:\Users\Umar\Desktop\ScaleUp.java:7: illegal start of expression
    File: C:\Users\Umar\Desktop\ScaleUp.java [line: 7]
    Error: C:\Users\Umar\Desktop\ScaleUp.java:7: ';' expected
    File: C:\Users\Umar\Desktop\ScaleUp.java [line: 7]
    Error: C:\Users\Umar\Desktop\ScaleUp.java:7: ';' expected



    What am I doing wrong?
    Thanks in advance.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    You are declaring a method inside another method. You can't do that.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Brainz View Post
    Java Code:
    public class ScaleUp
    {
    
    public static Picture main(String[] args)
      {                          
    
    public Picture scaleUp(int numTimes)
      {
       ...
    What am I doing wrong?
    You are trying to define a method in the body of another method; Java can't do that. Your class structure has to be like this:

    Java Code:
    public class ScaleUp {
    
       public static void main(String[] args) { ... }
       public Picture scaleUp(int numTimes) { ... }
       ...
    Note the return type of the main( ... ) method; it has to be void by definition.

    kind regards,

    Jos

    edit: darn, so early in the morning and already too slow ... ;-)
    Last edited by JosAH; 11-16-2010 at 08:32 AM.
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    Thanks for the quick response.
    I have another question though, what do I declare after:

    Java Code:
    public static void main(String[] args) {
    I tried this:
    Java Code:
    public static void main(String[] args) {
    String fileName = FileChooser.pickAFile();
         Picture pictObj = new Picture(fileName);                            
    }
    public Picture scaleUp(int numTimes)
      {...}
    but I was returned many errors like the following:

    File: C:\Users\Umar\Desktop\ScaleUp.java [line: 10]
    Error: C:\Users\Umar\Desktop\ScaleUp.java:10: cannot find symbol
    symbol : method getWidth()
    location: class ScaleUp

    I think I have to refer to my method or an object that can call on the method getWidth(), but I'm not sure how to do this.

    Thanks again for the help.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Brainz View Post
    Thanks for the quick response.
    I have another question though, what do I declare after:

    Java Code:
    public static void main(String[] args) {
    I tried this:
    Java Code:
    public static void main(String[] args) {
    String fileName = FileChooser.pickAFile();
         Picture pictObj = new Picture(fileName);                            
    }
    public Picture scaleUp(int numTimes)
      {...}
    but I was returned many errors like the following:

    File: C:\Users\Umar\Desktop\ScaleUp.java [line: 10]
    Error: C:\Users\Umar\Desktop\ScaleUp.java:10: cannot find symbol
    symbol : method getWidth()
    location: class ScaleUp

    I think I have to refer to my method or an object that can call on the method getWidth(), but I'm not sure how to do this.

    Thanks again for the help.
    You didn't show us all of the relevant code: the compiler is complaining about the symbol/method getWidth(); it can't find a definition of it but you didn't show us what you had in mind.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    Sorry about that:

    Java Code:
    public class ScaleUp
    {                 
    public static void main(String[] args) {
    String fileName = FileChooser.pickAFile();
         Picture pictObj = new Picture(fileName);                            
    }
    public 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;
    
    }
    }
    The errors are:
    File: C:\Users\Umar\Desktop\ScaleUp.java [line: 10]
    Error: C:\Users\Umar\Desktop\ScaleUp.java:10: cannot find symbol
    symbol : method getWidth()
    location: class ScaleUp
    File: C:\Users\Umar\Desktop\ScaleUp.java [line: 11]
    Error: C:\Users\Umar\Desktop\ScaleUp.java:11: cannot find symbol
    symbol : method getHeight()
    location: class ScaleUp
    File: C:\Users\Umar\Desktop\ScaleUp.java [line: 18]
    Error: C:\Users\Umar\Desktop\ScaleUp.java:18: cannot find symbol
    symbol : method getWidth()
    location: class ScaleUp
    File: C:\Users\Umar\Desktop\ScaleUp.java [line: 22]
    Error: C:\Users\Umar\Desktop\ScaleUp.java:22: cannot find symbol
    symbol : method getHeight()
    location: class ScaleUp
    File: C:\Users\Umar\Desktop\ScaleUp.java [line: 25]
    Error: C:\Users\Umar\Desktop\ScaleUp.java:25: cannot find symbol
    symbol : method getPixel(int,int)
    location: class ScaleUp

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Because you haven't defined those methods. You are calling "this.getWidth....", which means, of course, that those methods must either be defined in that class, or one its super classes (i.e. some class "up the extension chain"). Since you are not extending anything, and haven't defined the methods here, those methods simply don't exist.

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

    Default

    Thanks! I see what I did there. You guys have been very helpful but I have another question.

    My code now is:
    Java Code:
    public class ScaleUp
    {                 
    public static void main(String[] args) {
    String fileName = FileChooser.pickAFile();
         Picture pictObj = new Picture(fileName);
    }
    public Picture scaleUp(int numTimes)
      {
      String fileName = FileChooser.pickAFile();
      Picture origPic = new Picture(fileName);
        Picture targetPicture = 
          new Picture(origPic.getWidth() * numTimes,
                      origPic.getHeight() * numTimes);
        Pixel sourcePixel = null;
        Pixel targetPixel = null;
        int targetX = 0;
        int targetY = 0;
        
        for (int sourceX = 0;
           sourceX < origPic.getWidth();
           sourceX++)
        {
            for (int sourceY=0;
            sourceY < origPic.getHeight();
            sourceY++)
        {
              sourcePixel = origPic.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;
    
    }
    }
    It compiles fine but when I type the following to test my program in the interactions pane:
    Java Code:
    > String fileName = FileChooser.getMediaPath("rose.jpg");
    > Picture p = new Picture(fileName);
    > p = p.scaleUp(2);
    I get this message:
    Static Error: No method in Picture has name 'scaleUp'

    I'm a little confused because I thought I did create the scaleUp method within the Picture class.

  9. #9
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Because it is the class "ScaleUp" that has that method, not the class "Picture".

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

    Default

    Alright. Then can you please explain how I can get the program to actually spit out targetPicture?

  11. #11
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Uhm, add the scaleUp method to the Picture class?

    Make the scaleUp method static and modify it to also take a "Picture" as a parameter?

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

    Default

    How would I go about modifying it to take a "Picture" as a parameter?

  13. #13
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Uhm
    Java Code:
    public Picture scaleUp(Picture somePic, int numTimes)

Similar Threads

  1. Main method
    By vibaviattigala in forum New To Java
    Replies: 2
    Last Post: 11-07-2010, 02:29 PM
  2. Calling The main method from another method
    By SwissR in forum New To Java
    Replies: 3
    Last Post: 07-27-2010, 11:03 AM
  3. Help with main method
    By eliCanzee in forum JDBC
    Replies: 4
    Last Post: 01-06-2010, 09:12 AM
  4. calling method from main method
    By bob_bee in forum New To Java
    Replies: 4
    Last Post: 10-02-2009, 05:30 PM
  5. Main method here to :S:S hlp pls
    By eliCanzee in forum AWT / Swing
    Replies: 6
    Last Post: 05-27-2009, 04:45 AM

Tags for this Thread

Posting Permissions

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