Results 1 to 4 of 4
  1. #1
    BariMutation is offline Member
    Join Date
    Oct 2010
    Posts
    22
    Rep Power
    0

    Default Accessing a file, assigning it to an array

    Okay, I think my major problem here is that the professor provided a method that he explicitly told us we would not understand... so I'm unable to see what I'm even doing wrong here. This is my code (the main method I wrote, and the readImageintoArray is the method he wrote).

    Java Code:
    public static void main (String [] args)
    	{
    		
    		ComputeImageStatistics(readImageintoArray);
    		displayGrayscaleImage(readImageintoArray);
    	}
    public static int[][] readImageintoArray(String filename) 
    	{
    		File f = new File(filename);
    		BufferedImage readimage = null;
    		try 
    			{
             	readimage = ImageIO.read(f);
            	} 
    		catch (IOException e) 
    			{
                e.printStackTrace();
            	}
    		int imageWidth = readimage.getWidth();
    		int imageHeight = readimage.getHeight();
    
          int[][] GrayScaleData = new int[imageWidth][imageHeight];
          for (int i = 0; i < imageHeight; i++) 
    			{
                for (int j = 0; j < imageWidth; j++) 
    					{
                   	int pixel = readimage.getRGB(j, i);
    						GrayScaleData[j][i] = (((pixel >> 16) & 0xff) + ((pixel >> 8) & 0xff) + ((pixel) & 0xff)) / 3;
                	}
            	}
          return GrayScaleData;
    	}
    Essentially, I need this GrayScaleData array to be used in other methods of the program, which would be ComputeImageStatistics and displayGrayscaleImage (as you can see in the main method). This GrayScaleData is being created by a color image that is being converted into a black, white, and gray image. What am I doing wrong here?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    What am I doing wrong here?

    readImageintoArray() requires a string argument - the name of the file being used. So your main() method should look like:

    Java Code:
    public static void main (String [] args)
      {
        String filename = "whereever/whatever.jpg"
        ComputeImageStatistics(readImageintoArray(filename));
        displayGrayscaleImage(readImageintoArray(filename));
      }

    A couple of things:

    Watch the case used in method and variable names. computeImageStatistics(), readImageIntoArray(), grayscaleData. (I realise the last two are your teachers...)

    If computeImageStatistics() doesn't actually alter the contents of the array it is passed then you do not need to call the teacher's method twice. Declare an int[][] array in main() and assign it to the data returned by that method, then use that array as the argument to the compute....() and display...() methods.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Also...

    "static" will cause your computer to overheat and become laggy. Not really, but it *will* cause your brain to overheat and become laggy. Don't use static unless you need to which, when you start to learn Java, is (1) main() and (2) because an assignment says you absolutely must.

    (This is dogmatic and provocative. However Java ain't C. If teachers prefer to use Java that's fine, but they should use Java and be prepared to pay the upfront cost of dealing with what classes are. OP: I realise in writing this parenthetical remark that this post may be directed more at your teacher than you...)

  4. #4
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    A bit offtopic, but I just wanted to pop in and agree with pbrockway2. I just had an assignment in my computer graphics class witch required us to implement vector and matrix math. The professor explicitly stated to make all computation methods static. My head still hurts from nonsense like this:
    Java Code:
    Vector3D v1 = new Vector3D(1,2,3);
    Vector3D v2 = new Vector3D(4,1,7);
    Vector3D result = Vector3D.add(v1, v2);
    When it would be much more elegant to simply do this:
    Java Code:
    Vector3D v1 = new Vector3D(1,2,3);
    Vector3D v2 = new Vector3D(4,1,7);
    Vector3D result = v1.add(v2);
    Why not take advantage of OOPs neater organisation and stick to C-like code?
    Ever seen a dog chase its tail? Now that's an infinite loop.

Similar Threads

  1. Replies: 1
    Last Post: 11-04-2010, 03:42 PM
  2. Replies: 2
    Last Post: 04-30-2010, 06:13 PM
  3. Assigning a good Icon to the executable jar file
    By praveen_has in forum AWT / Swing
    Replies: 3
    Last Post: 04-28-2010, 04:39 PM
  4. Array Index Out Of Bounds and Problem in Assigning Values
    By chronoz1300 in forum New To Java
    Replies: 2
    Last Post: 12-28-2009, 08:14 PM
  5. Assigning Words to an Array
    By ScaryJello in forum New To Java
    Replies: 10
    Last Post: 02-17-2009, 05:40 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
  •