This site requires JavaScript, please enable it in your browser!
Greenfoot back
BlackNova47
BlackNova47 wrote ...

2017/2/7

New Font/Color (Need help re-coding)

BlackNova47 BlackNova47

2017/2/7

#
I haven't coded in awhile and I reloaded some of my old code after updating Greenfoot. As you can imagine my color/font code no longer works (Incompatible types. I'd rather use the update Greenfoot for future coding but I'm rusty to everything and don't know how I would approach coding the color and font again in the new format. Here is the code for my on screen text:
 public void createImage(){
        text = text+"\n";
        String[] lines = text.split("\r\n|\r|\n");
        int longestString = longestSringLength(lines);
        image = new GreenfootImage((int)((size * longestString) /1.6),(lines.length * size) + size +5);
        image.setColor(new Color(255,255,255, 128));
        image.fillRect(0, 0, (int)((size * longestString)/1.6+size), (lines.length * size) + size + 5);
        image.setColor(new Color(0, 0, 0, 128));
        image.fillRect(5, 5, (int)((size * longestString)/1.6-10), ((lines.length * size)+ (size -5)));
        
        font = new Font("courier",0,size);
        font = font.deriveFont((float)size);
        image.setFont(font);
        image.setColor(new Color(255,255,255, 255));
        font = new Font("courier",0,size);
        for(int x = 0;x < lines.length;x++)
        {
            image.drawString(lines[x],10,(size * x) + (size + 5));
        }
        
    }
Super_Hippo Super_Hippo

2017/2/7

#
Remove the 0 in lines 11 and 15. I don't see a problem with Color there. Best thing would be if you could point at the lines where the errors appear.
danpost danpost

2017/2/7

#
Are you sure that "courier" is a valid font name ... try "Courier New" or "Courier Bold". You can check the valid font names with my TextImage class 'listFonts' method. See my Support Classes collection for the scenario that contains it. Just download the scenario and open it up in greenfoot; then manually execute the method (right click on the class icon and select the method from the drop down list) to view the valid names.
BlackNova47 BlackNova47

2017/2/8

#
It worked! I removed the 0's and changed the "courier" to "Courier Bold" (And made similar adjustments to other classes that used the old method) Thank you both for your help!
MrBradley MrBradley

2017/2/9

#
Also to note one of the new (greenfoot) Font constructors is Font ( String FontName , boolean bold , boolean italic , int size ) Its a good idea to reference the online api. :)
Nosson1459 Nosson1459

2017/2/16

#
danpost wrote...
Are you sure that "courier" is a valid font name ... try "Courier New" or "Courier Bold". You can check the valid font names with my TextImage class 'listFonts' method. See my Support Classes collection for the scenario that contains it. Just download the scenario and open it up in greenfoot; then manually execute the method (right click on the class icon and select the method from the drop down list) to view the valid names.
When I do:
setFont(new Font("font not really a font on my PC", Font.PLAIN, 16));
I don't get any errors and the program runs fine, shouldn't it tell me that there is no such font? Also, if I use a font that really is on my computer it will show it fine but what will happen if the person running the program doesn't have that font will there be an error or will the font automatically get downloaded to that person's computer, or neither somehow?
danpost danpost

2017/2/16

#
Nosson1459 wrote...
When I do:
setFont(new Font("font not really a font on my PC", Font.PLAIN, 16));
I don't get any errors and the program runs fine, shouldn't it tell me that there is no such font?
That method is in the GreenfootImage class and not related to my TextImage class. Therefore, I cannot rightly say without looking at the GreenfootImage class source code.
Also, if I use a font that really is on my computer it will show it fine but what will happen if the person running the program doesn't have that font will there be an error or will the font automatically get downloaded to that person's computer, or neither somehow?
I am sure the font will not automatically download to that person's computer. If you did not get an error with the above, then I doubt that someone else will (using the same version of greenfoot). It probably just does not change the font currently set to the image (which is something you can test easily enough).
Nosson1459 Nosson1459

2017/2/17

#
danpost wrote...
Nosson1459 wrote...
When I do:
setFont(new Font("font not really a font on my PC", Font.PLAIN, 16));
I don't get any errors and the program runs fine, shouldn't it tell me that there is no such font?
That method is in the GreenfootImage class and not related to my TextImage class. Therefore, I cannot rightly say without looking at the GreenfootImage class source code.
In your first post you wrote "Are you sure that "courier" is a valid font name ... try "Courier New" or "Courier Bold".", but by me the name didn't make a difference, that is my question.
Also, if I use a font that really is on my computer it will show it fine but what will happen if the person running the program doesn't have that font will there be an error or will the font automatically get downloaded to that person's computer, or neither somehow?
I am sure the font will not automatically download to that person's computer. If you did not get an error with the above, then I doubt that someone else will (using the same version of greenfoot). It probably just does not change the font currently set to the image (which is something you can test easily enough).
When I say downloaded I mean automatically go along with the program somehow, so that the program still uses that font even if the user didn't/doesn't have it.
danpost danpost

2017/2/17

#
Nosson1459 wrote...
In your first post you wrote "Are you sure that "courier" is a valid font name ... try "Courier New" or "Courier Bold".", but by me the name didn't make a difference, that is my question.
I was asking the question without any personal testing. I was no sure about it myself.
When I say downloaded I mean automatically go along with the program somehow, so that the program still uses that font even if the user didn't/doesn't have it.
I think I answered that with
I am sure the font will not automatically download to that person's computer.
If the program does not have access to that font, then you cannot except that font to be used by the program.
Nosson1459 Nosson1459

2017/2/17

#
Thanks for the help.
You need to login to post a reply.