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!
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);
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.
Joined: Apr 08, 2008
of corse i can sorry for tht....here it is for you to look at:
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 ]
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.