Re: Getting text's pixels
I would consider using a JEditorPane for this and setting the text color via attributes.
Re: Getting text's pixels
Quote:
Originally Posted by
Fubarable
I would consider using a JEditorPane for this and setting the text color via attributes.
Well, I'm using a Canvas to display text as I am trying to create a game. I am rendering everything using a pixel array and that's why I need the pixels.
Here is my progress at the moment:
Code:
public void drawText(String text, int x, int y){
if(text == null)
return;
int color = 0;
int xx = x;
for(int i = 0; i < text.length(); i++){
if(text.charAt(i) == '@' && i + 4 < text.length() && text.charAt(i + 4) == '@'){
color = getColor(text.substring(i + 1, i + 4));
if(color == -1){
color = 0xFFF;
}
i+=4;
continue;
}
char c = text.charAt(i);
int r = (color & 0xff0000) >> 16;
int g = (color & 0xff00) >> 8;
int b = (color & 0xff);
screen.g.setColor(new Color(r, g, b));
screen.g.drawString(c + "", xx, y);
xx+= font.getSize();
//System.out.println(Arrays.toString(tl.getBaselineOffsets()));
}
screen.g.drawString(text, 100, 200);
}
Something like this would work, but it doesn't align the chars properly and this makes me feel like I am cheating because I'm using drawString() as my goal of creating this game is to learn how to play with pixels.
Re: Getting text's pixels
You might want to take a look at TextLayout.
db
Re: Getting text's pixels
Re: Getting text's pixels
Moderation: spam post deleted.
Re: Getting text's pixels
Quote:
Originally Posted by
DarrylBurke
You might want to take a look at TextLayout.
db
Should I look at this for aligning the chars correctly or to get the pixels? Or both?
I have no idea what to do with it, I have tried aligning the chars with it, but it makes them look weird, I have also tried getting the pixels of the chars but everything that I've tried didn't work :/
If it's really such a fuss getting the pixels of char then, I think I will just use g.drawString(), because all the pixel stuff is too confusing. :(
Would it be more efficient to use g.drawString instead of drawing using pixels to draw text?
But I think it would be better to use pixels as then I could add different effects and stuff.
Re: Getting text's pixels
Quote:
Originally Posted by
PhQ
... then I could add different effects and stuff.
You can add quite a bit of 'effects and stuff' by leveraging the Font API. More specifically, the Font#deriveFont(Map<? extends AttributedCharacterIterator.Attribute,?> attributes) method. Go to the API for java.awt.font.TextAttribute to see what attributes can be used. Take special note that Background/Foreground can be Paint instances, you're not limited to Colors.
db
Re: Getting text's pixels
Quote:
Originally Posted by
DarrylBurke
You can add quite a bit of 'effects and stuff' by leveraging the Font API. More specifically, the Font#deriveFont(Map<? extends AttributedCharacterIterator.Attribute,?> attributes) method. Go to the API for java.awt.font.TextAttribute to see what attributes can be used. Take special note that Background/Foreground can be Paint instances, you're not limited to Colors.
db
Woah! Thank you! This works great for me!
Re: Getting text's pixels
Ok, after an hour of messing with AttributedString I've made this:
Code:
class ColorAttribute{
public final int index;
public final Color color;
public ColorAttribute(Color color, int index){
this.color = color;
this.index = index;
}
}
public void drawText(String text, int x, int y) {
if (text == null) return;
int color = 0;
ArrayList<ColorAttribute> attr = new ArrayList<ColorAttribute>();
StringBuilder str = new StringBuilder();
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) == '@' && i + 4 < text.length() && text.charAt(i + 4) == '@') {
color = getColor(text.substring(i + 1, i + 4));
if (color == -1) {
color = 0xFFF;
}
i += 4;
int r = (color & 0xff0000) >> 16;
int g = (color & 0xff00) >> 8;
int b = (color & 0xff);
attr.add(new ColorAttribute(new Color(r, g, b), str.length()));
continue;
}
str.append(text.charAt(i));
}
AttributedString as1 = new AttributedString(str.toString());
for(ColorAttribute ca : attr){
as1.addAttribute(TextAttribute.FOREGROUND, ca.color, ca.index, str.length());
}
screen.g2d.drawString(as1.getIterator(), 15, 60);
}
Would it be possible to improve this code? Or make it faster? Or is it fine?