File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Tic Tac Toe blank board

 
Lexy afaras
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Lexy afaras
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bill Shirley
Ranch Hand
Posts: 457
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the correct group is:

Swing / AWT / ...

http://www.coderanch.com/forums/f-2/GUI
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic