1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Is it possible to replace a character in a string with a polygon?

Discussion in 'Software Development' started by ElementOfMagic, Feb 2, 2013.

Thread Status:
Not open for further replies.
Advertisement
  1. ElementOfMagic

    ElementOfMagic Thread Starter

    Joined:
    Dec 30, 2012
    Messages:
    52
    Hello. :)

    I was wondering if it was possible to replace a character with a polygon, in any instance of that character's occurrence in a string?
    For example, if I typed-in the word 'Holly' and hit 'enter', the first letter would be replaced by the polygon.
    If I then went to type the word 'thistle', the polygon's new position would be in place of the second letter?

    This is my code fragment, the character I want to replace is called sl1.

    Code:
    if(pr_word.indexOf(sl1)!=-1)
    		{
    		   pr_word = pr_word.replace(sl1, ' ');
    		   g.setColor(Color.red);
    		   g.fillPolygon(xh,yh,5);
    		   
    		   g.setColor(Color.white);
    		   g.fillRect(352,40,18,20);
    		}
    
    Any help/guidance will be greatly appreciated. :) (y)
     
  2. jdean

    jdean

    Joined:
    Jan 20, 2002
    Messages:
    433
    If your string is stored in Unicode, you can replace a character with a Unicode character. There are many Unicode characters, including some that are geometric shapes, check out this reference for starters: http://en.wikipedia.org/wiki/Geometric_Shapes

    However this won't give you the ability to change the color. Without going into too much detail, your question is mixing two different concepts. One concept is a string, which is a series of characters. The other concept is the display of characters on a display device, which is a more complex subject and depends on the windowing system.

    While a string can be rendered onto a display device, the string itself has no information or knowledge of where the characters are displayed, what font is used, what color is used, etc. So in your code fragment, there is no way to determine the coordinates. Also, you code can't know the size of the font, so you won't be able to determine the size of the polygon.
     
  3. ElementOfMagic

    ElementOfMagic Thread Starter

    Joined:
    Dec 30, 2012
    Messages:
    52
    Thanks, but it's not a Unicode character I want to replace the character in the string with - it's the polygon I 'drew' using the fillPolygon() method (it is mentioned in my code ^^).
     
  4. jdean

    jdean

    Joined:
    Jan 20, 2002
    Messages:
    433
    When I answer a question, I try to understand the writer's intent, so that I can try to be helpful even if the question is worded in a way that makes it difficult to provide a good answer. That's the reason I mentioned the Unicode approach, even though I knew it was a long shot. The other two paragraphs that I wrote attempted to provide a bit more explanation.

    The simple and precise answer to your question is "No, It is not possible to replace a character in a string with a polygon". The reason is that a string can only contain characters. A polygon is not a character.
     
  5. loserOlimbs

    loserOlimbs

    Joined:
    Jun 19, 2004
    Messages:
    7,800
    Though you could probably approximate it as far as the way the end result goes. I can't see it being practical... but the logic would probably be something like:

    Code:
    varXY = label.XY
    char[] FindChar = stringToPlayWith
    
    indexOfChar toReplace = replace(' ")
    
    callMethodForPoly(varXY, IndexOfChar)
    
    callMethodForPoly{
    x = varX.X + (width of chars * indexOfChar)
    y = varXY.Y
    
    PolyPaintMethod(left = X, top = Y, height = Char.Height, width = Char.width)
    }
    
    
    That is in no way Java, btw. I just wanted to give you something to think about for a way to do it. Honestly though, I can't see that this would ever be practical in a desktop app.
     
  6. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1087928

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice