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: Draw an arbitrary polygon in place of each occurrence of a char in a String?

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

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

    ElementOfMagic Thread Starter

    Joined:
    Dec 30, 2012
    Messages:
    52
    This is merely a general question more than anything, but nonetheless, it's still one that I would very much appreciate an answer to.

    When creating a Java applet, is it possible to Draw an arbitrary polygon (i.e. one drawn using drawPolygon()) in place of each occurrence of a certain character in a charArray?
     
  2. andythepandy

    andythepandy

    Joined:
    Jul 2, 2006
    Messages:
    379
    It is certainly possible, it's just how you want to do it. For instance you could have (pseudo code):
    Code:
    for(int i = 0; i < lengthOf(string); i++) {
        if(string.charAt(i) == 'c')
            drawPolygon(x, y);
        else
            drawCharacter(string.charAt(i));
    }
    
    You'll need to find the x coordinate of where the polygon should be drawn by knowing the width of each letter and multiplying that by how far into the string you are
     
  3. ElementOfMagic

    ElementOfMagic Thread Starter

    Joined:
    Dec 30, 2012
    Messages:
    52
    I do have something a bit similar to that, but for some reason the line where I draw the polygon is giving me a 'cannot find symbol' error:

    Code:
    if(pr_word.indexOf(sl2)!=-1)
    		{
    		   pr_word = pr_word.replace(sl2, ' ');
    		   g.setColor(Color.red);
    		   
    		   
    		   for(int i = 0; i < letters.length; i++)
            {    	
            	String s = (new StringBuilder()).append("").append(letters[i]).toString();
                    if(str.charAt(i) == 'b' || str.charAt(i) == 'B')
                    {
                int[]xb={0+xcord+sl2,0+xcord+sl2,0+xcord+sl2,0+xcord+sl2,10+xcord+sl2,0+xcord+sl2,0+xcord+sl2,0+xcord+sl2,0+xcord+sl2};
            	int[]yb={0+ycord+sl2,10+ycord+sl2,20+ycord+sl2,30+ycord+sl2,10+ycord+sl2,10+ycord+sl2,20+ycord+sl2,30+ycord+sl2,30+ycord+sl2};
            		[COLOR="Red"]g.drawPolygon(xb+fm.charWidth('b')*xcord,yb,5);[/COLOR]
            		}
            }
    
    I have imported both the Font and FontMetrics packages into the program, but they don't seem to be the cause of the error.
    I'm not sure what else I can try? :confused: :(

    Btw, how can I know what the width of each letter is?
     
  4. andythepandy

    andythepandy

    Joined:
    Jul 2, 2006
    Messages:
    379
    Could you post the whole code so I can try and compile it myself please
     
  5. ElementOfMagic

    ElementOfMagic Thread Starter

    Joined:
    Dec 30, 2012
    Messages:
    52
    All I need to know really is how to add each character's width to the x-coordinate of the polygon I'm drawing.
     
  6. andythepandy

    andythepandy

    Joined:
    Jul 2, 2006
    Messages:
    379
    You could have a variable that counts the length of the characters in the string as it goes along. Something like this maybe:

    Code:
    int runningTotal = 0;
    
    ...
    
    for(int i = 0; i < letters.length;i++) {
        runningTotal += fm.charWidth(letters[i]);
        thisWidth = fm.charWidth(letters[i]);
        
        ....
        
        //when you need to draw the polygon, you can use the running total + your starting point to find where to draw the polygon
        //something like this:
        g.drawPolygon(new int[]{startX+runningTotal, startX+runningTotal+thisWidth, startX+runningTotal+thisWidth,startX+runningTotal}, new int[]{startY, startY+height, startY, startY+height}, 4);
    
     
  7. andythepandy

    andythepandy

    Joined:
    Jul 2, 2006
    Messages:
    379
    Yes I made some new variables in my block of code that will need declaring / replacing with variables you have already. I.e. you'll need to decalre thisWidth, startX, height etc
     
  8. ElementOfMagic

    ElementOfMagic Thread Starter

    Joined:
    Dec 30, 2012
    Messages:
    52
    Okay, that seemed to work on all variables except height.
    For that, I'm getting a 'height is not public in Component; cannot be accessed from outside package' error. :confused:

    Is there anything specific I need to do to declare height?
     
  9. andythepandy

    andythepandy

    Joined:
    Jul 2, 2006
    Messages:
    379
    No there shouldn't be, it's just a normal variable. You can always just replace all instances of height with a number for testing it. height represents the height of the polygon you want to draw in my example
     
  10. ElementOfMagic

    ElementOfMagic Thread Starter

    Joined:
    Dec 30, 2012
    Messages:
    52
    Okay, it now compiles without errors, but the polygon is still only being drawn in the first instance of the special character. :confused:
     
  11. ElementOfMagic

    ElementOfMagic Thread Starter

    Joined:
    Dec 30, 2012
    Messages:
    52
    Would it help if I posted my code again, along with the changes I've made?
     
  12. andythepandy

    andythepandy

    Joined:
    Jul 2, 2006
    Messages:
    379
    Yes please. The code at the moment is very bulky and unclear for what its doing so if you post your code I can edit it and comment it to explain whats going on :)
     
  13. andythepandy

    andythepandy

    Joined:
    Jul 2, 2006
    Messages:
    379
    Here is a neater version with lots of comments that (hopefully) should explain everything

    Code:
    		
    		//loop through all the characters in the char array
    		for(int i = 0; i < letters.length; i++) {
    			
    			//find the width of the current char (+10 so thers a space between letters)
    			thisWidth = fm.charWidth(letters[i]) + 10;
    			
    			//add this to the running total so we know how far through the string we are
    			runningTotal += thisWidth;
    			
    			//define the (x, y) coords of the polygon
    			int x1, x2, y1, y2;
    			//first x coord
    			x1 = xcord + runningTotal;
    			//second x coord (-10 because we added 10 earlier)
    			x2 = x1 + thisWidth - 10;
    			
    			//first y coord
    			y1 = ycord - height;
    			//second y coord
    			y2 = y1 + height;
    			
    			//check if the current letter is either sl1 or sl2
    			if(letters[i] == sl1 || letters[i] == sl2) {
    				//if it is, define the coord arrays then draw the polygon
    				xh = new int[] {x1, x2, x2, x1};
    				yh = new int[] {y1 - height, y1 - height, y2 - height, y2 - height};
    				g.drawPolygon(xh, yh, 4);
    			}
    			else
    				//if it's not the character we want, just draw the character
    				g.drawChars(new char[] {letters[i]}, 0, 1, x1, y1);
    		}
    	}
    
    EDIT: Removed full code but left the important bit so other people can still find something useful from it
     
  14. ElementOfMagic

    ElementOfMagic Thread Starter

    Joined:
    Dec 30, 2012
    Messages:
    52
    Thank-You, that's all I need. :)
     
  15. ElementOfMagic

    ElementOfMagic Thread Starter

    Joined:
    Dec 30, 2012
    Messages:
    52
    Would it be okay for me to ask you to remove the full code from your message?
    I just don't want other people using it without my permission. ^^;;
     
  16. 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/1088625

  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