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.
Code java:
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);
}
}
------------------------------------------------------------------------------------------------------------------------------------
Code java:
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);
}
}
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
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.
Code java:
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);
}
Re: Help me calculate which element of the grid the mouse was in when it was clicked.
Quote:
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?
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)
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.
Quote:
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
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.
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.
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... ^:)^
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.