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 11 of 11

Thread: Converting a picture made up of 0s and 1s into a colored picture??

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Converting a picture made up of 0s and 1s into a colored picture??

    I got a recent task of converting a bunch of 0s and 1s that create a sort of image into a colored picture (Attached txt is the 0s and 1s given to me in the task). The 1 is
    supposed to be converted to a different color than that of the 0s so then the image shows up. It says the task will help you learn arrays, loops, and graphic output. I'm unsure how arrays tie in with this, nor loops as a matter of fact.

    I'm new to java and any help on where to start or how to go about this task would be great, thanks so much in advance
    Attached Files Attached Files


  2. #2
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: Converting a picture made up of 0s and 1s into a colored picture??

    What are the parameters of the assignment? Are you allowed to count the lines and find the size of the image before coding or must your program do that?

    This really isn't too difficult and does involve arrays, loops and graphic output.

  3. The Following User Says Thank You to bgroenks96 For This Useful Post:

    Kranti1992 (November 21st, 2011)

  4. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Converting a picture made up of 0s and 1s into a colored picture??

    output for picture.jpgHey thanks for the reply, yes we can do anything we want like count the number of digits in the line or size of the image before we do the program. We just need the program to convert those into a colored version, with 1s and 0s converted to different colors. We are then to rotate the image and make 4 total images, an example was given and I've attached it. I really don't know where to start I wouldn't ask for how to exactly do it, just how to start and some examples as to how to do such conversion. An explanation of why we need to include such code would be helpful as well, I'm planning to learn from this task.
    Last edited by Kranti1992; November 21st, 2011 at 03:32 PM.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,375
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: Converting a picture made up of 0s and 1s into a colored picture??

    You could start with a GUI consisting of a JFrame and a JPanel. Override the JPanel's paintComponent method and draw some colored shapes on that.
    Then work out a x,y grid for the JPanel and map the 0s and 1s into that grid.
    Use the techniques you worked out when you drew the shapes above to draw on the grid as per the 1s row/column locations.

  6. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Converting a picture made up of 0s and 1s into a colored picture??

    How does this assignment link to arrays and how would I use arrays to do it though? And also loops should be included. Sorry I'm very new to java, in first year course only in the sixth week, which is about 10 lectures..

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,375
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: Converting a picture made up of 0s and 1s into a colored picture??

    You would read the data from the file and store it in an array.
    You should look at how to create a two dimensional array and fill it with some numbers and print those numbers out to see how to use a two dim array before you try writing your program.
    In fact I'd recommend that as you figure out each thing you need to do, that you write a small program to do just that, get it to work and the later copy the logic into your project program.

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

    Kranti1992 (November 21st, 2011)

  9. #7
    Junior Member
    Join Date
    Nov 2011
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Converting a picture made up of 0s and 1s into a colored picture??

    Ok thanks, I just put them into a 2d array so it prints out the shape of the man like shown in the attached pic above (just one man) but in 1s and 0s. I did:
    int[][] picture = {{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0},
    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },{0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0}, (etc until all 25 lines are added)}

    And then looped them as my notes told me to:
    for (int[] row : picture) {
    for (int n : row)
    System.out.print(n+" ");
    System.out.println();

    That made the shape of the man with 0s and 1s when I ran it in command prompt. However I do not know where to go from here, to rotate the man or to convert the 0s or 1s into color and output it instead of showing it on the command prompt.

  10. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,375
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: Converting a picture made up of 0s and 1s into a colored picture??

    to convert the 0s or 1s into color and output it instead of showing it on the command prompt.
    I tried to suggest a way in post#4.

  11. #9
    Junior Member
    Join Date
    Nov 2011
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Converting a picture made up of 0s and 1s into a colored picture??

    Ok thank you very much, reading my previous post do you think my array and loop is on the right track?

  12. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,375
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: Converting a picture made up of 0s and 1s into a colored picture??

    Does it go through the rows and columns in the order that you want?

  13. #11
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: Converting a picture made up of 0s and 1s into a colored picture??

    Alright well here is one way to do this:

    -Find the length and width of the 1s and 0s block in the file.
    -Create a multi-dimensional array of integers with that length and width
    int[][] plane;
     
    //inside of a method...
    plane = new int[length][width]; //where length is the number of rows (left/right) and width is the number of columns (up/down)

    -Instantiate an Image or BufferedImage using a GUI component or by whatever means you want.
    (personally I would use BufferedImage and call createGraphics() to get a Graphics2D object to work with)
    -Make a Scanner reading from your binary text file.
    -Scan each line using the Scanner's next() method.
    public void example() {
     
        Scanner sc = new Scanner(file);
        for(int[] row:plane) {
            String s = sc.next();
            char[] chars = s.toCharArray();
            for(int i=0;i<s.length();i++) {
                row[i] = Integer.parseInt(Character.toString(chars[i]));
            }
        }
    }

    You now have a set of 1s and 0s. You can iterate through that and draw squares onto an image with different colors depending on the number.

    //g2 is a Graphics2D object of the image
    int pixelWidth = 5; //Each of our picture "pixels" is 5 actual screen pixels in width and length.
    int pixelHeight = 5;
    int x,y = 0;  //Location at which the "pixel" is being drawn.
    for(int[] row:plane) {
        for(int i:row) {
            if(i==0)
                g2.setColor(color);
            else  if(i==1)
                g2.setColor(color2);
            g2.fillRect(x,y,pixelWidth,pixelHeight);
            x+=pixelWidth;
        }
        y+=pixelHeight;
    }

    You could easily incorporate those last two examples into one block of code. Just think about it. Instead of converting to integers, you could make your plane a multidimensional array of characters (chars), then test for the characters 1 and 0.

    This should work. I kind of threw the code together on the spot (didn't compile it or anything) so if there are any syntax errors or simple logical errors, I apologize. Let me know and I will correct them.

    See where you can get yourself and if you have more questions just ask.
    Last edited by bgroenks96; November 21st, 2011 at 05:29 PM. Reason: fixed if statement structure error

Similar Threads

  1. How to place icon on a picture base on (x,y) axis
    By FaintSmile in forum Java Theory & Questions
    Replies: 1
    Last Post: July 13th, 2010, 07:27 AM
  2. Showing a picture in a GUI
    By joachim89 in forum AWT / Java Swing
    Replies: 1
    Last Post: February 15th, 2010, 01:42 PM
  3. Help with a program to make a landscape picture
    By noseeds in forum Java Theory & Questions
    Replies: 1
    Last Post: December 15th, 2009, 09:25 PM
  4. Modify Colors in a Picture
    By theuniverse in forum Java Theory & Questions
    Replies: 0
    Last Post: October 17th, 2009, 04:49 PM
  5. Need help with a Picture!
    By Scout in forum Java Theory & Questions
    Replies: 1
    Last Post: October 12th, 2009, 05:33 PM

Tags for this Thread