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

• November 21st, 2011, 01:12 PM
Kranti1992
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.

• November 21st, 2011, 02:46 PM
bgroenks96
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.
• November 21st, 2011, 03:29 PM
Kranti1992
Re: Converting a picture made up of 0s and 1s into a colored picture??
Attachment 852Hey 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.
• November 21st, 2011, 03:48 PM
Norm
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.
• November 21st, 2011, 03:52 PM
Kranti1992
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..
• November 21st, 2011, 03:57 PM
Norm
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.
• November 21st, 2011, 04:14 PM
Kranti1992
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.
• November 21st, 2011, 04:24 PM
Norm
Re: Converting a picture made up of 0s and 1s into a colored picture??
Quote:

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.
• November 21st, 2011, 04:39 PM
Kranti1992
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?
• November 21st, 2011, 04:55 PM
Norm
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?
• November 21st, 2011, 05:25 PM
bgroenks96
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
Code :

```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)
-Scan each line using the Scanner's next() method.
Code :

```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.

Code :

```//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.