aspose file tools*
The moose likes Beginning Java and the fly likes Tic Tac Toe blank board Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Tic Tac Toe blank board" Watch "Tic Tac Toe blank board" New topic
Author

Tic Tac Toe blank board

Lexy afaras
Greenhorn

Joined: Jan 23, 2008
Posts: 4
I am having problem construction the blank Tic tac toe board. I have the main square in place but filling the square with the equally distributed nine squares has been a challenge. Any help is greatly appreciated. This what i have so far

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class DrawExample extends JFrame
implements ActionListener {

private JButton button;
private JPanel panel;

public static void main(String[] args) {
DrawExample frame = new DrawExample();
frame.setSize(400, 300);
frame.createGUI();
frame.setVisible(true);
}

private void createGUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new FlowLayout() );

panel = new JPanel();
panel.setPreferredSize(new Dimension(300, 200));
panel.setBackground(Color.white);
window.add(panel);

button = new JButton("Press me");
window.add(button);
button.addActionListener(this);
}

public void actionPerformed(ActionEvent event) {
Graphics paper = panel.getGraphics();
paper.drawRect(10, 10, 180, 180);
paper.fillRect(15, 15, 60, 60);
paper.fillRect(15, 15, 60, 60);
paper.fillRect(15, 15, 60, 60);
paper.fillRect(15, 15, 60, 60);
paper.fillRect(15, 15, 60, 60);
paper.fillRect(15, 15, 60, 60);
paper.fillRect(15, 15, 60, 60);
paper.fillRect(15, 15, 60, 60);
paper.fillRect(15, 15, 60, 60);
}
}
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Your nine "fillRect()" calls all have the same arguments, so they're all going to draw in the same spot. They'd each need to use the coordinates of one square. A nicer version would use two nested "for" loops, one for the rows, one for the columns, one line that calls fillRect(), and calculated the parameters for each call.

BUT...

Wouldn't it be easier just to draw the four lines that are traditionally drawn to make a tictactoe board, rather than filling in the spaces between?


[Jess in Action][AskingGoodQuestions]
Lexy afaras
Greenhorn

Joined: Jan 23, 2008
Posts: 4
Ernest Thanks for the follow up. The Sad thing is I am a beginner and still working my way through the java experience but this part of your statement
"A nicer version would use two nested "for" loops, one for the rows, one for the columns, one line that calls fillRect(), and calculated the parameters for each call"

I didn't really understand it very well
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11480
    
  16

in this case, don't think about how to print each of the nine squares individually.

to print your board, you need to print three rows. if you could write a for-loop that on each of the three iterations prints a row, problem solved.

you can write code that does that - sort of. write some code that outputs this by writing a single for-loop:

printed row 1
printed row 2
printed row 3


once you have that working, you can change the body of the loop. instead of outputting 'printed row x', have it print three boxes


or, do it the other way around. write some code that prints 3 boxes across. then, write a loop around that to call it three times, each time moving down where you print.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
the correct group is:

Swing / AWT / ...

http://www.coderanch.com/forums/f-2/GUI


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
 
 
subject: Tic Tac Toe blank board