Results 1 to 4 of 4

Thread: mouseClicked

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2007
    Posts
    50

    mouseClicked

    Code:
    //Import Elements
    import java.awt.*;
    import java.applet.Applet;
    import java.awt.event.*;
    import java.awt.Graphics;
    import java.util.Date;
    
    public class matchingSquares extends Applet implements MouseListener
    {
    	card[] cards;
    	Image Cars;
    	card firstcard, secondcard, blankcard;
    
    	int pos;
    	int gstatus;
    	int tmp, x, y;
    	int rows, cols;
    	int width, height;
    	int W = cols * width;
    	int noGoes, noMatched;
    	String status;
    	String row, col;
    
    	public void init()
    	{
    		// Add the MouseListener to your applet
      		addMouseListener(this);
    
    		if (row == null)
    			rows = 4;
    		else
    
    		if (rows > 4 || rows < 0)
    			rows = 4;
    
    		if (col == null)
    			cols = 3;
    		else
    
    		if (cols > 3 || cols < 0)
    			cols = 3;
    		if (((rows %2) != 0) && ((cols %2) != 0))
    			cols = 3;
    
    		    width = 100;
          		height = 100;
    
    		cards = new card[rows * cols];
    		for(int i = 0, j = 0; i < rows * cols/2; i++, j+=2)
    		{
    			Cars = getImage(getDocumentBase(), "pic" +i+ ".jpg");
    			cards[j] = new card(Cars, j+1, i+1);
             	cards[j+1] = new card(Cars, j+2, i+1);
    		}
    			firstcard = new card();
    			secondcard = new card();
          		blankcard = new card();
    	}
    
    	public void paint(Graphics g)
    	{
    		g.clearRect(0, 0, W, 100);
    		for (int i = 0; i < rows * cols; i++)
    		{
    			if (cards[i].getID() <= rows*cols/2)
    		    {
    		    	pos = cards[i].getPos();
    		        g.setColor(Color.red);
    		        g.fillRect(xPos(pos), yPos(pos), 100, 100);
    		        g.setColor(Color.black);
    		        g.drawRect(xPos(pos), yPos(pos), 100, 100);
    		     }
    		}
    
    		if (firstcard.getID() != 0)
    		{
    			pos = firstcard.getPos();
    		    g.drawImage(firstcard.getImg(), xPos(pos), yPos(pos), this);
    		    g.drawRect(xPos(pos), yPos(pos), 100, 100);
    		}
    
    		if (secondcard.getID() != 0)
    		{
    			pos = secondcard.getPos();
    		    g.drawImage(secondcard.getImg(), xPos(pos), yPos(pos), this);
    		    g.drawRect(xPos(pos), yPos(pos), 100, 100);
          	}
    	}
    
    	public int xPos(int pos)
    	{
    		int col = cols;
    		tmp = pos % col;
    
    	    if (0 == tmp) tmp = col;
    	    	y = (tmp - 1) * width;
    	    return y;
    	}
    
    	public int yPos(int pos)
    	{
    		int col = cols;
    		tmp = pos/col;
    
    	    if (0 == pos%col) tmp = tmp - 1;
    	    	y = (tmp * height) + 100;
    	    return y;
    	}
    
    	public void mouseClicked(MouseEvent e)
    	{
    		x = e.getX();
    		y = e.getY();
    		repaint();
    	}
    
    	public boolean mouseClick(int x, int y)
    	{
    		if (y < 100)
    	    {
    	    	return true;
    	    }
    
    	    else if (cards[getmouseClick(x,y)].getID() > rows*cols/2)
    	    	return true;
    
    	    else if (0 == gstatus)
    	    {
    	    	firstcard = cards[getmouseClick(x,y)];
    	        gstatus = 2;
    	        status = "Choose Second Card";
    	        repaint();
    	        return true;
    	    }
    
    	    else if (1 == gstatus)
    	    {
    	    	firstcard = cards[getmouseClick(x,y)];
    	        gstatus = 2;
    	        status = "Choose Second Card";
    	        repaint();
    	        return true;
    	    }
    
    	    else if (2 == gstatus)
    	    {
    	    	if (firstcard == cards[getmouseClick(x,y)])
    	    		return true;
    	        	secondcard = cards[getmouseClick(x,y)];
    	        	gstatus = 3;
    	        	noGoes++;
    	        if (firstcard.getID() == secondcard.getID())
    	        {
    	        	status = "Well Done";
    	            noMatched++;
    	          	if (rows*cols/2 == noMatched)
    	            {
    	           		status = "Game Over - YOU WIN";
    	            }
    	        }
    	        else
    	        	status = "Please Try Again";
    	         	repaint();
    	         	return true;
    		}
    
    	    else
    	    	return false;
    	}
    
    	public int getmouseClick(int x, int y)
    	{
    		int row = (y-100)/height;
    	    int col = x/width;
    	    return (cols * row) + col;
    	}
    
    
    	public void mousePressed(MouseEvent e){}
    	public void mouseEntered(MouseEvent e){}
    	public void mouseReleased(MouseEvent e){}
    	public void mouseExited(MouseEvent e){}
    }
    when i press the mouse it should show the image of a car but it stays as a red square and does not show the picture, can someone please help me.

  2. #2

    Thread Starter
    Member
    Join Date
    Mar 2007
    Posts
    50

    Re: mouseClicked

    Code:
    import java.awt.*;
    
    public class card
    {
    	private Image noCars;
       	private int cPos;
       	private int carNo;
    
    	public card(Image img, int pos, int id)
    	{
    		noCars = img;
    	    cPos = pos;
    		carNo = id;
    	}
    
    	public card(Image img)
    	{
    		noCars = img;
    	    cPos = 0;
    	    carNo = 0;
    	}
    
    	public card()
    	{
    		noCars = null;
    	    cPos = 0;
    	    carNo = 0;
    	}
    
    	public Image getImg(){return noCars;}
    	public int getPos(){return cPos;}
    	public int getID(){return carNo;}
    
    	public void setImg(Image img){noCars = img;}
    	public void setPos(int pos){cPos = pos;}
    	public void setID(int id){carNo = id;}
    
    }
    this is the external file that is used for the cards

  3. #3
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: mouseClicked

    Make sure [getDocumentBase(), "pic" + i + ".jpg"] returns the path to the image
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  4. #4

    Thread Starter
    Member
    Join Date
    Mar 2007
    Posts
    50

    Re: mouseClicked

    k thanks i got it working now

Posting Permissions

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



Click Here to Expand Forum to Full Width