Negative coordinates are valid. However you won't see anything drawn there, at least not until you learn how to make the screen scrollable. This line will appear to enter the applet from above and to the left.
These are two different ways of specifying the same line. One draws from top to bottom and left to right, the other from bottom right to top left. However, both produce the same line.
These are two different lines. The first is between the two points (x=0, y=1) and (x=5, y=6); the second between (x=1, y=0) and (x=6, y=5). This isn't a large difference, but it is a difference.
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.FontMetrics;
import java.util.StringTokenizer;
import java.awt.Font;
public class WrapTextApplet extends Applet {
String input_from_page;
int margin = 5;
Font theFont;
public void init() {
String s;
int fontsize = 12;
String fontname = "Courier";
int fontstyle = Font.PLAIN;
input_from_page = getParameter("Text");
if ((s = getParameter("Size")) != null) {
fontsize = Integer.parseInt(s);
}
if ((s = getParameter("Style")) != null) {
if (s.equalsIgnoreCase("Italic")) fontstyle = Font.ITALIC;
if (s.equalsIgnoreCase("Bold")) fontstyle = Font.BOLD;
}
if ((s = getParameter("Font")) != null) {
fontname = s;
}
theFont = new Font(fontname, fontstyle, fontsize);
}
public void paint(Graphics g) {
int i = 0;
int linewidth = 0;
StringBuffer sb = new StringBuffer();
g.setFont(theFont);
FontMetrics fm = g.getFontMetrics();
StringTokenizer st = new StringTokenizer(input_from_page);
while (st.hasMoreTokens()) {
String nextword = st.nextToken();
if (fm.stringWidth(sb.toString() + nextword) < size().width) {
sb.append(nextword);
sb.append(' ');
}
else if (sb.length() == 0) {
g.drawString(nextword, margin, ++i*fm.getHeight());
}
else {
g.drawString(sb.toString(), margin, ++i*fm.getHeight());
sb = new StringBuffer(nextword + " ");
}
}
if (sb.length() > 0) {
g.drawString(sb.toString(), margin, ++i*fm.getHeight());
}
}
}