Results 1 to 10 of 10
Like Tree3Likes
  • 1 Post By Fubarable
  • 2 Post By DarrylBurke

Thread: Getting text's pixels

  1. #1
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default 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:
    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'm not sure where to go from there.
    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 03:58 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Getting text's pixels

    I would consider using a JEditorPane for this and setting the text color via attributes.
    DarrylBurke likes this.

  3. #3
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Getting text's pixels

    Quote Originally Posted by Fubarable View Post
    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:
    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);
    		
    	}
    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.
    Last edited by PhQ; 12-24-2012 at 05:50 AM.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: Getting text's pixels

    You might want to take a look at TextLayout.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Getting text's pixels

    seems legit

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  7. #7
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Getting text's pixels

    Quote Originally Posted by DarrylBurke View Post
    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.
    Last edited by PhQ; 12-25-2012 at 08:18 PM.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: Getting text's pixels

    Quote Originally Posted by PhQ View Post
    ... 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
    PhQ and Fubarable like this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Getting text's pixels

    Quote Originally Posted by DarrylBurke View Post
    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!

  10. #10
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Getting text's pixels

    Ok, after an hour of messing with AttributedString I've made this:
    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);
    	}
    Would it be possible to improve this code? Or make it faster? Or is it fine?

Similar Threads

  1. help with some pixels
    By HelloWorld1234 in forum New To Java
    Replies: 13
    Last Post: 07-09-2012, 01:58 AM
  2. Pixels
    By shakeel in forum Java 2D
    Replies: 0
    Last Post: 03-01-2011, 12:32 PM
  3. Decimal Pixels?
    By Jcbconway in forum AWT / Swing
    Replies: 0
    Last Post: 11-29-2010, 10:25 PM
  4. Pixels and Color
    By tenis_09 in forum Java 2D
    Replies: 3
    Last Post: 07-13-2010, 12:29 AM
  5. Counting Pixels
    By shaungoater in forum Java 2D
    Replies: 5
    Last Post: 11-29-2007, 06:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •