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 9 of 9

Thread: Get the mouse x , y?

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Get the mouse x , y?

    So i have been working on a game and i want to shoot something but my missile insted of going to targetX and tarrgetY it goes little bit off, it can go only some direction what is the problem?

    asdf.jpg

    I want it to go in all of the directions but it doesn't! Please help!
    github.com/GNecro1/TROTMGRO


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Get the mouse x , y?

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.
    it can go only some direction what is the problem?
    How would we, could we know?

    Post your code in code tags per the above link that aims/shoots the missiles. Even better, post a short, runnable example that demonstrates the problem.

  3. #3
    Junior Member
    Join Date
    Jul 2014
    Location
    Canada
    Posts
    25
    My Mood
    Bored
    Thanks
    0
    Thanked 5 Times in 4 Posts

    Default Re: Get the mouse x , y?

    If you haven't already, add a class that implements MouseListener and MouseMotionListener to your main frame - whichever you're using of the awt version of Applet/Frames or the Swing JApplet/JFrame - using addMouseListener(this) and addMouseMotionListener(this). In your MouseListener implementation, add code in the overridden mouseMoved function to assign the MouseEvent's getX() and getY() return values to variables, then call them from your main class every repaint() and assign them to your targeting variables.

    Basically, read up on MouseListener and MouseMotionListener.

  4. #4
    Junior Member
    Join Date
    Jul 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Get the mouse x , y?

    Well i have posted the github! if you saw the image i posted you wouldn't tell me this, because i have already done that and i have the problem with my missiles going to the mouse x,y! And i don't use JPanel i use buffer strategy and i don't need to use repaint() because its done every time it can! I don't think that you have read the post but you only read the title of my post!

  5. #5
    Junior Member
    Join Date
    Jul 2014
    Location
    Canada
    Posts
    25
    My Mood
    Bored
    Thanks
    0
    Thanked 5 Times in 4 Posts

    Default Re: Get the mouse x , y?

    Note the very first line of my response - 'If you haven't already, ...'. I'm not going to dig through your entire project to debug your code for you. You gave a cursory explanation of your problem, so I gave a cursory response. If you want help, figure out where the problem is and provide more information than "I want it to go in all of the directions but it doesn't! Please help!".

  6. #6
    Junior Member
    Join Date
    Jul 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Get the mouse x , y?

    this is a code segment from one of my tower defense games.

    public static double[] bulletVel(SoldierParent soldier, ZombieParent zombie)
    	{
    		double velX = zombie.getX() - soldier.getX();
    		double velY = zombie.getY() - soldier.getY();
    		double distance = Math.pow((Math.pow(velX, 2)+Math.pow(velY, 2)), .5);
    		double speed = 24;
    		@SuppressWarnings("unused")
    		double checkSpeed = Math.pow((Math.pow(velX/distance*5, 2)+Math.pow(velY/distance*5, 2)), .5);
    		//System.out.printf("VelX = %f\tVelY = %f\tspeed = %f\n",velX, velY, checkSpeed);
    		double vel[] = {(velX/distance)*speed,(velY/distance)*speed};
    		return vel;
    	}

    I'm sure you can adapt the code to what you need.
    you can throw this in for the x and y coords.

    Point point = MouseInfo.getPointerInfo().getLocation();
    	int x = (int) point.getX();
    	int y = (int) point.getY();

  7. #7
    Junior Member
    Join Date
    Jul 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Get the mouse x , y?

    If you read the post you wouldn't sad 'If you haven't already....' Because i have said "my missile insted of going to targetX and tarrgetY it goes little bit off" when i said targetX and targetY i meant the mouse position!

  8. #8
    Junior Member
    Join Date
    Jul 2014
    Location
    Canada
    Posts
    25
    My Mood
    Bored
    Thanks
    0
    Thanked 5 Times in 4 Posts

    Default Re: Get the mouse x , y?

    There was no way for me to know that within the confines of your post. I'll refer back to what GregBrannon posted initially:

    Quote Originally Posted by GregBrannon View Post
    How would we, could we know?

    Post your code in code tags per the above link that aims/shoots the missiles. Even better, post a short, runnable example that demonstrates the problem.
    Provide some demonstrable code within your posts that indicates the issue you're having. If you aren't willing to do that, why should we be willing to sort through your code to figure out what you're trying to do and how to make it work?

    "targetX and tarrgetY" does not imply mouseX and mouseY. Even had you said mouseX and mouseY, there's no guarantee your implementation uses a MouseListener or a MouseMotionListener. Understand that you're asking people for help, and either provide a context for us to do so, or solve your problem on your own. Getting annoyed at the answers people who are helping you for free are giving is counter-productive.

    That said, I'll ask once more - where in your code is the issue occurring? Post the code that controls the trajectories of your projectiles in a response post, and it would be far easier to diagnose the issue you're having and solve it.

  9. #9
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Get the mouse x , y?

    Coming onto a forum that is dedicated to helping people with Java programming and
    being rude and negative towards people that are trying to help you understand the problem
    is not going to win you any friends here. A bit of common politeness and courtesy goes a
    long way.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

Similar Threads

  1. mouse listener on jtextarea
    By nirro in forum AWT / Java Swing
    Replies: 1
    Last Post: September 19th, 2013, 10:10 AM
  2. How to get Mouse Position even if it is not within our application?
    By Freaky Chris in forum Java Programming Tutorials
    Replies: 2
    Last Post: January 4th, 2012, 10:57 AM
  3. Mouse Listeners
    By newbie in forum AWT / Java Swing
    Replies: 2
    Last Post: November 27th, 2010, 11:08 PM
  4. [SOLVED] mouse getX and getY are off?
    By Brt93yoda in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 6th, 2010, 12:49 AM
  5. How to get Mouse Position even if it is not within our application?
    By Freaky Chris in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 29th, 2009, 12:41 PM