English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

x
xx
xxx
xxxx
xxxxx

2007-02-25 03:11:18 · 2 answers · asked by Julie C (little ninja) 1 in Computers & Internet Programming & Design

2 answers

This is too easy! The only problem is that yahoo answers destroys some text formatting, and may even fail to show some text --- so the code might look messy.

To get the pattern you have shown, you can do something like this in the applet's paint method:

public void paint(Graphics g) {
g.drawString("x",15,15);
g.drawString("xx",15,30);
g.drawString("xxx",15,45);
g.drawString("xxxx",15,60);
g.drawString("xxxxx",15,75);
}
_____________________________

A more complicated but flexible way to do it with applet parameters is:

/*
* TriangleX.java
*/
import java.awt.Graphics;

public class TriangleX extends java.applet.Applet {
int xCount, startingXPos, startingYPos, verticalSpacing;
public void init() {
String xCountString = getParameter("xCount");
xCount = Integer.parseInt(xCountString);
String startingXPosString = getParameter("startingXPos");
startingXPos = Integer.parseInt(startingXPosString);
String startingYPosString = getParameter("startingYPos");
startingYPos = Integer.parseInt(startingYPosString);
String verticalSpacingString = getParameter("verticalSpacing");
verticalSpacing = Integer.parseInt(verticalSpacingString);

System.out.print("xCount = ");
System.out.print(xCount);
System.out.println();
System.out.print("startingXPos = ");
System.out.print(startingXPos);
System.out.println();
System.out.print("startingYPos = ");
System.out.print(startingYPos);
System.out.println();
System.out.print("verticalSpacing = ");
System.out.print(verticalSpacing);
System.out.println();
}

public void paint(Graphics g) {
String xStr = "x";
int x = startingXPos;
int y = startingYPos;
for(int i = 0; i < xCount; i++) {
g.drawString(xStr, x, y);
y += verticalSpacing;
xStr = xStr.concat("x");
}
}
}
_____________________________

Your html file with the applet tag and parameters would be something like this:



Applet HTML Page



Applet HTML Page











By tkb



_____________________________

2007-02-25 07:50:04 · answer #1 · answered by Terence B 2 · 0 0

Inside your applet's 'paint' method:

TextArea txt;
this.add(txt);

for (int i=0; i<5; i++){
for (int j=0; j<=i; j++){
txt.append("x");
}
txt.append("\n");
}

2007-02-25 03:43:10 · answer #2 · answered by Amit Y 5 · 0 0

fedest.com, questions and answers