This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

reading a file to draw an emoji  RSS feed

 
Johnny Januan
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

I'm working on a program to read a file that looks like this:



The objective is to read it and depending on the read value(0,1,2,3) the program must paint a square in a color (black, white, yellow, red) that will make an emoji.

So my idea is to create a method that reads the file and creates a matrix by reading the file and then this matrix will be iterated to read its components and paint the squares.
I have one class called Square:



Then this class that paints the colours on the panel, depending on the value of "c" that should be the position of the matrix that I'm reading.



But what I don't know how to do is the method that reads and passes the values to the matrix.





This reads the file properly.

It would be great if someone could give me some advice on how to continue

Thanks!
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johny,

Since you are just loading an array, all you need is a couple variables for indexes and you can load your array right where you read the data.
I'd look at making the index variables and array as instance variables:

There were you have these lines, add the following:

It's very simple and straight forward. I think you can see what is going on, and I leave it to you to modify if you want to use constants and etc.

 
Johnny Januan
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this but reports me an IndexOutOfBounds:



Am I doing something wrong?

And another question: by doing this the values that will be in cEmot are literally the ones in the file, right? (0,1,2,3)

Thanks
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Move the j++ to after the if statement.

Johnny Januan wrote:I have this but reports me an IndexOutOfBounds:
 
Carey Brown
Bartender
Posts: 2994
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnny Januan wrote:I have this but reports me an IndexOutOfBounds:


Am I doing something wrong?

And another question: by doing this the values that will be in cEmot are literally the ones in the file, right? (0,1,2,3)

Thanks

Because your file is a text file, your 'k' variable will hold the ASCII value for the character, not the values 1, 2, 3. Your approach is also problematic in that it doesn't handle the new-lines in the file which are platform dependent. You're better off using br.readLine() instead.
 
Johnny Januan
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the fast reply.

This time it doesn't compile due to "possible lossy conversion from int to char" here:
Isn't there an easier way of doing this?
I see that if I write it like this it compiles, but when I print the value it appears like this: [[C@1d81eb93 and always the same value.
 
Carey Brown
Bartender
Posts: 2994
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To print
 
Carey Brown
Bartender
Posts: 2994
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This confused me

In one place you're comparing the numeric valu "c==0", and in another you're comparing the ASCII value "c=='1'". Which one do you want?
 
Johnny Januan
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want the value that's in the file.



With this code I'm trying to paint the squares to "form" the emoji, it compiles but it doesn't work. Maybe I'm putting it in the wrong spot.

In my main class I have this:


Thanks for your time
 
Carey Brown
Bartender
Posts: 2994
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To match the change you made, change

to
 
Johnny Januan
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd already changed it
What change do you mean?
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You changed one line:

For the above code to work, you need to have characters in cEmot[][].
 
Johnny Januan
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is exactly what I have in my JPanel class:



And in my main class:



And my class Square:


It compiles perfectly, but it doesn't even open any window. I can't see where's my error. Is it the code that checks the values in cEmot to paint the Panel??

Thanks
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnny Januan wrote:It would be great if someone could give me some advice on how to continue

I realise that this thread has moved on quite a bit, but it seems to me that your Emoji class could include a lot more of this "procedure".

It also strikes me that this whole business of arrays of characters is redundant since (and correct me if I'm wrong), the whole business seems to be to convert your text file(s) into a grid of colours.

On that basis, I think my Emoji class might look something like this:and now you can add a "getter" to return the Color for a particular row and column, viz:or - perhaps even better - write a method to convert the entire thing into a Graphics object. After all, it only needs to be done once, doesn't it?

And now, whenever you want to create an Emoji, all you need is:
Not sure if it helps (and I'm certainly no graphics expert), but it's the way I'd do it.

Winston
 
Johnny Januan
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will try to use the code you posted. However, what I was trying to do is to "copy" the idea of the grid from another program that creates a chessboard. The chessboard is much easier to do, because it doesn't read a file to paint, it just paint the squares in black or white depending on its position. So basically the idea is the same in the case of the Emoji, but with different conditions. The method that creates the chessboard looks like this:



Isn't it possible to do it like this to draw the emoji?
Maybe my whole idea of reading from the array is wrong. Maybe what is being read from the array is displayed as 0,1,2,3 but saved in ASCII so when I write if(cEmot[i][j]=='0') it doesn't enter the condition.
But anyway I think the idea of the chessboard and the emoji are very similar and I wonder that it could be another problem which I can't find what is causing the program to not work at all in terms of representing the graphics.
What I know is that the file is correctly written into the matrix cEmot, maybe it's not properly read?

Thanks
 
Piet Souris
Rancher
Posts: 1980
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your emoji class is not bad, I think.

But I would not store the matrix of colour numbers. Instead, I would subclass Rectangle, and store the color index in it.
You then just need a List of these, and either supply a draw method in your subclassed Rectangle or in your emoji class.
What seeems handy is to have a Map or array of colors (like: 1 = Color.red, 0 = transparent), and a method that allows you to set
or change that map or array. Then you can easily change the colors.

Your chess board would just be another emoji. in which you do not need this Colour[][]. The pattern is known: 01010101 || 10101010 || ...
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnny Januan wrote:I will try to use the code you posted.

Don't if it doesn't make sense. All I was trying to show was how I'd do it; and I'm a great believer in the idea that all classes should know how to construct themselves - in whatever way is required.

It also seems to me that this Emoji thing is basically an icon - ie, a Graphics object (or possibly an image). The fact that you read it from a text file is simply an implementation detail that anything outside your Emoji class shouldn't need to know about.

However, what I was trying to do is to "copy" the idea of the grid from another program that creates a chessboard.
...
Isn't it possible to do it like this to draw the emoji?

Simple answer: Don't know - because I don't know enough about graphics (or Graphics) objects.

However, if the colour of these Emojis is fixed, rather than supplyable at runtime, I'd say you want to get from your file format to some kind of "image" as fast as you can, since that's how you're likely to use them. If not, maybe there's an iterim stage (Graphics?) that allows you to "paint" your Emoji with whatever colours you like at runtime.

But to me, this should all be part of the Emoji class - ie, your GUI shouldn't have to worry about HOW to do any of this.

If I was writing a GUI program, I'd want to be able to create (or find) an Emoji object, then tell it how big I want it and what colours I want to use, and bingo, it returns an image (or Graphics object) that I can plug straight into my screen map.

But like I say, I'm NOT a GUI expert...

Winston
 
Johnny Januan
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to summarize a bit what I'm trying to do:

I've got a text file with numbers that represent different colours each.
I have to read the text file and save each of the numbers into a matrix (cEmot[][]).
The I want to read that matrix to check what value is in each position of the matrix, at the same time I want to create a Panel, which is formed by another matrix Square[][], where I will display squares in different colours depending on the value of that position that I'm checking in cEmot[][], if the value is 0 I will paint a white Square and so on.
I have the code that I posted in a reply above, but it doesn't work as I've already mentioned.

About your code, Winston: by reading it I understand what you want to do, but I don't see how that would open a window and etc.
As I said this program CAN'T be much different from the one of the chess board. I think I'm putting this code in the wrong place:

It's nside of this for: (the first one) that writes the file into cEmot[][]


Maybe this is the problem, I don't know. I'm completely new to Graphics as well, so could someone point me in the right direction?
 
Johnny Januan
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just saw what you posted Winston.

Yes, that's what I'm thinking about. I think the emoji method (the one that paints the emoji) should be separated from the method where I read the file.
Maybe if in that method where I read the file I return cEmot[][] and then pass it into the method Emoji?? Or not a good idea?
 
Piet Souris
Rancher
Posts: 1980
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it certainly has similarities to a BufferedImage (especially TYPE_BYTE_INDEXED ), but there are some very big dfferences.
This emoji idea is certainly worthwhile, but I am awaiting OP's latest ideas about this class.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnny Januan wrote:Yes, that's what I'm thinking about. I think the emoji method (the one that paints the emoji) should be separated from the method where I read the file.
Maybe if in that method where I read the file I return cEmot[][] and then pass it into the method Emoji?? Or not a good idea?

As long as it's all done inside the Emoji object, it shouldn't matter too much.

My only wonder is whether cEmot[][] should be a char[][].

chars are what you're getting from your file, which has very little to do with what an "Emoji" actually IS - either as an object, or in terms of information.

Each of those characters actually represents a colour; so I'd say convert them to a grid of Colors at the very least, so you have all the information you need to convert to an image at your fingertips. Characters are simply the way you're reading in the information; not the way you want it.

Winston
 
Johnny Januan
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's is a char[][] :

But again I ask, where should I put this: (it doesn't work where I have it now)



Am I checking reading cEmot[][] properly?


Thanks
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should encapsulate the drawing of your object into the object itself in a method called something like "public void draw(Graphics g, int x, int y)" so it will be able to draw itself onto any object's Graphics context. Once you have something like that done, then you can put a call to your image draw in your paintComponent of your display object.

[edit]
If you render your image onto a BufferedImage, of the appropriate size, then your draw body becomes:

[/edit]
Johnny Januan wrote:It's is a char[][] :

But again I ask, where should I put this: (it doesn't work where I have it now)



Am I checking reading cEmot[][] properly?


Thanks
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnny,
You end up with an class structured something like this:

Les
 
Piet Souris
Rancher
Posts: 1980
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would probably not use a BufferedImage.

The biggest disadvantage of the use of that is a matter of size. Compare the size of a BufferedImage of a Circle to that of a Shape of a Circle.

And changing colour would involve recreating this BufferedImage. Even when using a type_byte_index would not let you easily change color.

But then again: I've mentioned about changing colors in an easy way, but I don't know if that is important to OP.

 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:I would probably not use a BufferedImage.

The biggest disadvantage of the use of that is a matter of size. Compare the size of a BufferedImage of a Circle to that of a Shape of a Circle.

I would agree there, except in the case we are looking at we are using close packed pixels of varying color to generate form.

Piet Souris wrote:
And changing colour would involve recreating this BufferedImage. Even when using a type_byte_index would not let you easily change color.

No, not at all. the bi object is created as an instance variable so the life of the bi is the life of the object, to change color it's a very simple replacement using setRGB. The coloration of the object is set by a dynamic load, so therein possibly is my confusion, the image is loaded from a external file. In the scenario I presented, and usually implement, the image is only loaded once and can be directly rendered to the bi. The bi then is not only the image, but the needed matrix to hold the discrete color for each pixel. All that need be done is give an access point for the change by implementing a "changeColor(Color oColor, Color nColor)" method or a changePixel(Color nColor, int x, int y) method.
 
Paul Clapham
Sheriff
Posts: 22487
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:The biggest disadvantage of the use of that is a matter of size.


But according to the original post, it's a 16 x 16 grid of pixels being displayed. So I wouldn't consider size to be an issue in this particular case.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!