Solved: Draw an arbitrary polygon in place of each occurrence of a char in a String?

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

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?
 
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
 

ElementOfMagic

Thread Starter
Joined
Dec 30, 2012
Messages
52
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
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?
 

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.
 
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);
 
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
 

ElementOfMagic

Thread Starter
Joined
Dec 30, 2012
Messages
52
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
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?
 
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
 

ElementOfMagic

Thread Starter
Joined
Dec 30, 2012
Messages
52
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
Okay, it now compiles without errors, but the polygon is still only being drawn in the first instance of the special character. :confused:
 
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 :)
 
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
 

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. ^^;;
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

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 807,865 other people just like you!

Latest posts

Staff online

Top