// From text by Cay Horstmann: Java Concepts, 2005. import java.applet.Applet; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Line2D; public class TicTacToe extends Applet { private static final int XMIN = 0, XMAX = 3, YMIN = 0, YMAX = 3; // converts user-coordinates to pixels for x (horizontal) scale - p. 175 private double xpixel(double xuser) { return (xuser - XMIN) * (getWidth() - 1) / (XMAX - XMIN); // note: getWidth() is an Applet method } // same for y (vertical) scale, assuming y increases upwards - p. 176 private double ypixel(double yuser) { return (yuser - YMAX) * (getHeight() - 1) / (XMIN - XMAX); // note: accounts for pixels that actually increase downwards } /** Draws a tic-tac-toe board. The board consists of 2 horizonal lines and 2 vertical lines, and it always fills the window. */ public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; g2.draw(new Line2D.Double( // bottom horizontal line xpixel(0), ypixel(1), xpixel(3), ypixel(1))); g2.draw(new Line2D.Double( // top horizontal line xpixel(0), ypixel(2), xpixel(3), ypixel(2))); g2.draw(new Line2D.Double( // left vertical line xpixel(1), ypixel(0), xpixel(1), ypixel(3))); //note error in text g2.draw(new Line2D.Double( // right vertical line xpixel(2), ypixel(0), xpixel(2), ypixel(3))); } }