Tic Tac Toe

I’ve been working on a simple Tic Tac Toe game. Most things going well, but I’ve been working on the painting of the X’s and O’s within the insets of a given JButton. In the end, the calculation that actually centered it drove me crazy: I had to use 2 * the width of paint stroke.

Anyway, I’m looking for a cleaner way to draw the X and O Icon within a JButton. Here’s the current image; it currently resizes well, but there’s gotta be another way. Any suggestions? Thanks in advance. Here’s the paint code:

	public void paintIcon(Component c, Graphics g, int x, int y) {
		if (!CLEAR_SQUARE.equals(type)) {
			int strokeWidth = calcStrokeWidth(getWidth(), getHeight());

			int strokeWidthX2 = strokeWidth * 2;

			Insets insets = getInsets();
			int width = getWidth() - strokeWidthX2 - insets.left - insets.right;
			int height = getHeight() - strokeWidthX2 - insets.top - insets.bottom;
			int left = insets.left + strokeWidth;
			int right = left + width;
			int top = insets.top + strokeWidth;
			int bottom = top + height;

			Graphics2D g2 = (Graphics2D) g;

			if (X_SQUARE.equals(type)) {
				g2.drawLine(left, top, right, bottom);
				g2.drawLine(left, bottom, right, top);

			} else if (O_SQUARE.equals(type)) {
				g2.drawOval(left, top, width, height);

This entry was posted in Swing. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *