public void init() 
 {
  // Assign values to the rectanagle coordinates.

  // Add the MouseListener to your applet
 }

 public void paint(Graphics g) 
 {
  // Rectangle's color
  g.setColor(Color.green);

  g.fillRect(rect1xco,rect1yco,rect1width,rect1height);

  g.setColor(Color.red);

  // When the user clicks this will show the coordinates of the click
  // at the place of the click..........[b]Why SO???[/b]
  g.drawString("("+xpos+","+ypos+")",xpos,ypos);

}

This is just a part of the code.......I deleted the non essential code....

What the code is supposed to do is to listen to mouseclicks inside the green rectangle and give out the co-ordinates......

First we select green color using g.setColor(Color.green)
then we create or fill up the rectangle with green

now after selecting red color, how does the program understand that g.drawString(......) implies co-ordinates of the mouse pointer? why can't assume an arbitrary value of xpos, ypos say 0,0

I am still learning, but you might have better luck here

http://math.hws.edu/javanotes/c6/s4.html

happy programming!

mitch9654

That is a fantastic resource............thanks a lot...

But I figured out how the code works......

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.