aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Ohhhhh this has taken ALLLL DAY! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Ohhhhh this has taken ALLLL DAY! " Watch "Ohhhhh this has taken ALLLL DAY! " New topic
Author

Ohhhhh this has taken ALLLL DAY!

ben williamson
Greenhorn

Joined: Apr 08, 2008
Posts: 5
hey all,

Well im going absolutely insane! I have been sitting infront of this thing all day today just to get myself some decent practise with java after not having programmed for a couple of weeks....so i thought i would create a simple naughts and crosses type of game!?! well i say simple!?!

I am trying to do just a two player thing at the moment, whereby first person who clicks...the square turns black...second person chooses a square, turns red, then back to 1st player and black....etc etc! but for some reason, this thing only does the red square, and then does not go onto the black one, it's so frustrating i have tried so many diff ways! If any genious out there that can help, by all means please please do!

Here is the code i have created:






public class theGame extends JApplet {

final int width = 375;
final int height = 375;
boolean valid = false, valid2 = false, valid3 = false, valid4 = false;
boolean valid5 = false, valid6 = false, valid7 = false, valid8 = false, valid9 = false;
JTextField textFieldOne;
JTextArea textAreaOne;
JButton b1, b2, b3, b4, b5, b6, b7, b8, b9;

public void init() {

String name = JOptionPane.showInputDialog(null,
"What's your name??",
"Hello, what's your name??",
JOptionPane.QUESTION_MESSAGE);

Container c = getContentPane();
c.setLayout(null);
c.setBackground(Color.white);

textFieldOne = new JTextField();
textFieldOne.setText("Hello " +name+ ", Welcome to Red & Black )");
textFieldOne.setEditable(false);

textAreaOne = new JTextArea(5,5);
textAreaOne.setText("Heres How To Play: Simply click on the squares below" +
"\nin order to set it to your colour. Make a horizontal," +
"\nvertical, or even a diagonal line of your colour to win!!");
textAreaOne.setEditable(false);

b1 = new JButton("?");
b1.addActionListener(new ButtonHandler(this, 'A'));
b2 = new JButton("?");
b2.addActionListener(new ButtonHandler(this, 'B'));
b3 = new JButton("?");
b3.addActionListener(new ButtonHandler(this, 'C'));
b4 = new JButton("?");
b4.addActionListener(new ButtonHandler(this, 'D'));
b5 = new JButton("?");
b5.addActionListener(new ButtonHandler(this, 'E'));
b6 = new JButton("?");
b6.addActionListener(new ButtonHandler(this, 'F'));
b7 = new JButton("?");
b7.addActionListener(new ButtonHandler(this, 'G'));
b8 = new JButton("?");
b8.addActionListener(new ButtonHandler(this, 'H'));
b9 = new JButton("?");
b9.addActionListener(new ButtonHandler(this, 'I'));

SquarePanel graphicsPanel = new SquarePanel(this);

//FOR THE TEXT FIELD POSITIONING

Insets insets = c.getInsets();
Dimension size = textFieldOne.getPreferredSize();
textFieldOne.setBounds(70 + insets.left, 25 + insets.top,
size.width, size.height);

Insets insets0 = c.getInsets();
Dimension size2 = textAreaOne.getPreferredSize();
textAreaOne.setBounds(35 + insets0.left, 50 + insets0.top,
size2.width, size2.height);

//FOR THE BUTTONS POSITIONING PLACED ON THE BOARD

Insets insets2 = c.getInsets();
b1.setBounds(87 + insets2.left, 137 + insets2.top,
50, 50);

Insets insets3 = c.getInsets();
b2.setBounds(162 + insets3.left, 137 + insets3.top,
50, 50);

Insets insets4 = c.getInsets();
b3.setBounds(237 + insets4.left, 137 + insets4.top,
50, 50);

Insets insets5 = c.getInsets();
b4.setBounds(87 + insets5.left, 214 + insets5.top,
50, 50);

Insets insets6 = c.getInsets();
b5.setBounds(162 + insets6.left, 214 + insets6.top,
50, 50);

Insets insets7 = c.getInsets();
b6.setBounds(237 + insets7.left, 214 + insets7.top,
50, 50);

Insets insets8 = c.getInsets();
b7.setBounds(87 + insets8.left, 292 + insets8.top,
50, 50);

Insets insets9 = c.getInsets();
b8.setBounds(162 + insets9.left, 292 + insets9.top,
50, 50);

Insets insets10 = c.getInsets();
b9.setBounds(237 + insets10.left, 292 + insets10.top,
50, 50);


//FOR THE ACTUAL GRAPHICS PANEL

Insets insets11 = c.getInsets();
graphicsPanel.setBounds(insets11.left, 50 + insets11.top,
375, 375);


c.add(graphicsPanel);
c.add(b1);
c.add(b2);
c.add(b3);
c.add(b4);
c.add(b5);
c.add(b6);
c.add(b7);
c.add(b8);
c.add(b9);
c.add(textFieldOne);
c.add(textAreaOne);

repaint();

}

class SquarePanel extends JPanel {

int counter = 1;
theGame theApplet;
SquarePanel(theGame app)

{
theApplet = app;
}

public void paint (Graphics g) {

theApplet.setSize(375, 375);
super.paintComponents(g);

g.drawLine(75, 150, width - 75, 150);
g.drawLine(75, height - 150, width - 75, height - 150);
g.drawLine(150, 75, 150, height - 75);
g.drawLine(width - 150, 75, width - 150, height - 75);

for (int x = counter; x <= 9; x++){

if ((counter == 1) || (counter == 3) || (counter == 5) || (counter == 7) || (counter == 9)){

g.setColor(Color.red);

if (theApplet.valid) {

g.fillRect(87,87,50,50);

} else if (theApplet.valid2) {

g.fillRect(162,87,50,50);

} else if (theApplet.valid3) {

g.fillRect(237,87,50,50);

} else if (theApplet.valid4) {

g.fillRect(87,164,50,50);

} else if (theApplet.valid5) {

g.fillRect(162,164,50,50);

} else if (theApplet.valid6) {

g.fillRect(237,164,50,50);

} else if (theApplet.valid7) {

g.fillRect(87,241,50,50);

} else if (theApplet.valid8) {

g.fillRect(162,241,50,50);

} else if (theApplet.valid9) {

g.fillRect(237,241,50,50);

}

//repaint();
//counter++;

}

else if ((counter == 2) || (counter == 4) || (counter == 6) || (counter == 8)){

g.setColor(Color.black);

if (theApplet.valid) {

g.fillRect(87,87,50,50);

} else if (theApplet.valid2) {

g.fillRect(162,87,50,50);

} else if (theApplet.valid3) {

g.fillRect(237,87,50,50);

} else if (theApplet.valid4) {

g.fillRect(87,164,50,50);

} else if (theApplet.valid5) {

g.fillRect(162,164,50,50);

} else if (theApplet.valid6) {

g.fillRect(237,164,50,50);

} else if (theApplet.valid7) {

g.fillRect(87,241,50,50);

} else if (theApplet.valid8) {

g.fillRect(162,241,50,50);

} else if (theApplet.valid9) {

g.fillRect(237,241,50,50);

}

//repaint();
//counter++;

}

repaint();

}

}

}

class ButtonHandler implements ActionListener {

theGame theApplet;
char theButton;

public ButtonHandler(theGame theApp, char but) {

theApplet = theApp;
theButton = but;

}

public void actionPerformed(ActionEvent e) {

if (theButton == 'A') {

valid = true;

}

if (theButton == 'B') {

valid2 = true;

}

if (theButton == 'C') {

valid3 = true;

}

if (theButton == 'D') {

valid4 = true;

}

if (theButton == 'E') {

valid5 = true;

}

if (theButton == 'F') {

valid6 = true;

}

if (theButton == 'G') {

valid7 = true;

}

if (theButton == 'H') {

valid8 = true;

}

if (theButton == 'I') {

valid9 = true;

}

theApplet.repaint();

}

}

}






I have simply been mucking around with the paint method for about 2 hours now, and cannot fix this problem! Any help would be brilliant, thank you!
Jess Dancel
Greenhorn

Joined: Nov 07, 2007
Posts: 11
Hey, Ben.

Can I suggest reformatting your code posting with Code tags? Just edit the post, highlight the code and click on the Code button below your edit window under "Instant UBB Code." It will be much nicer to read then.
ben williamson
Greenhorn

Joined: Apr 08, 2008
Posts: 5
of corse i can sorry for tht....here it is for you to look at:


Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
This is hard to read because of the lack of code tags (and hence indentation) but, disregarding that, I think this code is kind of confused.

For one, you seem to try to be painting filled rectangles over the JButtons. While I guess that could work in theory, it would be much easier to not try to do that in paint() and simply call something like b1.setBackground(Color.RED), don't you think?

I'm not wild about setting the layout manager to null and calling setBounds() for everything, but I guess it could be permissible for naughts and crosses. (The problem is that the technique is used far too often, especially by beginners, when there is no reason for it.)

Also, you might want to rename it from theGame to TheGame. The convention is that classes should begin with a capital letter.
[ April 08, 2008: Message edited by: Brian Cole ]

bitguru blog
ben williamson
Greenhorn

Joined: Apr 08, 2008
Posts: 5
i think that's a little better for you...
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by ben williamson:
i think that's a little better for you...


Well it does have code tags, but still no indentation.
If you wrote it without indentation, then fine. Otherwise
maybe go back (edit [pencil-on-paper icon] one or both of
your previous posts, don't make a new post) and paste your
indented code between the code tags.

As for your code, allow me to make this suggestion:

Comment out your paint() method. Get it working to your
satisfaction without the crosshatch g.drawLine() stuff,
using setBackground() to set the color of the buttons.
Then when you get that far, go ahead and override paint()
for the crosshatch drawing.

This will allow you to fix a couple of flaws in your code
right away:

1) You shouldn't really be calling setSize() from within paint().
You should only have to initialize the size once.

2) Definitely don't call repaint() from within paint(). This
will cause continual repainting and chew up a lot of unnecessary
CPU cycles.

There's also no point in calling repaint() at the end of init(),
but that one is reasonably harmless.

In fact, if you're going to use setBackground() to change
the color of the buttons, you shouldn't need to call repaint()
anywhere in the whole applet. If you're going to override paint()
to do that (which I don't recommend) then you will have to call
repaint() but the correct place for it is in your ButtonHandler.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Ohhhhh this has taken ALLLL DAY!
 
Similar Threads
Help needed with emoticons in a chat application
basic things with awt
Adding JButton Array to a applet issue
tictactoe help
problem with handling the states of the events