• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Graphics g.drawString  RSS feed

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here I have some code(shortened to show the most important parts)



I would like my g.drawString() to loop through my array called list which contains [B,G,G,B,B,B,B,G] and colour each value according to its colour based on my hashMap..

so my hashMap["G" = color.green, "B"=color.red]

I tried this :



but this seems to colour the whole text green or red could some one explain how to loop the the array called list and use g.drawString to colour each value accordingly?
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ouch! As I said earlier today elsewhere, such use of instanceof makes me suspect there is something wrong with your design.
You cannot apply colours to a String. It doesn't have colours. You can insert control sequences or HTML tags into a String, but that won't help you here.
You can paint a String a colour by setting the foreground colour for your Graphics Object. But you can't paint part of the String one colour and another part a different colour, so we are getting nowhere fast.
Start thinking object‑oriented. Put colours in the Person objects. Get their colours from them for use in display. Even better, give the Person objects a paint() method which takes the Graphics object as a parameter and sets its colour, too.
You will have to do something about font metrics, I think, because each Person object will have to have its name printed in a different location, so you will have to work out its size.
 
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Purely a guess because a lot of your code is missing. And, use of instanceof is smelly, i.e. probably a much better way to do it.

 
Wendy Jackson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the explanations i updated my code to use more OO approach so came out with something like this:


but am not sure how to access my values from the hashMap to use in public void paintComponent(). I came out with something like this:


but this gives an error. Any suggestions on how to loop and print through each value based on their respective colorRepresentation?
 
Master Rancher
Posts: 2045
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at line 4, where you use the variable 'u'.

Now that a Person has a 'getColorRepresentation()' method, do you think that the hashmap is still needed?

And what I would like to know: this list that must be printed, do you want it to be printed horizontally, with different colors, or do you want each element to be printed on a separate line?
 
Wendy Jackson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to print this list horizontally by using a hashMap, so that each character of the array has its values first stored in the hashmap (key, value pair) . so basically the idea is that each character in my array should be printed using g.drawString() and coloured according using their colour values from the hashmap.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wendy Jackson wrote:. . . by using a hashMap, so that each character of the array has its values first stored in the hashmap . . .
So this is an exercise in using Maps? If not, delete the words Hash‍Map and Map from your vocabulary. Also read this FAQ.

And since I was at school with a girl called Carol, please tell me what colours you are going to map to the three chars 'C' 'a' and 'l'.
 
Wendy Jackson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And since I was at school with a girl called Carol, please tell me what colours you are going to map to the three chars 'C' 'a' and 'l'


yes its about Maps. so basically if my array called  "list"[A,B,C] hashMaq with key value pairs [A=color.red, B=color.green, C=color.blue] respectively the g.drawString should print horizontally these values colored.
 
Piet Souris
Master Rancher
Posts: 2045
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, a somewhat contrived way is to use this paintComponent:

where lineNumber is a counter that determines on what line the list will be printed. I made a small demo:
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am afraid I don't understand the problem; PS is miles ahead of me in that respect.
You have created a List containing “randomly‑chosen” "R" and "G" and each of those makes a letter turn that colour. Isn't there supposed to be some relationship to the Person object, different colour for each sex, or something like that?
If you simply want randomly‑coloured letters, I don't understand why you can't do this:-That will obviously create a (25‑element) List<Color> which you can use to change the colour of the individual letters. You could create a Color[] array quite similarly. I don't know why you insist on using a Map, unless this is an exercise in using Maps.
 
Piet Souris
Master Rancher
Posts: 2045
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In OP's opening post the now famous map was created from the kind of Person in question. In OP's second reply the map is filled according to a new property in the Person class. That's why I asked if this map was still necessary. From the other replies, I gather that this is an exercise in Maps. Now, the given code snippets are too short to get a clear idea of the full background of this exercise, but I hope OP is helped a little further. If not, OP'll let us know.
 
Wendy Jackson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the explanation learnt a lot from them. I have posted more information below :

my array is made up of characters and each of them has its own colour as shown in the hashmap


where the string is my character in the array [A,B,C,D] so my hashMap is of the form (A=color.RED, B=color.Green, etc) that is key value pairs.



so the above method first gets my array which i have named "list" and also generates the hashMap called colorMap.

based on this information I created my protected void paintComponent(Graphics g) method to use g.drawString() to print each character in the array and colour it accordingly
so the output will be something similar to this ..

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!