Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Help me calculate which element of the grid the mouse was in when it was clicked.

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    8
    My Mood
    Depressed
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Help me calculate which element of the grid the mouse was in when it was clicked.

    For example, if the user clicks in the top-left element, program could print out, on the console, "Mouse" clicked in element 0,0". So far this is what I have done. the program compiles but most of the results given are incorrect which does not match with the number of rows and columns.

     
    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseEvent;
     
     
    class GridPanel extends JPanel implements MouseListener
    {
    	int numCols;
    	int numRows;
     
     
     
     public void mouseReleased(MouseEvent event)
      {
      System.out.println("Mouse button is released");
     
     
      }
     
      public void mousePressed(MouseEvent event)
      {
      System.out.println("Mouse button is pressed");
     
      System.out.println("Mouse button is released");
      }
     
      public void mouseClicked(MouseEvent event)
      {
      System.out.println("Mouse is clicked");
      int x = event.getX();
    	int y = event.getY();
    	System.out.println("the x position is " + x);
    	System.out.println("the y position is " + y);
    	int numCols=5;
    	int numRows=7;
    	int ClickColIndex=numCols-1;
    	int ClickRowIndex=numRows-1;
    	int width = getWidth();
    	int height = getHeight();
    	System.out.println("width of pane= " + width);
    	System.out.println("height of pane= " + height);
    	ClickColIndex = (numCols * x)/(width);
    	ClickRowIndex= (numRows * y)/(height);
     
    	System.out.println ("Mouse clicked in element "+ClickRowIndex  + ","+ ClickColIndex);
     
      }
     
      public void mouseEntered(MouseEvent event)
      {
      System.out.println("Mouse cursor entered the screen");
      }
     
      public void mouseExited(MouseEvent event)
      {
      System.out.println("Mouse cursor exited the screen");
      }
     
     
    	public GridPanel(int nc, int nr)
    	{
    	addMouseListener(this);
    		numCols = nc;
    		numRows = nr;
    	}
     
    	Rectangle getRect(int thisCol, int thisRow)
    	{
     
    		if(thisCol <0 || thisRow < 0)
    			return null;
    		if(thisCol>=numCols || thisRow>=numRows)
    			return null;
     
     
    		int w = getWidth()/numCols;
    		int h = getHeight()/numRows;
     
    		int x = thisCol*w;
    		int y = thisRow*h;
     
    		Rectangle myRect = new Rectangle(x,y,w,h);
    		return myRect;
    	}
     
    	public void paint(Graphics g)
    	{
    		g.setColor(Color.gray);
    		g.fillRect(0,0,getWidth(), getHeight());
    		g.setColor(Color.black);
     
     
    		Graphics2D g2 = (Graphics2D)g;
     
     
    		for (int i = 0;i<numCols;i++)
    		{
    			for(int j = 0;j<numRows;j++)
    			{
    				Rectangle r = getRect(i,j);
    				g2.draw(r);
    			}
    		}
    	}
     
     
     
    	public static void main(String[] args)
    	{
    		W2MouseEvents w = new W2MouseEvents();
    		w.setVisible(true);
    	}
     
    }
    ------------------------------------------------------------------------------------------------------------------------------------
    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;
     
     
    public class W2MouseEvents extends JFrame
    {
    	GridPanel myGridPanel;
     
    	public static void main(String[] args)
    	{
    		W2MouseEvents w = new W2MouseEvents();
    		w.setVisible(true);
    	}
     
    	public W2MouseEvents()
    	{
    		setTitle("Workshop 2 (Mouse Events): starting code");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(500,220);
    		setLocation(300,300);
    		myGridPanel = new GridPanel(7, 5);
    		add(myGridPanel);
     	}
     
     }
    Last edited by sanjam58; February 20th, 2012 at 01:01 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,979
    Thanks
    56
    Thanked 2,359 Times in 2,330 Posts

    Default Re: Help me calculate which element of the grid the mouse was in when it was clicked.

    Can you post the output from the program and add comments to the output showing what you want the output to be?

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    8
    My Mood
    Depressed
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help me calculate which element of the grid the mouse was in when it was clicked.

    According to my program, I have made a grid which consists of 5 columns and 7 rows.. when I click on the top right element it gives me output 0,4 which is wrong...because it is on row no.6 and column no.0 which the answer should have been 6,0..... I believe there is a calculation error in the following part of the program which you can help me correct it.
    public void mouseClicked(MouseEvent event)
      {
      System.out.println("Mouse is clicked");
      int x = event.getX();
    	int y = event.getY();
    	System.out.println("the x position is " + x);
    	System.out.println("the y position is " + y);
    	int numCols=5;
    	int numRows=7;
    	int ClickColIndex=numCols-1;
    	int ClickRowIndex=numRows-1;
    	int width = getWidth();
    	int height = getHeight();
    	System.out.println("width of pane= " + width);
    	System.out.println("height of pane= " + height);
    	ClickColIndex = (numCols * x)/(width);
    	ClickRowIndex= (numRows * y)/(height);
     
    	System.out.println ("Mouse clicked in element "+ClickRowIndex  + ","+ ClickColIndex);
     
      }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,979
    Thanks
    56
    Thanked 2,359 Times in 2,330 Posts

    Default Re: Help me calculate which element of the grid the mouse was in when it was clicked.

    when I click on the top right element it gives me output 0,4 which is wrong...
    because it is on row no.6 and column no.0 which the
    answer should have been 6,0.
    Looks like you need to check your arithmetic and logic.
    Try clicking on each square on the top row, left to right
    and then on each square on the first column, top to bottom
    and post the output here.

    Your coordinates are different from what I am used to.
    0,4 would be the top row, and 4/5th column to the right
    How are you numbering the squares in your grid? Do you locate a square by giving its column and then row?

  5. The Following User Says Thank You to Norm For This Useful Post:

    sanjam58 (February 20th, 2012)

  6. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    8
    My Mood
    Depressed
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help me calculate which element of the grid the mouse was in when it was clicked.

    I am also new to co-ordinates and I am assuming rows (x-axis) and columns (y-axis)

    Output on each square on top row (left to right)
    (0,0)
    (0,1)
    (0,1)
    (0,2)
    (0,3)
    (0,3)
    (0,4)

    Output on each square on the first column (top to bottom)
    (0,0)
    (1,0)
    (3,0)
    (4,0)
    (6,0)

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,979
    Thanks
    56
    Thanked 2,359 Times in 2,330 Posts

    Default Re: Help me calculate which element of the grid the mouse was in when it was clicked.

    It looks like your arithmetic is faulty. Take a piece of grid paper and draw the grid and mark the x,y coords for each boundary line and work through your arithmetic to see why it is not giving you the numbers you want.

    I am assuming rows (x-axis) and columns (y-axis)
    That is not a standard way to look at it. I'm used to rows on the y-axis and rows on the x-axis.
    Your grid is on the side for me.
    I would show it this way:
    row1
    row2
    row3
    row4

    c c c
    o o o
    L L L
    1 2 3

  8. The Following User Says Thank You to Norm For This Useful Post:

    sanjam58 (February 20th, 2012)

  9. #7
    Junior Member
    Join Date
    Feb 2012
    Posts
    8
    My Mood
    Depressed
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help me calculate which element of the grid the mouse was in when it was clicked.

    Can you help me figure out which part of my Arithmetic is faulty and suggestion to get it right because am really stuck in this problem though-out the day.
    (Reference for assumming rows (x-axis) and columns (y-axis) : X-Axis and Y-Axis: An Easy Trick to Remember them Forever)
    Apologies for being dumb.

  10. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,979
    Thanks
    56
    Thanked 2,359 Times in 2,330 Posts

    Default Re: Help me calculate which element of the grid the mouse was in when it was clicked.

    First suggestion is to create two variables to hold the number of rows and the number of columns:
    final int NbrRows = 5;
    final int NbrCols = 7;

    Then use only their values in the rest of the code. You have 3 or 4 places where you use different values for the number of rows and columns. This is a problem. You should set the values ONE TIME and then use them as needed.

  11. The Following User Says Thank You to Norm For This Useful Post:

    sanjam58 (February 20th, 2012)

  12. #9
    Junior Member
    Join Date
    Feb 2012
    Posts
    8
    My Mood
    Depressed
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help me calculate which element of the grid the mouse was in when it was clicked.

    Thank you very much... your suggestion really helped me out...

  13. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,979
    Thanks
    56
    Thanked 2,359 Times in 2,330 Posts

    Default Re: Help me calculate which element of the grid the mouse was in when it was clicked.

    Glad you were able to solve your problem.

Similar Threads

  1. How to display data against row clicked on html table?
    By reubenmk in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: February 16th, 2012, 03:34 AM
  2. Table with CustomModel when clicked shows old entry
    By Nesh108 in forum AWT / Java Swing
    Replies: 6
    Last Post: November 9th, 2011, 06:38 AM
  3. GUI: JButtons aren't visible until clicked
    By Staticity in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 7th, 2011, 03:49 PM
  4. button update when clicked
    By prettynew in forum AWT / Java Swing
    Replies: 4
    Last Post: March 13th, 2011, 05:47 PM
  5. How can i point the mouse over a html element within the web browser?
    By bobomonkey in forum Java Theory & Questions
    Replies: 2
    Last Post: October 18th, 2009, 01:37 PM