Java tutorials

Playing With Text, Java Applet

“Play­ing With Text, Java Applet” is an Java Applet pro­ject that I have done for my pro­gram­ming sub­ject in first year of my study. The “Play­ing With Text, Java Applet” pro­ject let the user to enter a text and it will draw the text in dif­fer­ent man­ners such as diag­on­ally, around a circle, around a square and even there is an abil­ity to make the text animated.

To see the “Play­ing With Text, Java Applet” demo click here

Play­ing With Text, Java Applet Manual

* **********************Manual********************
* Enter Text: This is a text field for user to enter a word or a sen­tence
* Style of Draw­ing: this is a text field uses to indic­ate style of draw­ing from list below
* 1– ‘d’: Draw the entered text ver­tic­ally
* 2– ‘i’: Draw the entered text in zig­zag man­ner
* 3– ‘s’: Draw the entered text around a scal­able square
* 4– ‘c’: Draw the entered text around a scal­able circle
* 5– ‘t’: Draw the entered text around a scal­able tri­angle
* Lar­ger: This is a but­ton that makes the entered text lar­ger in terms of font size
* Smal­ler: This is a but­ton that makes the entered text smal­ler in terms of font size
* Anim­ate: This is a but­ton that makes the entered text to be anim­ated in man­ner the font size get lar­ger and smal­ler auto­mat­ic­ally
* Dis­play All Shapes: It will retain all text has been entered by user with their size, loc­a­tion and style of draw­ing and will dis­play them.
* Clear Screen: This is a but­ton that clear the screen
* His­tory: This is a text area that records and dis­plays inform­a­tion about the entered text such as the text, the text size, the style of draw­ing and the x,y coordin­a­tions
* ******************Ends of Manual*****************


<div>/** This is an applet application which is an assignment for Programming Module in first year study of Multimedia and Web Computing at Manchester Metropolitan University
?* @author Parham Majdabadi
?* @version 23nd Feb 2013
?*//**
* **********************Manual********************
* Enter Text: This is a text field for user to enter a word or a sentence
* Style of Drawing: this is a text field uses to indicate style of drawing from list below
* 1- 'd': Draw the entered text vertically
* 2- 'i': Draw the entered text in zigzag manner
* 3- 's': Draw the entered text around a scalable square
* 4- 'c': Draw the entered text around a scalable circle
* 5- 't': Draw the entered text around a scalable triangle
* Larger: This is a button that makes the entered text larger in terms of font size
* Smaller: This is a button that makes the entered text smaller in terms of font size
* Animate: This is a button that makes the entered text to be animated in manner the font size get larger and smaller automatically
* Display All Shapes: It will retain all text has been entered by user with their size, location and style of drawing and will display them.
* Clear Screen: This is a button that clear the screen
* History: This is a text area that records and displays information about the entered text such as the text, the text size, the style of drawing and the x,y coordinations
* ******************Ends of Manual*****************
*/
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.awt.Graphics;
public class Majdabadi_Parham_Seven extends Applet implements MouseListener, ActionListener, Runnable {

//declaring public variable for the class
Label enterText, enterStyle;
TextField text1, text2;
TextArea drawingDetails;
Button button1, button2, restart, animate, retainsAll;
String phrase, style, styleOfDrawing, showingDetails;
final char sl1 = 'p';
final char sl2 = 'm';
final char sl1C = 'P';
final char sl2C = 'M';
Font font;
int startPointX, startPointY, whichStyle, scale, length, j, mouseX, mouseY;
int phraseLength, phraseCounter;
String [] phraseCoordinates, allPhrases, allStylesOfDrawing;
int [] allStartPointX, allStartPointY, allScales;
Thread runner;
boolean threadSuspended, isMouseClick, showAllShapes;
//Methods
public void init()
//pre:
//post: adds text-fields, labels, buttons and mouse listener to applet
{
enterText = new Label("Enter Text");
enterStyle = new Label("Style of Drawing");
text1 = new TextField(10);
text2 = new TextField(2);
drawingDetails = new TextArea(" ",6,70);
button1 = new Button("Larger");
button2 = new Button("Smaller");
restart = new Button("Clear Screen");
animate = new Button("Animate");
retainsAll = new Button("Display All Shapes");
add(enterText);
add(text1);
add(enterStyle);
add(text2);
add(button1);
add(button2);
add(animate);
add(retainsAll);
add(restart);
add(drawingDetails);
text1.addActionListener(this);
text2.addActionListener(this);
button1.addActionListener(this);
button2.addActionListener(this);
animate.addActionListener(this);
restart.addActionListener(this);
retainsAll.addActionListener(this);
addMouseListener(this);
}

public void start() {
//pre:
//post: Set size, background Colour, font and also initialise some of public variables
setSize(800,600);
setBackground(Color.LIGHT_GRAY);
phrase = "";
style = "";
startPointX = -10;
startPointY = -10;
scale = 1;
length = 0;
showingDetails = "";
phraseLength = 0;
styleOfDrawing = "";
drawingDetails.setText("");
j = 1;
allPhrases = new String[50];
allStylesOfDrawing = new String[50];
allStartPointX = new int[50];
allStartPointY = new int[50];
allScales = new int[50];
phraseCounter = 0;
mouseX = -5;
mouseY = -5;
}

public void run() {
//pre: Executes once animate button has been clicked
//post: Create a loop for the animation and increase scale of font size by 1 each time. The loop continues till threadSuspended is false
try {
while (true) {
if (isMouseClick == false) {
startPointX = getSize().width / 2;
startPointY = getSize().height / 2;
}
if (j <= 4){
scale +=1;
} else if (j > 4) {
scale -=1;
}
// Here's where the thread does some work
++j; // this is shorthand for "j = j+1;"
if ( j == 10 ) {
j = 1;
}

// Now the thread checks to see if it should suspend itself
if ( threadSuspended ) {
synchronized( this ) {
while ( threadSuspended ) {
wait();
}
}
}
repaint();
// interval given in milliseconds
runner.sleep(1000);
}
}
catch (InterruptedException e) { }
}

public void stop() {
//pre:
//post:stop the animation by setting threadSuspended to true
threadSuspended = true;
}

public void paint(Graphics g) {
//pre: g is valid graphics object
//post: draws the text as well as the polygons if they are applicable either at recorded mouse location or the default location. apply the font. also check if it must draws all the texts
if(showAllShapes == false) {
phraseLength = phrase.length();
phraseCoordinates = new String[phrase.length()];
//set a max and min for font size scale
if(scale <= 1) {
scale = 1;
} else if (scale >= 7){
scale = 7;
}
//initialise font variable to be scalable
font = new Font("TimesRoman", Font.PLAIN, (10 + (scale * 14)));
g.setFont(font);
//check and assign new value for starting point of the text
if(startPointX == -10 && startPointY == -10) {
startPointX = getSize().width / 2;
startPointY = getSize().height / 2;
}
//if the phrase was empty ignore the for loop
if(phrase != "") {
switch(whichStyle)
{
case (1): {
//calling method displayVertically to draw the text vertically
displayVertically(g);
break;
}
case (2): {
//calling method displayDiagonally to draw the text diagonally
displayDiagonally(g);
break;
}
case (3): {
//calling method displayZigzag to draw the text in zigzag manner
displayZigzag(g);
break;
}
case (4): {
//calling method displaySquare to draw the text around a square
displaySquare(g);
break;
}
case (5): {
//calling method displayCircle to draw the text around a circle
displayCircle(g);
break;
}
case (6): {
//calling method displayTraiangel to draw the text around a triangle
displayTriangle(g);
break;
}

}
//add new info about the text which was displayed to the text area
drawingDetails.setText(showingDetails);
}
} else if(showAllShapes) {
//the user clicked on Display all shapes button. Make a loop through the arrays(allPhrases, allScales, allStartPointX,Y and allStyleOFDrawing to retain required information
for (int i = 0; i < phraseCounter; i++) {
if(allPhrases[i] != "") {
phraseLength = allPhrases[i].length();
//set a max and min for font size scale
if(allScales[i] <= 1) {
allScales[i] = 1;
} else if (allScales[i] >= 7){
allScales[i] = 7;
}
//initialise font variable to be scalable
font = new Font("TimesRoman", Font.PLAIN, (10 + (allScales[i] * 14)));
g.setFont(font);
//set the value of the scale to current scale, to make the Polygons sizable.
scale = allScales[i];
//check and assign new value for starting point of the text

startPointX = allStartPointX[i];
startPointY = allStartPointY[i];

if(allStylesOfDrawing[i] != "") {
switch(allStylesOfDrawing[i])
{
case ("Vertically"): {
//calling method displayVertically to draw the text vertically
displayVertically(g);
break;
}
case ("Diagonally"): {
//calling method displayDiagonally to draw the text diagonally
displayDiagonally(g);
break;
}
case ("Zigzag"): {
//calling method displayZigzag to draw the text in zigzag manner
displayZigzag(g);
break;
}
case ("Square"): {
//calling method displaySquare to draw the text around a square
displaySquare(g);
break;
}
case ("Circle"): {
//calling method displayCircle to draw the text around a circle
displayCircle(g);
break;
}
case ("Triangle"): {
//calling method displayTraiangel to draw the text around a triangle
displayTriangle(g);
break;
}

}
}

}
}
showAllShapes = false;
}
}

public void mouseClicked(MouseEvent e) {
//pre: event driven by mouse click
//post: get mouse location and then repaint
isMouseClick = true;
startPointX = e.getX();
startPointY = e.getY();
mouseX = startPointX;
mouseY = startPointY;
//add one to phraseCounter for indexing issues
phraseCounter ++;
//add values to the arrays
allPhrases[phraseCounter-1] = phrase;
allStylesOfDrawing[phraseCounter-1] = styleOfDrawing;
allStartPointX[phraseCounter-1] = startPointX;
allStartPointY[phraseCounter-1] = startPointY;
allScales[phraseCounter-1] = scale;
repaint();

}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}

public void actionPerformed(ActionEvent e) {
//pre: event driven by any component action
//post: take the user inputs through the text fields and buttons
//check if the Display all shapes has been clicked
if (e.getSource() == retainsAll) {
showAllShapes = true;
repaint();
} else {
//add one to phraseCounter for indexing issues
phraseCounter ++;
showStatus("");
//taking the word/sentence from text1
phrase = text1.getText();
//taking the style of drawing from text2
style = text2.getText();
//add a new value to allPhrases array
allPhrases[phraseCounter-1] = phrase;
//check which style of drawing has been chosen and set the whichStyle variable to applicable number
if (style.equals("d") || style.equals("D")) {
whichStyle = 1;
showStatus("Written Vertically");
styleOfDrawing = "Vertically";
repaint();
} else if (style.equals("i") || style.equals("I")) {
whichStyle = 2;
showStatus("Written Diagonally");
styleOfDrawing = "Diagonally";
repaint();
} else if(style.equals("z") || style.equals("Z")) {
whichStyle = 3;
showStatus("Written in Zigzag Manner");
styleOfDrawing = "Zigzag";
repaint();
} else if(style.equals("s") || style.equals("S")) {
whichStyle = 4;
showStatus("Written around a Square");
styleOfDrawing = "Square";
repaint();
} else if(style.equals("c") || style.equals("C")) {
whichStyle = 5;
showStatus("Written around a Circle");
styleOfDrawing = "Circle";
repaint();
} else if(style.equals("t") || style.equals("T")) {
whichStyle = 6;
showStatus("Written around a Triangle");
styleOfDrawing = "Triangle";
repaint();
}
else {
showStatus("Invalid Stlye!");

}
//add a new value to allStyleOfDrawing array
allStylesOfDrawing[phraseCounter-1] = styleOfDrawing;
//add a new value to allStartPointX,Y arrays
if(startPointX == -10 && startPointY == -10) {
allStartPointX[phraseCounter-1] = getSize().width / 2;
allStartPointY[phraseCounter-1] = getSize().height / 2;
} else {
allStartPointX[phraseCounter-1] = startPointX;
allStartPointY[phraseCounter-1] = startPointY;
}

//check if the larger button has been clicked
if(e.getSource() == button1) {
//increase the scale by 1
scale += 1;
repaint();
}
//check if the smaller button has been clicked
else if (e.getSource() == button2) {
//decrease the scale by 1
scale -= 1;
repaint();
}
//check if the smaller button has been clicked
else if (e.getSource() == restart) {
//restart (Clear Screen) the applet
drawingDetails.setText("");
text1.setText("");
text2.setText("");
showStatus("");
threadSuspended = true;
runner = null;
isMouseClick = false;
start();

}
//check if the animate button has been clicked
else if (e.getSource() == animate) {
//activate the thread (animation)
if (runner == null) {
runner = new Thread(this);
threadSuspended = false;
runner.start();
}
}
//check if button1 or button2 has been clicked, or the user has pressed enter on text1 or text2 to add a new value to allScales array
if (e.getSource() == button1 || e.getSource() == button2 || e.getSource() == text1 || e.getSource() == text2 ) {
allScales[phraseCounter-1] = scale;
}
}
}

public void displayVertically(Graphics gIn) {
//pre: the method is called in paint method. gIn is a valid graphic object
//post: draw the text vertically
//going through the text character by character and then draw it

for (int i = 0; i < phrase.length(); i++) {
char currentLetter;
currentLetter = phrase.charAt(i);
// draw the character by fillPolygon if the current character is 'P' or 'p'
if (currentLetter == sl1 || currentLetter == sl1C){
//call method drawsl1() to draw 'P'
drawSl1(gIn);
//add a number to the current position Y for drawing next character
startPointY += (25 + (scale * 4));
//draw the character by fillPolygon if the current character is 'M' or 'm'
} else if(currentLetter == sl2 || currentLetter == sl2C) {
//call method drawsl2() to draw 'M'
drawSl2(gIn);
//add a number to the current position Y for drawing next character
startPointY += (25 + (scale * 4));
//draw the character by drawString if the current character is not 'P' or 'M'
} else {
//call method drawText() to draw the character if it is not 'P' or 'M'
drawText(gIn, currentLetter);
//add a number to the current position Y for drawing next character
startPointY += (25 + (scale * 4));
}
phraseCoordinates[i] = "( " + startPointX + " , " + startPointY + " )";
}
showingDetails += "TEXT: " + phrase + " -- SIZE: " + phraseLength + " -- DRAWINGSTYLE: " + styleOfDrawing + " -- COORDINATES: " + displayCoordinates(phraseCoordinates) + "n";
//reset startpoint for X and Y
if (isMouseClick) {
startPointX = mouseX;
startPointY = mouseY;
} else {
startPointX = -10;
startPointY = -10;
isMouseClick = false;
}
}

public void displayDiagonally(Graphics gIn) {
//pre: the method is called in paint method. gIn is a valid graphic object
//post: draw the text diagonally
//going through the text character by character and then draw it
for (int i = 0; i < phrase.length(); i++) {
char currentLetter;
currentLetter = phrase.charAt(i);
// draw the character by fillPolygon if the current character is 'P' or 'p'
if (currentLetter == sl1 || currentLetter == sl1C){
//call method drawsl1() to draw 'P'
drawSl1(gIn);
//add a number to the current position X,Y for drawing next character
startPointX += (25 + (scale * 3));
startPointY += (25 + (scale * 3));
//draw the character by fillPolygon if the current character is 'M' or 'm'
} else if(currentLetter == sl2 || currentLetter == sl2C) {
//call method drawsl2() to draw 'M'
drawSl2(gIn);
//add a number to the current position X,Y for drawing next character
startPointX += (25 + (scale * 3));
startPointY += (25 + (scale * 3));
//draw the character by drawString if the current character is not 'P' or 'M'
} else {
//call method drawText() to draw the character if it is not 'P' or 'M'
drawText(gIn, currentLetter);
//add a number to the current position X,Y for drawing next character
startPointX += (25 + (scale * 3));
startPointY += (25 + (scale * 3));
}
phraseCoordinates[i] = "( " + startPointX + " , " + startPointY + " )";
}
showingDetails += "TEXT: " + phrase + " -- SIZE: " + phraseLength + " -- DRAWINGSTYLE: " + styleOfDrawing + " -- COORDINATES: " + displayCoordinates(phraseCoordinates) + "n";
//reset startpoint for X and Y
if (isMouseClick) {
startPointX = mouseX;
startPointY = mouseY;
} else {
startPointX = -10;
startPointY = -10;
isMouseClick = false;
}
}

public void displayZigzag(Graphics gIn) {
//pre: the method is called in paint method. gIn is a valid graphic object
//post: draw the text in zigzag manner
//going through the text character by character
for (int i = 0; i < phrase.length(); i++) {
char currentLetter;
currentLetter = phrase.charAt(i);
// draw the character by fillPolygon if the current character is 'P' or 'p'
if (currentLetter == sl1 || currentLetter == sl1C){
//call method drawsl1() to draw 'P'
drawSl1(gIn);
//add a number to the current position X, Y(if i is even or subtract if i is not even) for drawing next character
if(i % 2 == 0) {
startPointX += (25 + (scale * 3));
startPointY += (scale * 3) + 30;
} else {
startPointX += (25 + (scale * 3));
startPointY -= (scale * 3) + 30;
}
//draw the character by fillPolygon if the current character is 'M' or 'm'
} else if(currentLetter == sl2 || currentLetter == sl2C) {
//call method drawsl1() to draw 'P'
drawSl2(gIn);
//add a number to the current position X, Y(if i is even or subtract if i is not even) for drawing next character
if(i % 2 == 0) {
startPointX += (25 + (scale * 3));
startPointY += (scale * 3) + 30;
} else {
startPointX += (25 + (scale * 3));
startPointY -= (scale * 3) + 30;
}
//draw the character by drawString if the current character is not 'P' or 'M'
} else {
//call method drawText() to draw the character if it is not 'P' or 'M'
drawText(gIn, currentLetter);
//add a number to the current position X, Y(if i is even or subtract if i is not even) for drawing next character
if(i % 2 == 0) {
startPointX += (25 + (scale * 3));
startPointY += (scale * 3) + 30;
} else {
startPointX += (25 + (scale * 3));
startPointY -= (scale * 3) + 30;
}
}
phraseCoordinates[i] = "( " + startPointX + " , " + startPointY + " )";
}
showingDetails += "TEXT: " + phrase + " -- SIZE: " + phraseLength + " -- DRAWINGSTYLE: " + styleOfDrawing + " -- COORDINATES: " + displayCoordinates(phraseCoordinates) + "n";
//reset startpoint for X and Y
if (isMouseClick) {
startPointX = mouseX;
startPointY = mouseY;
} else {
startPointX = -10;
startPointY = -10;
isMouseClick = false;
}
}

public void displaySquare(Graphics gIn) {
//pre: the method is called in paint method. gIn is a valid graphic object
//post: draw the text around a square
char currentLetter;
int scaleSquare = 1;
int space = 0;
//set scale for square according to the length of string
if(phrase.length() > 4 && phrase.length() <= 8) {
scaleSquare = 2;
} else if(phrase.length() > 8 && phrase.length() <= 12) {
scaleSquare = 3;
} else if(phrase.length() > 12 && phrase.length() <= 16) {
scaleSquare = 4;
} else if(phrase.length() > 16 && phrase.length() <= 20) {
scaleSquare = 5;
} else if(phrase.length() > 20 && phrase.length() <= 24) {
scaleSquare = 6;
} else if(phrase.length() > 24 && phrase.length() <= 28) {
scaleSquare = 7;
}
//set size for the side of square
int sideSize = scaleSquare * 50;
//going through the text character by character
for (int i = 0; i < phrase.length(); i++) {
currentLetter = phrase.charAt(i);
// draw the character by fillPolygon if the current character is 'P' or 'p'
if (currentLetter == sl1 || currentLetter == sl1C){
//call method drawsl1() to draw 'P'
drawSl1(gIn);
//add 50 to the current position X or Y for drawing next character
space +=50;
if(space < sideSize) {
startPointX += 50;
} else if(space >= sideSize && space < 2 * sideSize) {
startPointY += 50;
} else if(space >= 2 * sideSize && space < 3 * sideSize) {
startPointX -= 50;
} else if (space >= 3 * sideSize && space < 4 * sideSize) {
startPointY -= 50;
}
//draw the character by fillPolygon if the current character is 'M' or 'm'
} else if(currentLetter == sl2 || currentLetter == sl2C) {
//call method drawsl2() to draw 'M'
drawSl2(gIn);
//add 50 to the current position X or Y for drawing next character
space +=50;
if(space < sideSize) {
startPointX += 50;
} else if(space >= sideSize && space < 2 * sideSize) {
startPointY += 50;
} else if(space >= 2 * sideSize && space < 3 * sideSize) {
startPointX -= 50;
} else if (space >= 3 * sideSize && space < 4 * sideSize) {
startPointY -= 50;
}
//draw the character by drawString if the current character is not 'P' or 'M'
} else {
//call method drawText() to draw the character if it is not 'P' or 'M'
drawText(gIn, currentLetter);
//add 50 to the current position X or Y for drawing next character
space +=50;
if(space < sideSize) {
startPointX += 50;
} else if(space >= sideSize && space < 2 * sideSize) {
startPointY += 50;
} else if(space >= 2 * sideSize && space < 3 * sideSize) {
startPointX -= 50;
} else if (space >= 3 * sideSize && space < 4 * sideSize) {
startPointY -= 50;
}
}
phraseCoordinates[i] = "( " + startPointX + " , " + startPointY + " )";
}
showingDetails += "TEXT: " + phrase + " -- SIZE: " + phraseLength + " -- DRAWINGSTYLE: " + styleOfDrawing + " -- COORDINATES: " + displayCoordinates(phraseCoordinates) + "n";
//reset startpoint for X and Y
if (isMouseClick) {
startPointX = mouseX;
startPointY = mouseY;
} else {
startPointX = -10;
startPointY = -10;
isMouseClick = false;
}
}

public void displayCircle (Graphics gIn) {
//pre: the method is called in paint method. gIn is a valid graphic object
//post: draw the text around a circle
int width, height;
int startPointX1, startPointY1;
//set width and height according to the applet's size
width = getSize().width;
height = getSize().height;
//set radius according to width and text length
int radius = (width / 10) + (2 * phrase.length());
//going through the text character by character
for (int i = 0; i < phrase.length(); i++) {
char currentLetter;
currentLetter = phrase.charAt(i);
// Calculate the angle between characters compare to centre of the circle
// Calculate (x,y) positions along a circle
double angle = 2*Math.PI*i/(double)phrase.length();
int x = (int)( radius*Math.cos(angle) );
int y = (int)( radius*Math.sin(angle) );
//Check if X, Y coordinates are taken from MouseClicked
if(startPointX == -10 && startPointY == -10) {
startPointX1 = (width/2+x);
startPointY1 = (height/2+y);
} else {
startPointX1 = startPointX +x;
startPointY1 = startPointY +y;
}
// draw the character by fillPolygon if the current character is 'P' or 'p'
if (currentLetter == sl1 || currentLetter == sl1C){
gIn.setColor(Color.red);
int[] x1 = {startPointX1, startPointX1, (startPointX1- (2 * scale)), (startPointX1- (2 * scale)), (startPointX1+ (8 * scale)), (startPointX1+ (8 * scale)), startPointX1, startPointX1, (startPointX1 + (6 * scale)), (startPointX1+ (6 * scale)), startPointX1, startPointX1};
int[] y1 = {startPointY1, (startPointY1+ (8 * scale)), (startPointY1+ (8 * scale)), (startPointY1- (8 * scale)), (startPointY1- (8 * scale)), startPointY1, startPointY1, (startPointY1- (2 * scale)), (startPointY1- (2 * scale)), (startPointY1- (6 * scale)), (startPointY1- (6 * scale)), (startPointY1- (2 * scale))};
gIn.fillPolygon(x1, y1, x1.length);
//draw the character by fillPolygon if the current character is 'M' or 'm'
} else if(currentLetter == sl2 || currentLetter == sl2C) {
gIn.setColor(Color.red);
int[] x1 = {startPointX1, (startPointX1), (startPointX1 + (10 * scale)), (startPointX1 + (20 * scale)), (startPointX1 + (20 * scale)), (startPointX1 + (18 * scale)), (startPointX1 + (18 * scale)), (startPointX1 + (10 * scale)), (startPointX1 + (2 * scale)), (startPointX1 + (2 * scale))};
int[] y1 = {startPointY1, (startPointY1- (15 * scale)), (startPointY1 - (4 * scale)), (startPointY1 - (15 * scale)), startPointY1, startPointY1, (startPointY1 - (11 * scale)), startPointY1, (startPointY1 - (11 * scale)), startPointY1};
gIn.fillPolygon(x1, y1, x1.length);
//draw the character by drawString if the current character is not 'P' or 'M'
} else {
gIn.setColor(Color.black);
//<<Casting>> Convert the Character to String to use of the drawString method
String s = Character.toString(currentLetter);
gIn.drawString(s, startPointX1, startPointY1);
}
phraseCoordinates[i] = "( " + startPointX1 + " , " + startPointY1 + " )";
}
showingDetails += "TEXT: " + phrase + " -- SIZE: " + phraseLength + " -- DRAWINGSTYLE: " + styleOfDrawing + " -- COORDINATES: " + displayCoordinates(phraseCoordinates) + "n";
//reset startpoint for X and Y
if (isMouseClick) {
startPointX = mouseX;
startPointY = mouseY;
} else {
startPointX = -10;
startPointY = -10;
isMouseClick = false;
}
}

public void displayTriangle(Graphics gIn) {
//pre: the method is called in paint method. gIn is a valid graphic object
//post: draw the text around a Triangle
char currentLetter;
int scaleTriangle = 1;
int space = 0;
//set scale for triangle according to the length of string
if(phrase.length() > 3 && phrase.length() <= 6) {
scaleTriangle = 2;
} else if(phrase.length() > 6 && phrase.length() <= 9) {
scaleTriangle = 3;
} else if(phrase.length() > 9 && phrase.length() <= 12) {
scaleTriangle = 4;
} else if(phrase.length() > 12 && phrase.length() <= 15) {
scaleTriangle = 5;
} else if(phrase.length() > 15 && phrase.length() <= 18) {
scaleTriangle = 6;
} else if(phrase.length() > 18 && phrase.length() <= 21) {
scaleTriangle = 7;
} else if(phrase.length() > 21 && phrase.length() <= 24) {
scaleTriangle = 8;
}
//set size for the side of triangle
int sideSize = scaleTriangle * 50;
//going through the text character by character
for (int i = 0; i < phrase.length(); i++) {
currentLetter = phrase.charAt(i);
// draw the character by fillPolygon if the current character is 'P' or 'p'
if (currentLetter == sl1 || currentLetter == sl1C){
//call method drawsl1() to draw 'P'
drawSl1(gIn);
//add 50 to the current position X or Y for drawing next character
space +=50;
if(space < sideSize) {
startPointX += 50;
startPointY += 50;
} else if(space >= sideSize && space < 2 * sideSize) {
startPointX -= 100;
} else if(space >= 2 * sideSize && space < 3 * sideSize) {
startPointX += 50;
startPointY -= 50;
}
//draw the character by fillPolygon if the current character is 'M' or 'm'
} else if(currentLetter == sl2 || currentLetter == sl2C) {
//call method drawsl2() to draw 'M'
drawSl2(gIn);
//add 50 to the current position X or Y for drawing next character
space +=50;
if(space < sideSize) {
startPointX += 50;
startPointY += 50;
} else if(space >= sideSize && space < 2 * sideSize) {
startPointX -= 100;
} else if(space >= 2 * sideSize && space < 3 * sideSize) {
startPointX += 50;
startPointY -= 50;
}
//draw the character by drawString if the current character is not 'P' or 'M'
} else {
//call method drawText() to draw the character if it is not 'P' or 'M'
drawText(gIn, currentLetter);
//add 50 to the current position X or Y for drawing next character
space +=50;
if(space < sideSize) {
startPointX += 50;
startPointY += 50;
} else if(space >= sideSize && space < 2 * sideSize) {
startPointX -= 100;
} else if(space >= 2 * sideSize && space < 3 * sideSize) {
startPointX += 50;
startPointY -= 50;
}
}
phraseCoordinates[i] = "( " + startPointX + " , " + startPointY + " )";
}
showingDetails += "TEXT: " + phrase + " -- SIZE: " + phraseLength + " -- DRAWINGSTYLE: " + styleOfDrawing + " -- COORDINATES: " + displayCoordinates(phraseCoordinates) + "n";
//reset startpoint for X and Y
if (isMouseClick) {
startPointX = mouseX;
startPointY = mouseY;
} else {
startPointX = -10;
startPointY = -10;
isMouseClick = false;
}
}

public void drawSl1(Graphics gIn) {
//pre: The letter needs to be 'P' or 'p'
//post: Draws letter 'P' using fillPolygon method
gIn.setColor(Color.red);
int[] x = {startPointX, startPointX, (startPointX+ (8 * scale)), (startPointX+ (8 * scale)), (startPointX+ (2 * scale)), (startPointX+ (2 * scale)), (startPointX+ (6 * scale)), (startPointX+ (6 * scale)), (startPointX + (2 * scale)), (startPointX+ (2 * scale)), startPointX};
int[] y = {startPointY, (startPointY- (16 * scale)), (startPointY- (16 * scale)), (startPointY- (8 * scale)), (startPointY- (8 * scale)), (startPointY- (14 * scale)), (startPointY- (14 * scale)), (startPointY- (10 * scale)), (startPointY- (10 * scale)), startPointY, startPointY};
gIn.fillPolygon(x, y, x.length);
}

public void drawSl2(Graphics gIn) {
//pre: The letter needs to be 'M' or 'm'
//post: Draws letter 'M' using fillPolygon method
gIn.setColor(Color.red);
int[] x = {startPointX, (startPointX), (startPointX + (10 * scale)), (startPointX + (20 * scale)), (startPointX + (20 * scale)), (startPointX + (18 * scale)), (startPointX + (18 * scale)), (startPointX + (10 * scale)), (startPointX + (2 * scale)), (startPointX + (2 * scale))};
int[] y = {startPointY, (startPointY- (15 * scale)), (startPointY - (4 * scale)), (startPointY - (15 * scale)), startPointY, startPointY, (startPointY - (11 * scale)), startPointY, (startPointY - (11 * scale)), startPointY};
gIn.fillPolygon(x, y, x.length);
}

public void drawText(Graphics gIn, char currentLetterIn) {
//pre: The letter needs to be anything apart from 'P' and 'M'
//post: Draws the cuurent letter using drawString method
gIn.setColor(Color.black);
//<<Casting>> Convert the Character to String to use of the drawString method
String s = Character.toString(currentLetterIn);
gIn.drawString(s, (startPointX), (startPointY));
}

public String displayCoordinates(String[] phraseCoordinatesIn) {
//pre: Some words/sentence needs to be entered
//post: Reutns a string which stores the coordinatation of entered text
String coordinates = "";
for (int i = 0; i < phraseCoordinatesIn.length; i++) {
coordinates += phraseCoordinatesIn[i];
}
return coordinates;
}

}