# 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

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?

#### andythepandy

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

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?

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

#### andythepandy

Could you post the whole code so I can try and compile it myself please

#### ElementOfMagic

All I need to know really is how to add each character's width to the x-coordinate of the polygon I'm drawing.

#### andythepandy

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

#### andythepandy

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

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.

Is there anything specific I need to do to declare height?

#### andythepandy

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

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.

#### ElementOfMagic

Would it help if I posted my code again, along with the changes I've made?

#### andythepandy

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

#### andythepandy

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

Thank-You, that's all I need.

#### ElementOfMagic

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.

As Seen On