简体   繁体   中英

I don't understand this Java code (bitmap Font)

I was analyzing a open-source game code and i don't understand setDefaultSpecialChars() method and setDefaultSmallAlphabet(). These statements fontCharTable[':']=1 + indexOf_Point; and fontCharTable['a'+i] = indexOf_a + i; are new to me.

 import javax.microedition.midlet.*;
 import javax.microedition.lcdui.*;

public class GFont {


public int[] fontCharTable = new int[256];

public void setFontCharTableDefaults(boolean specialChars) {
    setDefaultSmallAlphabet(0);
    setDefaultBigAlphabet(0);
    setDefaultDigits(27);
    if (specialChars) {
        setDefaultSpecialChars(37);
    }
}

public void setDefaultSpecialChars(int indexOf_Point) {
    fontCharTable['.']=0 + indexOf_Point;
    fontCharTable[':']=1 + indexOf_Point;
    fontCharTable[',']=2 + indexOf_Point;
    fontCharTable[';']=3 + indexOf_Point;
    fontCharTable['?']=4 + indexOf_Point;
    fontCharTable['!']=5 + indexOf_Point;
    fontCharTable['(']=6 + indexOf_Point;
    fontCharTable[')']=7  + indexOf_Point;
    fontCharTable['+']=8 + indexOf_Point;
    fontCharTable['-']=9 + indexOf_Point;
    fontCharTable['*']=10 + indexOf_Point;
    fontCharTable['/']=11 + indexOf_Point;
    fontCharTable['=']=12  + indexOf_Point;
    fontCharTable['\'']=13 + indexOf_Point;
    fontCharTable['_']=14 + indexOf_Point;
    fontCharTable['\\']=15 + indexOf_Point;
    fontCharTable['#']=16 + indexOf_Point;
    fontCharTable['[']=17 + indexOf_Point;
    fontCharTable[']']=18 + indexOf_Point;
    fontCharTable['@']=19 + indexOf_Point;
    fontCharTable['ä']=20 + indexOf_Point;
    fontCharTable['ö']=21 + indexOf_Point;
    fontCharTable['ü']=22 + indexOf_Point;
    fontCharTable['Ä']=fontCharTable['ä'];
    fontCharTable['Ö']=fontCharTable['ö'];
    fontCharTable['Ü']=fontCharTable['ü'];
}


public void setDefaultSmallAlphabet(int indexOf_a) {
    for (i=0; i<26; i++) {
        fontCharTable['a'+i] = indexOf_a + i;
    }
}


}

It's just a normal array element assignment expression - but using the implicit conversion from char to int . So take this:

fontCharTable['+']=8 + indexOf_Point;

Now consider it as:

char indexAsChar = '+';
int indexAsInt = indexAsChar; // Use implicit conversion
fontCharTable[indexAsInt] = 8 + indexOf_Point;

Is that now clearer?

Likewise this:

for (i=0; i<26; i++) {
    fontCharTable['a'+i] = indexOf_a + i;
}

could be written as:

for (i=0; i<26; i++) {
    int a = 'a';
    int index = a + i'
    fontCharTable[index] = indexOf_a + i;
}

They may look strange but all they are doing is using characters as indexes.

You could try stepping throuhg the code in your debugger if you want to see what they are doing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM