Results 1 to 10 of 10
Thread: Getting text's pixels
- 12-24-2012, 02:18 AM #1
Getting text's pixels
Hi, I am working on a little game, and I was wondering how can I get the pixels of text?
I need them because I want it so if I use a String like this: "@gre@Hello @red@World" it would turn up like this: Hello World. Here is what I have so far:
I'm not sure where to go from there.Java Code:public void drawText(String text, int x, int y){ if(text == null) return; int color = 0; 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); } } private int getColor(String s) { switch (s) { case "whi": return 0xffffff; case "red": return 0xFF0000; case "gre": return 0x00FF00; case "blu": return 0x0000FF; default: return -1; } }
I know that I can just do g.drawString();, but is it possible to get an array of pixels?Last edited by PhQ; 12-24-2012 at 02:58 AM.
-
Re: Getting text's pixels
I would consider using a JEditorPane for this and setting the text color via attributes.
- 12-24-2012, 04:44 AM #3
Re: Getting text's pixels
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:
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.Java 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); }Last edited by PhQ; 12-24-2012 at 04:50 AM.
- 12-24-2012, 08:29 AM #4
Re: Getting text's pixels
You might want to take a look at TextLayout.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 12-25-2012, 01:31 AM #5
Re: Getting text's pixels
seems legit
-
Re: Getting text's pixels
Moderation: spam post deleted.
- 12-25-2012, 07:12 PM #7
Re: Getting text's pixels
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.Last edited by PhQ; 12-25-2012 at 07:18 PM.
- 12-25-2012, 08:08 PM #8
Re: Getting text's pixels
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 12-25-2012, 11:13 PM #9
- 12-26-2012, 01:49 AM #10
Re: Getting text's pixels
Ok, after an hour of messing with AttributedString I've made this:
Would it be possible to improve this code? Or make it faster? Or is it fine?Java 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); }
Similar Threads
-
help with some pixels
By HelloWorld1234 in forum New To JavaReplies: 13Last Post: 07-09-2012, 12:58 AM -
Pixels
By shakeel in forum Java 2DReplies: 0Last Post: 03-01-2011, 11:32 AM -
Decimal Pixels?
By Jcbconway in forum AWT / SwingReplies: 0Last Post: 11-29-2010, 09:25 PM -
Pixels and Color
By tenis_09 in forum Java 2DReplies: 3Last Post: 07-12-2010, 11:29 PM -
Counting Pixels
By shaungoater in forum Java 2DReplies: 5Last Post: 11-29-2007, 05:51 PM


3Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks