Results 1 to 5 of 5
  1. #1
    java_fun2007 is offline Member
    Join Date
    Nov 2007
    Posts
    35
    Rep Power
    0

    Default can you help me with mouse pressed method please?

    hello I'm trying to make this game but the player label it doesn't appear in the applet and i cant draw string in the location like X or O can you take a look at my code please it is in Board class.
    Thanks

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    class Player{
    	private int xCoordinate;
    	private int yCoordinate;
        private String playerText;
      public   Player(int x, int y, String t) 
        {
            playerText = t;
            xCoordinate = x;
            yCoordinate = y;
            
        }
        public void draw(Graphics g) 
         {
         	 g.drawString(playerText,  xCoordinate, yCoordinate); 
         }
         public void SetText(String t)
         { playerText = t;
         }
         
    }//end player
    class Square
    {
    	private int x;
    	private int y;
    	private int occupied;
    
    	public Square(int x1, int y1)
    	{
    		x = x1;
    		y= y1;
    		occupied = 0;
    	}
    	
    	
    	public void setX(int x1)
    	{
    		x = x1;
    	}
    	
    	public int getX()
    	{
    		return x;
    	}
    	
    	public void setY(int y1)
    	{
    		y = y1;
    	}
    	
    	public int getY()
    	{
    		return y;
    	}
    	
    	
    	public void setOccupied(int o)
    	{
    		occupied = o;
    	}
    	
    	public int getOccupied()
    	{
    		return occupied;
    	}
    	}
    	
    public class TicTac extends JApplet implements ActionListener
    { private JPanel panel;
    	private JButton newgame;
    	private Board B;
    	public void init()
    	{
    		B = new Board();
    		add( B, BorderLayout.CENTER );
    		panel = new JPanel();
          	newgame = new JButton( "New Game" );
          	panel.add( newgame);
          	add( panel, BorderLayout.SOUTH );
          	newgame.addActionListener(this);
    	}
    		public void actionPerformed( ActionEvent ev)
          	{	Object source = ev.getSource();
     	if (source == newgame)
          		{remove(B);
    			repaint();
    		B = new Board();
    			add( B, BorderLayout.CENTER );
    	B.setTurn(1);
    			repaint();
    			validate();
          	}
          	}
       	}//end class TicTac
       	
       	
    class Board extends JPanel  implements MouseListener, MouseMotionListener
    {
        private JLabel playerlabel;  
    	private boolean play;
    	private boolean start;
        private int turn;  
        private Square squares[][]; 
      private Player myplayer[][];
    
    	public Board()
    	{
    		setBackground( Color.WHITE );
    		playerlabel = new JLabel( "X player first" );
    		//playerlabel.setLayout(null);
    	//	playerlabel.setLocation(500,500);
    		add( playerlabel );
    		
    		
    		play = true;
    		turn = 1;
        	squares = new Square[3][3];
        	myplayer = new Player[3][3];
        	
        	 
        	int x = 40;
    		int y = 40;
    		for (int i=0; i<3; i++){
    			x = 40;
    			for (int j=0; j<3; j++){
    				Square s = new Square( x,y );
    				squares[i][j] = s;
    				x +=50;
    			}
    			y +=50;
    		}
        	
    		  this.addMouseListener(this); 
            this.addMouseMotionListener(this);
    	}
    	
    
    
          public void mouseClicked( MouseEvent event ){} 
          public void mouseMoved( MouseEvent event ){}
          public void mouseEntered( MouseEvent event ){}
          public void mouseExited( MouseEvent event ){}
    
          public void mousePressed( MouseEvent event )
          {int xPos = event.getX(); 
             	int yPos = event.getY(); 
          		if(play && turn ==1){
          		for (int i=0; i<3; i++){
    				for (int j=0; j<3; j++){
    					int x = squares[i][j].getX();
    					int y = squares[i][j].getY();
    					if(xPos> x && xPos<x+40&& yPos> y && yPos<y+40){
    						
    						if( squares[i][j].getOccupied() ==0 ){
    							String ptext;
    							ptext = "X";
    							Player p = new Player(x, y,ptext);
    							myplayer[i][j] = p;
    							squares[i][j].setOccupied(1);
    							
    							playerlabel.setText( "O Player Turn" );
    							
    							turn = 2;
    							repaint();
    							Win();
    						}
    						
    						
    						else{
    							turn = 2;
    							playerlabel.setText( "O Player Turn" );
    							
    							repaint();
    						}
    						
    					}
    					
    				}
    			}
    				
    			
    		}
    		
    		if ( play && turn ==2 )
    		{
    for (int i=0; i<3; i++){
    				for (int j=0; j<3; j++){
    					int x = squares[i][j].getX();
    					int y = squares[i][j].getY();
    					if(xPos> x && xPos<x+40&& yPos> y && yPos<y+40){
    						
    						if( squares[i][j].getOccupied() ==0 ){
    							String ptext;
    							ptext = "O";
    							Player p = new Player(x, y,ptext);
    							myplayer[i][j] = p;
    							squares[i][j].setOccupied(2);
    							
    							playerlabel.setText( "X Player Turn" );
    							
    							turn = 1;
    							repaint();
    							Win();
    						}
    						
    						
    						else{
    							turn = 1;
    							playerlabel.setText( "X Player Turn" );
    							
    							repaint();
    						}
    						
    					}
    					
    				}
    			}
    				
    			
    		}
    		}
    		
    
          public void mouseReleased( MouseEvent event )
          { }
        
          public void mouseDragged( MouseEvent event )
          { }  		
    
    	public void Win(){
    		if(squares[0][0].getOccupied() == squares[0][1].getOccupied() &&squares[0][1].getOccupied()==squares[0][2].getOccupied() &&squares[0][2].getOccupied() == 1)
    		{playerlabel.setText("Player 1 X WON THE GAME!");
    		play =false;		}
    		else if (squares[1][0].getOccupied() == squares[1][1].getOccupied() &&squares[1][1].getOccupied()==squares[1][2].getOccupied() &&squares[1][2].getOccupied() == 1)
    		{playerlabel.setText("Player 1 X WON THE GAME!");
    		play =false;		}
    		else 	if(squares[2][0].getOccupied() == squares[2][1].getOccupied() &&squares[2][1].getOccupied()==squares[2][2].getOccupied() &&squares[2][2].getOccupied() == 1)
    		{playerlabel.setText("Player 1 X WON THE GAME!");
    		play =false;		}
    		
    			if(squares[0][0].getOccupied() == squares[0][1].getOccupied() &&squares[0][1].getOccupied()==squares[0][2].getOccupied() &&squares[0][2].getOccupied() == 2)
    		{playerlabel.setText("Player 2 O WON THE GAME!");
    		play =false;		}
    		else if (squares[1][0].getOccupied() == squares[1][1].getOccupied() &&squares[1][1].getOccupied()==squares[1][2].getOccupied() &&squares[1][2].getOccupied() == 2)
    		{playerlabel.setText("Player 2 O WON THE GAME!");
    		play =false;		}
    		else 	if(squares[2][0].getOccupied() == squares[2][1].getOccupied() &&squares[2][1].getOccupied()==squares[2][2].getOccupied() &&squares[2][2].getOccupied() == 2)
    		{playerlabel.setText("Player 2 O WON THE GAME!");
    		play =false;		}
    		
    		//Vertically win
    		 if (squares[0][0].getOccupied() == squares[1][0].getOccupied() &&squares[1][0].getOccupied()==squares[2][0].getOccupied() &&squares[2][0].getOccupied() == 1)
    		{playerlabel.setText("Player 1 X WON THE GAME!");
    		play =false;		}
    		else if (squares[0][1].getOccupied() == squares[1][1].getOccupied() &&squares[1][1].getOccupied()==squares[2][1].getOccupied() &&squares[2][1].getOccupied() == 1)
    		{playerlabel.setText("Player 1 X WON THE GAME!");
    		play =false;		}
    		else 	if(squares[0][2].getOccupied() == squares[1][2].getOccupied() &&squares[1][2].getOccupied()==squares[2][2].getOccupied() &&squares[2][2].getOccupied() == 1)
    		{playerlabel.setText("Player 1 X WON THE GAME!");
    		play =false;		} 
    
    		 if (squares[0][0].getOccupied() == squares[1][0].getOccupied() &&squares[1][0].getOccupied()==squares[2][0].getOccupied() &&squares[2][0].getOccupied() == 2)
    		{playerlabel.setText("Player 2 O WON THE GAME!");
    		play =false;		}
    		else if (squares[0][1].getOccupied() == squares[1][1].getOccupied() &&squares[1][1].getOccupied()==squares[2][1].getOccupied() &&squares[2][1].getOccupied() == 2)
    		{playerlabel.setText("Player 2 O WON THE GAME!");
    		play =false;		}
    		else 	if(squares[0][2].getOccupied() == squares[1][2].getOccupied() &&squares[1][2].getOccupied()==squares[2][2].getOccupied() &&squares[2][2].getOccupied() == 2)
    		{playerlabel.setText("Player 2 O WON THE GAME!");
    		play =false;		} 
    		//Diagonal win
    			 if (squares[0][0].getOccupied() == squares[1][1].getOccupied() &&squares[1][1].getOccupied()==squares[2][2].getOccupied() &&squares[2][2].getOccupied() == 1)
    		{playerlabel.setText("Player 1 X WON THE GAME!");
    		play =false;		}
    		else if (squares[0][2].getOccupied() == squares[1][1].getOccupied() &&squares[1][1].getOccupied()==squares[2][0].getOccupied() &&squares[2][0].getOccupied() == 1)
    		{playerlabel.setText("Player 1 X WON THE GAME!");
    		play =false;		}
    		
    		
    		 if (squares[0][0].getOccupied() == squares[1][1].getOccupied() &&squares[1][1].getOccupied()==squares[2][2].getOccupied() &&squares[2][2].getOccupied() == 2)
    		{playerlabel.setText("Player 2 O WON THE GAME!");
    		play =false;		}
    		else if (squares[0][2].getOccupied() == squares[1][1].getOccupied() &&squares[1][1].getOccupied()==squares[2][0].getOccupied() &&squares[2][0].getOccupied() == 2)
    		{playerlabel.setText("Player 2 O WON THE GAME!");
    		play =false;		}
    	
    	}//end win
    public void setTurn(int t)
    	{
    		turn = t;
    		playerlabel.setText( "X Player Plays First" );
    	}	
    	public void paintComponent( Graphics g )
       	{
          	super.paintComponent( g );
          	this.setBackground( Color.WHITE );
    
    		for (int i=0; i<3; i++){
    			for (int j=0; j<3; j++){
    				g.setColor( Color.BLACK );
          			g.drawRect( squares[i][j].getX(), squares[i][j].getY(), 50, 50 );	
    			}
    		}
          	
    		    	
          	
          	for (int i=0; i<3; i++){
    			for (int j=0; j<3; j++){
    				
          			myplayer[i][j].draw(g);	
    			}
    		}
       	}
       	
       	//End of paintComponent    
       
    }
    Last edited by java_fun2007; 05-18-2008 at 05:54 PM.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    C:\jexp>javac TicTacApplet.java
    
    C:\jexp>appletviewer TicTacApplet.java
    Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
            at Board.paintComponent(TicTacApplet.java:330)
            ...
    You must instantiate the elements of the myplayer array in the Board class constructor:
    Java Code:
    Square s = new Square( x,y );
    squares[i][j] = s;
    [b]myplayer[i][j] = new Player(x, y, String.valueOf(i*3+j+1));[/b]
    x +=50;

  3. #3
    java_fun2007 is offline Member
    Join Date
    Nov 2007
    Posts
    35
    Rep Power
    0

    Default

    Thanks Hardwired for your help, I don't know why my compiler doesn't detect this error also I can't see the applet when I use appletviewer or the html page to show the applet does it work for you?

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    I don't know why my compiler doesn't detect this error
    There may be a difference in the code you're compiling and what you posted here.
    I can't see the applet when I use appletviewer or the html page to show the applet does it work for you?
    Yes. Make sure you have an applet tag in a comment in your source file:
    Java Code:
    //  <applet code="TicTacApplet" width="400" height="400"></applet>
    Using an html file to launch an applet is difficult during development. Browsers and the java plug–in both cache applet class files and run the cached version instead of an/any updated versions. To avoid this you have to change the class name every time you compile it. Or you can use appletviewer for your applet developmment. If this is the trouble you're having you will need to change the class name to get started with appletviewer development.

  5. #5
    java_fun2007 is offline Member
    Join Date
    Nov 2007
    Posts
    35
    Rep Power
    0

    Default

    Thanks a lot hardwired now the problem has been solved :D I can't imagine how does a java program get effected by a comment like this APPLET TAG, it worked immediately after I wrote the comment.

Similar Threads

  1. Replies: 2
    Last Post: 03-30-2008, 09:38 PM
  2. when muse pressed the background change
    By pcman in forum Java Applets
    Replies: 1
    Last Post: 03-18-2008, 12:51 AM
  3. key pressed event
    By kavithas in forum New To Java
    Replies: 7
    Last Post: 12-10-2007, 03:01 PM
  4. mouse over on JButton
    By gradon in forum Java Applets
    Replies: 1
    Last Post: 08-04-2007, 06:50 AM
  5. Use the mouse position
    By susan in forum Java Applets
    Replies: 1
    Last Post: 07-29-2007, 12:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •