Results 1 to 2 of 2
  1. #1
    susan is offline Member
    Join Date
    Jul 2007
    Posts
    44
    Rep Power
    0

    Default Use the mouse position

    I hope someone can enlighten me as I have no idea how to do what im trying to do.

    I have an applet which has a grid of 225 squares (15x15). The grid starts at 200, 0. The squares are 40x40. I have added a mouselistener and have coded it to execute a method on mousedown. What I am trying to do is make a method that finds the square that the mouse was clicked in, without coding 225 if(mouse is here) do something statements.

    What I am asking is if there is a way to get the x, y values of the corner of the square in which the user clicked. Example: the user clicks at 212, 5. The method will return 200, 0 (the x,y position of the corner of the topleft square in the grid).

    Java Code:
    	public void paintGrid(Graphics g)
    	{
    		
    		g.setColor(Color.gray);
    		
    		for(int x = 200; x < 800; x += 40)
    		{
    			for(int y = 0; y < 600; y += 40)
    			{
    				g.drawRect(x, y, 40, 40);
    			}
    		}
    	}
    Thanks

  2. #2
    marco is offline Member
    Join Date
    Jul 2007
    Posts
    23
    Rep Power
    0

    Default

    use this applet template i made
    all variables are 'private' variables, and this one responds to the mouse (x and y variables) as well as clicks and drags!! ^^ - you can also use the keyboard with this one (input in ASCII form tho)

    Java Code:
    //TestApplet
    import java.lang.*;
    import java.awt.*;
    import java.applet.*;
    
    public class TestNewApplet extends Applet implements Runnable
    {
    
        private int x, y, key; //Mouse X and Y
        private Thread pgm;  // program loop
        private boolean click, drag; // True when pressed.
        //All variables go here, having a 'private' infront of them
    
        public void init ()  //Sets bg and game start
        {
            pgm = new Thread (this);
            pgm.start (); // Start game thread.
        }
    
    
        public void run ()  //Calls the loop to run
        {
            mainProgram (); // Runs the game
        }
    
    
        private void mainProgram ()
        {
            Graphics c = getGraphics (); //Game graphics
            for (;;)
            {
                //all code goes here    
                if (click) //Check click to disallow multiple clicks on one mouse down
                    click = false;
                delay (10); //Refresh rate
            }
        } //Ends game loop method
    
    
        //Mouse methods for the program
        public boolean mouseMove (Event e, int mx, int my)
        {
            x = mx;
            y = my;
            click = false;
            drag = false;
            return true;
        }
    
    
        public boolean mouseDown (Event e, int mx, int my)
        {
            x = mx;
            y = my;
            click = true;
            drag = false;
            return true;
        }
    
    
        public boolean mouseUp (Event e, int mx, int my)
        {
            click = false;
            drag = false;
            x = mx;
            y = my;
            return true;
        }
    
    
        public boolean mouseDrag (Event e, int mx, int my)
        {
            click = false;
            drag = true;
            x = mx;
            y = my;
            return true;
        }
    
    
        public boolean keyDown (Event evt, int keyPressed)
        {
            key = keyPressed;
            return true;
        } // keyDown method
    
    
        public void delay (int tme)  //sets the delays within the game
        {
            try
            {
                Thread.sleep (tme);
            }
            catch (Exception err)
            {
            }
        }
    } //Ends main applet
    Last edited by levent; 07-28-2007 at 11:51 PM. Reason: Code is placed inside [code] tags!

Similar Threads

  1. Replies: 1
    Last Post: 03-28-2008, 10:26 AM
  2. BorderFactory to set position?
    By aleplgr in forum AWT / Swing
    Replies: 2
    Last Post: 01-30-2008, 12:46 PM
  3. How to get the position of character in TextArea
    By loganathan.lakshmanan in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 01-19-2008, 12:06 AM
  4. The mouse and the cheese
    By Don Quixote in forum Java 2D
    Replies: 4
    Last Post: 08-15-2007, 10:55 PM
  5. Help with position in java
    By fernando in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 07:54 PM

Posting Permissions

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