Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: Passing Arrays Between Methods

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Passing Arrays Between Methods

    Background: I am new to Java OO programming and I am currently taking a course in computer graphics. I wrote the program originally within the main method and it executed without any issues. However, I am currently trying to re-write the program to call methods to practice my java programming. My re-designed program does not execute. Please help me!!!

    Program function: The assignment was to write a program that drew a line using Bresenham's line algorithm, write the coordinates to an array and then write the array to an image (.png) file.

    I'm writing and testing code in Eclipse.

    This is the code that dose not execute that calls methods (working single method code below):
     
    package hw2;
     
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
     
    import javax.imageio.ImageIO;
     
    class hermiteBezier {
     
    	//declare variables and arrays to be passed to different methods
    	int height = 256;
    	int width = 512;
    	int image[][][] = new int[3][height][width];
     
    public void main (String[] args){
     
    	//call method to create Bresenham's line
    	bresenhamLine(128,128,108,148,image);
     
    	//call method to write image containing line array to file
    	createPNG(image);
     
    }
     
    //Method to write array to image file
    public void createPNG (int[][][] imagePNG) {
     
     
    	// -- write image to file
    	System.out.println("writing to file");
        try {
        	// -- move image into BufferedImage object
            BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            for (int i = 0; i < height; ++i) {
            	for (int j = 0; j < width; ++j) {
            		int pixel = (imagePNG[0][i][j] << 16) | (imagePNG[1][i][j] << 8) | (imagePNG[2][i][j]);
            		bi.setRGB(j, i, pixel);
            	}
            }
            File outputfile = new File("c:/Users/jclarine/Documents/Courses/CLU Classes/Computer Graphics/bressLine/"+"randomcolors.png");
            ImageIO.write(bi, "png", outputfile);
        } catch (IOException e) {
            System.out.println("image write error");
        }
     
    	System.out.println("done"); 
     
    }
     
     
    public void bresenhamLine(int x0, int y0, int x1, int y1, int[][][] imageBres) {
     
    	// -- imageBres[0][][] is the red plane
    	//    imageBres[1][][] is the green plane
    	//    imageBres[2][][] is the blue plane
     
    	int dx;
    	int dy;	
    	int sx;
    	int sy;
    	int err;
    	int e2;
     
       dx = Math.abs(x1-x0);
       dy = Math.abs(y1-y0); 
       if (x0 < x1) {sx = 1;} else {sx = -1;};
       if (y0 < y1) {sy = 1;} else {sy = -1;};
       err = dx-dy;
     
       while (x0 != x1){
    	   while (y0 != y1) {
     
    		 //write location of dot to array
             imageBres[0][x0][y0] = 255;	
     
    	     e2 = 2*err;
     
    	     if (e2 > -dy){ 
    	       err = err - dy;
    	       x0 = x0 + sx;
    	     }
    	     if (e2 <  dx){ 
    	       err = err + dx;
    	       y0 = y0 + sy; 
    	     }
    	   }
       }
     
       image = imageBres;
     
    }
     
     
    }

    This is my original working code:

    package bLine;
     
    //algorithm from wikipedia, png code from Dr. Reinhart
     
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
     
    import javax.imageio.ImageIO;
     
     
     
    class bressLinev3 {
     
     
     
    public static void main (String[] args){
    	int height = 256;
    	int width = 512;
    	// -- Image[0][][] is the red plane
    	//    Image[1][][] is the green plane
    	//    Image[2][][] is the blue plane
    	int image[][][] = new int[3][height][width];
    	int version = 600;
    	int x0 = 90;
    	int y0 = 256;
    	int x1 = 98;
    	int y1 = 168;
    	int dx;
    	int dy;	
    	int sx;
    	int sy;
    	int err;
    	int e2;
     
    	   dx = Math.abs(x1-x0);
    	   dy = Math.abs(y1-y0); 
    	   if (x0 < x1) {sx = 1;} else {sx = -1;};
    	   if (y0 < y1) {sy = 1;} else {sy = -1;};
    	   err = dx-dy;
     
    	   while (x0 != x1){
    		   while (y0 != y1) {
    			   ++version;
    		     image[0][x0][y0] = 255;
     
    		     e2 = 2*err;
     
    		     if (e2 > -dy){ 
    		       err = err - dy;
    		       x0 = x0 + sx;
    		     }
    		     if (e2 <  dx){ 
    		       err = err + dx;
    		       y0 = y0 + sy; 
    		     }
     
    				// -- write image to file
    				System.out.println("writing to file");
    			    try {
    			    	// -- move image into BufferedImage object
    			        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    			        for (int i = 0; i < height; ++i) {
    			        	for (int j = 0; j < width; ++j) {
    			        		int pixel = (image[0][i][j] << 16) | (image[1][i][j] << 8) | (image[2][i][j]);
    			        		bi.setRGB(j, i, pixel);
    			        	}
    			        }
    			        File outputfile = new File("c:/Users/jclarine/Documents/Courses/CLU Classes/Computer Graphics/bressLine/"+"randomcolors_"+version+".png");
    			        ImageIO.write(bi, "png", outputfile);
    			    } catch (IOException e) {
    			        System.out.println("image write error");
    			    }
     
    				System.out.println("done"); 
     
    		   }
    	   }
     
     
     
     
     
     
    	}
     
     
     
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,069
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Passing Arrays Between Methods

    My re-designed program does not execute.
    Do you mean it compiles and then fails or are there compiler errors?
    Please copy and paste the full text of the error messages here.

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Passing Arrays Between Methods

    The pop up window error I receive reads:

    "Fatal exception occurred. Program will exit."

    In the Eclipse console, the error I get reads:

    "java.lang.NoSuchMethodError: main
    Exception in thread "main" "

    Also, I'm not compiling the program. I'm running the program within the Eclipse IDE. Thanks!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,069
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Passing Arrays Between Methods

    Normally you have to compile a source before executing it. The IDE is hiding that from you.

    The java program is looking for a static method named main that it calls to start execution.
    Check that you have the correctly named method with the correct signature.
    I'm surprised that the IDE doesn't warn you about it.

  5. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Passing Arrays Between Methods

    The IDE does not give me any errors, however if I put static in the main method, the IDE gives me an error calling the array named image. The image array is declared outside of the main method in the class, and reason I'm declaring in outside of the main method to pass it to the other methods. This is the first time I'm trying to do all of this, so I may have it wrong.

    The error IDE gives me is "Cannot make a static reference to the non-static field image."

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,069
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Passing Arrays Between Methods

    IDE gives me an error calling the array named image.
    Please copy and paste here the error messages. I can't see them from here.

  7. #7
    Junior Member
    Join Date
    Sep 2011
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Passing Arrays Between Methods

    These are the errors that I get in the console when I click run:

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    Cannot make a static reference to the non-static field image
    Cannot make a static reference to the non-static field image

    at hw2.hermiteBezier.main(hermiteBezier.java:19)

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,069
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Passing Arrays Between Methods

    code in static methods can not access variables and methods that only exist when an instance of the class is created.
    Two solutions:
    make everything static
    have the main method create an instance of the class and put the method calls in the constructor of the class. This is the easier solution. It takes 3 lines of code.

  9. #9
    Junior Member
    Join Date
    Sep 2011
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Passing Arrays Between Methods

    Unfortunately, I do not know how to do that. Could you post the 3 lines of code required or something demonstrating what the code would look like?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,069
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Passing Arrays Between Methods

    public static void main (String[] args) {  
        new  ArraysProblem();                          // 1  call the constructor
    }   // end main()                                     // 2 
     
    public ArraysProblem() {                          // 3  The class constructor
    	//call method to create Bresenham's line
    	bresenhamLine(128,128,108,148,image);

    I renamed your class.

  11. #11
    Junior Member
    Join Date
    Sep 2011
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Passing Arrays Between Methods

    I tried implementing your code and I received the following error: ArraysProblem cannot be resolved to a type.

    Therefore, I tried making everything static. I brought my image array declarations into the main method. However, I back to my original problem of not being able to pass an array. I get the error:

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    image cannot be resolved to a variable

    at hw2.hermiteBezier.bresenhamLine(hermiteBezier.java :91)
    at hw2.hermiteBezier.main(hermiteBezier.java:19)

    The error matches with one of the last lines of code: "image = imageBress;" This is the portion of the code that I hope copies the contents in the imageBress array to the image array.


    package hw2;
     
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
     
    import javax.imageio.ImageIO;
     
    class hermiteBezier {
     
    public static void main (String[] args){
     
    	//declare variables and arrays to be passed to different methods
    	int height = 256;
    	int width = 512;
    	int image[][][] = new int[3][height][width];
     
    	//call method to create Bresenham's line
    	bresenhamLine(128,128,108,148,image);
     
    	//call method to write image containing line array to file
    	createPNG(height, width, image);
     
    }
     
     
    //Method to write array to image file
    public static void createPNG (int width, int height, int[][][] imagePNG) {
     
     
    	// -- write image to file
    	System.out.println("writing to file");
        try {
        	// -- move image into BufferedImage object
            BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            for (int i = 0; i < height; ++i) {
            	for (int j = 0; j < width; ++j) {
            		int pixel = (imagePNG[0][i][j] << 16) | (imagePNG[1][i][j] << 8) | (imagePNG[2][i][j]);
            		bi.setRGB(j, i, pixel);
            	}
            }
            File outputfile = new File("c:/Users/jclarine/Documents/Courses/CLU Classes/Computer Graphics/bressLine/"+"randomcolors.png");
            ImageIO.write(bi, "png", outputfile);
        } catch (IOException e) {
            System.out.println("image write error");
        }
     
    	System.out.println("done"); 
     
    }
     
     
    public static void bresenhamLine(int x0, int y0, int x1, int y1, int[][][] imageBres) {
     
    	// -- imageBres[0][][] is the red plane
    	//    imageBres[1][][] is the green plane
    	//    imageBres[2][][] is the blue plane
     
    	int dx;
    	int dy;	
    	int sx;
    	int sy;
    	int err;
    	int e2;
     
       dx = Math.abs(x1-x0);
       dy = Math.abs(y1-y0); 
       if (x0 < x1) {sx = 1;} else {sx = -1;};
       if (y0 < y1) {sy = 1;} else {sy = -1;};
       err = dx-dy;
     
       while (x0 != x1){
    	   while (y0 != y1) {
     
    		 //write location of dot to array
             imageBres[0][x0][y0] = 255;	
     
    	     e2 = 2*err;
     
    	     if (e2 > -dy){ 
    	       err = err - dy;
    	       x0 = x0 + sx;
    	     }
    	     if (e2 <  dx){ 
    	       err = err + dx;
    	       y0 = y0 + sy; 
    	     }
    	   }
       }
     
       image = imageBres;
     
    }
     
     
    }

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,069
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Passing Arrays Between Methods

    I received the following error: ArraysProblem cannot be resolved to a type.
    I renamed your class for my tests. Replace ArraysProblem with the name that you have.

  13. #13
    Junior Member
    Join Date
    Sep 2011
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Passing Arrays Between Methods

    By the way, thank you for your help so far!!!

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,069
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Passing Arrays Between Methods

    That'll be it for tonight. Back tomorrow.

  15. The Following User Says Thank You to Norm For This Useful Post:

    kigroy (September 10th, 2011)

  16. #15
    Junior Member
    Join Date
    Sep 2011
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Passing Arrays Between Methods

    Actually, that finally got everything working!!! I was able to implement code and generate a png file. Thanks for your help tonight and have a good weekend.

Similar Threads

  1. [SOLVED] Writing a program with arrays and class methods... PLEASE HELP?
    By syang in forum What's Wrong With My Code?
    Replies: 17
    Last Post: August 8th, 2011, 07:02 AM
  2. Objects passing to JSP
    By ober0330 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 28th, 2011, 02:19 PM
  3. Passing a value to another class
    By Semple in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 4th, 2010, 04:49 PM
  4. passing string into method
    By tabutcher in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 26th, 2010, 08:43 AM
  5. Passing in more than one flag
    By anon181 in forum Loops & Control Statements
    Replies: 3
    Last Post: February 2nd, 2010, 05:37 AM

Tags for this Thread