954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Binary Code to Picture

Hi there,

I have been asked to import a text file into java, the file contains a series of 1's and 0's which draw a picture. The 1's will be drawn as a dot where as the 0's will be a blank space. I was wondering if anyone could show me how to get started on this as i am currently struggling with even importing the text file and putting it into an array. I cannot use import.java io*, which means i am unable to use features such as BufferedReader.

Cheers

This is the text file:
00000100100010010000
00111001000001001100
00111110000001111100
00000000000000000000
00000000000000000000

** I can import a text file using

SimpleReader Input = new SimpleReader ("binary.txt")
twazzer
Newbie Poster
6 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

Can you use the Scanner class to read the text file?

What package is the SimpleReader class in? Is that something given you by your teacher?

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

No i can't use that either unfortunately.

The SimpleReader is in a very simple package supplied by our teacher.

twazzer
Newbie Poster
6 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

Has the teacher provided documentation on how to use the class? You will need to read that to see how to use it.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

Hello again, sorry for the slow response i have been very busy.

I have now managed to import the text file into an array and am able to display it in the command prompt exactly how it looks in the text file.

The next problem i have encountered is i need to use the series of 1's and 0's to create the image in a graphics window. This is the code i have so far:

// Create a graphics window for the picture to be displayed in
			SimpleGraphics g = new SimpleGraphics(400,400);
			
			// Use a loop to make 1's appear as dots
			int x = 20;			//Initial x coordinate
			int y = 360;			//Initial y coordinate
			g.setColor(200,0,0);
			
			for(char[] row : Array){
				for(char n : row){
					if (n!=0){
						g.plot(x,y);
						g.moveTo((x+5),(y));
					}
					else
						g.moveTo((x+5),(y));	
					}
				}


The code above only displays one pixel when i try and run it, where as i need to to go through the entire array and display all of the 1's as a pixel or dot.

Cheers.

twazzer
Newbie Poster
6 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 
code above only displays one pixel when i try


Try debugging your code by adding some printlns to show the execution flow and the values of variables as they change.

Where do you change the x,y position of the pixel you are drawing?

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

Thank you, i added some printlns and it has revealed that it just stays on the original x and y co-ordinates and doesn't change.

I thought i was changing the x,y position with:

g.moveTo((x+5),(y));


But now i realise this is clearly wrong, although im still not too sure where to go from here.

twazzer
Newbie Poster
6 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

Sorry, I know nothing the classes and methods you are using. What does the moveTo method do?
If you always pass it the same args, does it always move to the same location? If so, you need to change the x, y values to go to a new location.

Is there a getXXX method that you can use to get the current position that the plot() method uses so you can print them out for debug?

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: