Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2011
    Posts
    11
    Rep Power
    0

    Default 2D Array to Graphics

    I have this code which makes a 2D array out of a .txt file which contains 25X25 0's and 1's (625 characters in all) to make a picture. I have a graphics class calledEasyGraphics (which is a part of the "london" package), which makes a graphics window of custom size. I need the picture to be displayed in the graphics window except the 1's need to be a green dot and the 0's need to be just background.

    Here is the code for the 2D array:

    Java Code:
    import java.io.IOException;
    import london.*;
    public class Picture {
    	public static void main (String[] arg) {
    		EasyReader fileInput = new EasyReader("picture.txt");
    		int cols = 30; // The number of columns the array should have
    		int rows = 30; // The number of rows the array should have
                    EasyGraphics g = new EasyGraphics(1000,1000);
    		int[][] array2d = new int[rows][cols];
    
    		String strLine;
    		int xCounter = 0;
    		// Reading lines from the file using EasyReader
    		try {
    		    while ((strLine = fileInput.readLine()) != null) // fill the variable strLine with a String value.
    		    { 
    			char[] stringArray= new char[42];
    			stringArray = strLine.toCharArray(); 
    			//display the array
    			for(int index=0; index < stringArray.length; index++)
    			{
    				array2d[xCounter][index] = (int) stringArray[index] - 0x30;
    			}
    			xCounter++;
    		    }
    	         }
    	         catch (IOException ex)
    	         {
    	             System.out.println(ex.toString());
                     }  
            }
    }
    Does anyone know how I can achieve this? Thank you.
    Last edited by Norm; 11-28-2011 at 01:35 PM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,251
    Rep Power
    25

    Default Re: 2D Array to Graphics

    Map the contents of the2D array to the x,y pixel positions in an image. Set the pixels in the image as per the 0/1 values in the array.

  3. #3
    Join Date
    Nov 2011
    Posts
    11
    Rep Power
    0

    Default Re: 2D Array to Graphics

    yeah i tried doing that but i cant seem to get it to work?!

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,251
    Rep Power
    25

    Default Re: 2D Array to Graphics

    i cant seem to get it to work?!
    Post the code with the errors you get or a description of what the problem is.

  5. #5
    Join Date
    Nov 2011
    Posts
    11
    Rep Power
    0

    Default Re: 2D Array to Graphics

    Java Code:
    import java.io.IOException;
    import london.*;
    public class Picture {
    	public static void main (String[] arg) {
    		EasyReader fileInput = new EasyReader("picture.txt");
    		int cols = 30; // The number of columns the array should have
    		int rows = 30; // The number of rows the array should have
                    EasyGraphics g = new EasyGraphics(1000,1000);
    		int[][] array2d = new int[rows][cols];
    
    		String strLine;
    		int xCounter = 0;
    		// Reading lines from the file using EasyReader
    		try {
    		    while ((strLine = fileInput.readLine()) != null) // fill the variable strLine with a String value.
    		    { 
    			char[] stringArray= new char[42];
    			stringArray = strLine.toCharArray(); 
    			//display the array
    			for(int index=0; index < stringArray.length; index++)
    			{
    				array2d[xCounter][index] = (int) stringArray[index] - 0x30;
    			}
    			xCounter++;
    		    }
    	         }
    	         catch (IOException ex)
    	         {
    	             System.out.println(ex.toString());
                     }
                     
                     // display the picture
                     for (int y=0; y<rows; y++)
                       for (int x=0; x<cols; x++)
                         if (array2d[x][y]==1)
                           g.plot(x,y);
            }
    }
    There are no errors, but it just produces a blank white screen with no picture.
    Last edited by Norm; 11-28-2011 at 02:31 PM. Reason: added code tags

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,251
    Rep Power
    25

    Default Re: 2D Array to Graphics

    Where do you build an image and set its pixels to green?

    You are using a third party package: london that no one else has. No one without that package can test your code.

Similar Threads

  1. Drawing a graphics onto another Graphics ?
    By Ziden in forum Java Applets
    Replies: 0
    Last Post: 01-08-2011, 07:30 PM
  2. Graphics: Buffered Array as Video Memory
    By TiagoCT in forum Java 2D
    Replies: 2
    Last Post: 11-11-2009, 01:34 PM
  3. Graphics
    By Joe2003 in forum Advanced Java
    Replies: 1
    Last Post: 01-25-2008, 06:24 PM
  4. graphics
    By Joe2003 in forum Advanced Java
    Replies: 4
    Last Post: 01-18-2008, 07:44 PM
  5. Adding graphics to array
    By romina in forum Java 2D
    Replies: 1
    Last Post: 08-01-2007, 01:45 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
  •