Jump to content

Drawing probelm (code&photo included)

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
3m masr

3m masr

    Newbie

  • Members
  • Pip
  • 3 posts
السلام عليكم و رحمة الله و بركاته ,
Peace, mercy and blessings of God ,

I coded the TicTacToe game that is similar to "Introduction to java programming" book's one , but i have mysterious error , see that photo

Posted Image

and this is the code


// importings 

import java.awt.Color;

import java.awt.Graphics;

import java.awt.GridLayout;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;


import javax.swing.JApplet;

import javax.swing.JPanel;

import javax.swing.border.LineBorder;

// end



// applet

public class TicTacToe extends JApplet {

	private char turn = ' ';

	private TTTCell[][] cells = new TTTCell[3][3];

	private JPanel p = new JPanel(new GridLayout(3,3));

	

	// tells who's turn  

	public char turn(){

		if(this.turn == ' ' || this.turn == 'o' ){

			this.turn = 'x';

			System.out.println("I am now X");

			return this.turn;

		}

		this.turn = 'o';

		System.out.println("I am now O");

		return this.turn;

	}

	// end

	

	

	public void init(){

		for(int i=0;i<3;i++){

			for(int j=0;j<3;j++){

				cells[i][j] = new TTTCell();

				p.add(cells[i][j]);

			}

		}

		add(p);

	}

	

	// game's single cell

	public class TTTCell extends JPanel {

		

		// the token on the cell ( can be either X or O )

		private char token = ' ';

		

		// tells whether the cell is empty , i didn't use it till now 

		public boolean tokenIsEmpty(){

			if(token == ' '){

				return true;

			}

			return true;

		}

		

		// sets the token on the cell and repaints it

		public void setToken(char turn){

			token=turn;

			repaint();

		}

		

		

		public void paintComponent(Graphics g){

			super.paintComponents(g);

			if(token == 'x'){

				g.drawLine(10, 10, getWidth()-10, getHeight()-10);

				g.drawLine(getWidth()-10, 10, 10, getHeight()-10);

				// indicating what i am drawing

				System.out.println("I am X shape");

			}else if (token == 'o'){

				g.drawOval(10, 10, getWidth()-20, getHeight()-20);

				//indicating what i am drawing

				System.out.println("I am Oval shape");

			}

		}

		

		public TTTCell(){

			this.setSize(20, 20);

			this.setBorder(new LineBorder(Color.RED,1));

			addMouseListener(new MouseListener());

		}

		


		public class MouseListener extends  MouseAdapter{

			public void mouseClicked(MouseEvent event){

				// set token only if it is not set

				if(token == ' '){

					setToken(turn());

				}

			}

		}

		

	}

	

	

}




Edited by 3m masr, 17 October 2010 - 09:21 AM.
thumbnailing


#2
Roman Y

Roman Y

    Programmer

  • Members
  • PipPipPipPip
  • 189 posts
Haven't tested but I'm pretty sure it fails at the tokenIsEmpty() because it returns true regardless... so it should be:

public boolean tokenIsEmpty() {

if(token == ' ')

   return true;

return false;

}


#3
3m masr

3m masr

    Newbie

  • Members
  • Pip
  • 3 posts
Thanks man , despite not using this method till now , i corrected it but it still draw Xed O !!

#4
Roman Y

Roman Y

    Programmer

  • Members
  • PipPipPipPip
  • 189 posts
oh so it's so that it is drawing them both at the same time?! ok, I thought that the bug was that a player was able to draw one on top of the other, so I stopped looking after that one... I'll look into it more close tomorow (kind of bussy now).

#5
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
I don't quite understand why, but it works if you change the paintComponent into paint(Graphics g)

public void paint(Graphics g){

    super.paint(g);

    ....

}


Edit: paintComponent still works! You just have to do
super.paintComponent(g);
instead of
super.paintComponents(g);

the paintComponents will paint every component allready, including the one with the first X.
I think the graphics g you passed with it, is passed by reference, so everything that's painted on the other components will all end up in that Graphics g Objects,
and you then continue with the code, adding a circle on top of the X that was present in the graphics.
.. I hope this is somewhat clear, i find it hard to explain:mad:

1 word of advice, the mouseclicked only triggers if the mouse position of the time when the mouse is pressed is the same as the location when the mousebutton is released. This means that the mouseClicked will not trigger if you are moving the mouse, even if you stay within 1 cell.
I usually use mousePressed to avoid this annoying behaviour.

#6
3m masr

3m masr

    Newbie

  • Members
  • Pip
  • 3 posts
Roman : thanks man ,
Oxano : you are correct , it works , sometimes Eclipse is not very helpful !