Results 1 to 7 of 7
Thread: Can someone please help Me?!
- 11-25-2011, 10:14 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 11
- Rep Power
- 0
Can someone please help Me?!
If I had a pattern of 0's and 1's like this for example:
0000000000000000000000000
0000000000000000000000000
0000000000000000000000000
0000011000000000000000000
0000101000111000000000000
0000101001000100000000000
0000101000101000000000000
0000100111000111100000000
0000011100000000010000000
0000000010000000010000000
0000000010000011010000000
0000000010000011010000000
0000000010000011010000000
0000000010000011010000000
0000000010001001100000000
0000000010010100100000000
0000000010010100100000000
0000000100010100100000000
0000000100100010010000000
0000000100100010010000000
0000000100100010010000000
0000111001000001001100000
0000111110000001111100000
0000000000000000000000000
0000000000000000000000000
How could I do it in java programming so that the 1's were a coloured dot and the 0's were just background in a graphics window? If you stand away from your screen you can see what it should look like!
I know I have to make a 2D array but I am not entirely sure how to do this? I put the "picture" into a txt file called picture.txt so I need to read it into said 2D array but I am completely lost!
- 11-25-2011, 10:45 AM #2
Member
- Join Date
- Nov 2011
- Location
- Schijndel, Netherlands
- Posts
- 12
- Rep Power
- 0
Re: Can someone please help Me?!
(Just to warn you, haven't tested this code, so there could be compiler errors)
You can make a 2D array with the following code:
// declare the size of your array.
private int cols = 25;
private int rows = 25;
int[][] array2d = new int[rows][cols]; // 25 rows and 25 columns.
Then you need to read your file line by line. Which you do the following way:
try{
String filePath = ""; // Place you pathfile here
FileInputStream fstream = new FileInputStream(filePath);
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
// This is gonna be 1 line from your textfile.
String strLine;
//Read File Line By Line
int xCounter = 0;
while ((strLine = br.readLine()) != null) {
// DO SOMETHING WITH THIS LINE.
// You probably want to cut this line into chars. And then put all of those chars in your 2d array.
//declare the char array
char[] stringArray;
//convert string into array using toCharArray() method of string class
stringArray = strLine.toCharArray();
//display the array
for(int index=0; index < stringArray.length; index++) {
array2d[xCounter][stringArray[i]]);
}
xCounter++;
}
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
Now you have a filled 2D array.
Let's print this out in the console so you can check if everything went allright:
for(int x = 0; x < rows; x++)
{
String line = "";
for(int y = 0; y < cols; y++)
{
line += array2d[rows][cols];
}
System.out.println(line);
}
- 11-25-2011, 11:24 AM #3
Member
- Join Date
- Nov 2011
- Posts
- 11
- Rep Power
- 0
Re: Can someone please help Me?!
OK. Sorry I forgot to mention that we have this package called the "london" package which contains classes for input (EasyReader) and graphics (EasyGraphics). This is what I have so far:
But there are some errors still?!Java Code:import london.*; public class Picture { public static void main (String[] arg) { EasyReader fileInput = new EasyReader("picture.txt"); EasyGraphics g = new EasyGraphics(30,30); private int cols = 25; private int rows = 25; int[][] array2d = new int[rows][cols]; String strLine; int xCounter = 0; char[] stringArray; stringArray = strLine.toCharArray(); for(int index=0; index < stringArray.length; index++) { array2d[xCounter][stringArray[i]]); } cCounter++ } in.close(); }catch (Exception e){//Catch exception if any System.err.println("Error:" + e.getMessage()); }Last edited by Norm; 11-25-2011 at 01:00 PM. Reason: added code tags
- 11-25-2011, 12:59 PM #4
Re: Can someone please help Me?!
You'll have to post the error messages if you want help with them.there are some errors still?!
Or explain what the problem is.
- 11-25-2011, 01:15 PM #5
Member
- Join Date
- Nov 2011
- Posts
- 11
- Rep Power
- 0
Re: Can someone please help Me?!
I fiddled around with it and now only have 1 error but I cannot fix it!
This is the code:
import sheffield.*;
public class Picture {
public static void main (String[] arg) {
EasyReader fileInput = new EasyReader("picture.txt");
EasyGraphics g = new EasyGraphics(30,30);
int cols = 25;
int rows = 25;
int[][] array2d = new int[rows][cols];
int xCounter = 0;
char[] stringArray;
for(int index=0; index < stringArray.length; index++) {
}
xCounter++;
}
}
The error is on the "for(int index=0; index < stringArray.length; index++) {" line and it says "variable stringArray might not have been initialized". That obviously means that I have notinitialised the variable but how do I do this and what do I initialise it to?!
- 11-25-2011, 01:17 PM #6
Member
- Join Date
- Nov 2011
- Location
- Schijndel, Netherlands
- Posts
- 12
- Rep Power
- 0
Re: Can someone please help Me?!
the code you're having now, doesn't read the file.
What methods has EasyReader? Something like nextLine() maybe?
Java Code:// This is where you'll read the file line by line! // The line you read with your EasyReader is put in strLine. while ((strLine = fileInput.readLine()) != null) { //declare the char array char[] stringArray; //convert string into array using toCharArray() method of string class stringArray = strLine.toCharArray(); //display the array for(int index=0; index < stringArray.length; index++) { array2d[xCounter][stringArray[i]]); }
- 11-25-2011, 01:18 PM #7
Re: Can someone please help Me?!
You assign the variable a value using an assignment statement:but how do I do this and what do I initialise it to?!
theVariable = theValue;
The what part is up to you. What value do you want the variable to have? You could give it a null value to make the compiler happy. When/where do you ever give that variable a value?


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks