Results 1 to 20 of 20
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Object follow mouse

  1. #1
    Joey0412 is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Object follow mouse

    So I am currently creating a game about a fish which needs to eat other fishes to grow.
    I control my fish with the mouse. I did it with the e.getX and e.getY code.
    But now I want to to follow the mouse with a limited speed, because it looks very unrealistic.
    Can anyone help me? thanks :p
    here's what I currently have:
    Java Code:
    package visje;
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    
    public class Visje extends Applet implements MouseMotionListener {
    
        private Image Gvis, MGroen, MBlauw;
        int X, Y;
        Graphics bufferGraphics;
        Image Offscreen;
        Dimension dim;
        
        @Override
        public void init() {
            Gvis = getImage( getDocumentBase(), "main_rechts.png" );
            MGroen = getImage( getDocumentBase(), "MiniVisGroen.png" );
            MBlauw = getImage( getDocumentBase(), "MiniVisBlauw.png" );
            setBackground( Color.cyan );
           
            addMouseMotionListener(this);
            
            setSize(1000,600);
            
            
        }
        
        @Override
        public void paint( Graphics g ) {
            //Spelervis
            g.drawImage( Gvis, X, Y, this );     
            
            //Kleine visgroep
            g.drawImage( MGroen, 120, 140, this );
            g.drawImage( MBlauw, 140, 80, this );
            g.drawImage( MBlauw, 180, 100, this );
            g.drawImage( MBlauw, 200, 90, this );
        }
    
        @Override
        public void mouseMoved(MouseEvent e) {
            X = e.getX();
            Y = e.getY();
            repaint();
        }
    
        @Override
        public void mouseDragged(MouseEvent e) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    
    }
    PS some names like MGreen are in dutch language (just so you know :))
    oh and I was busy putting in a buffer so dont mind that
    Last edited by Joey0412; 04-12-2012 at 09:59 PM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Object follow mouse

    Java Code:
    //Spelervis
    g.drawImage( Gvis, X, Y, this );
    I guess this is the annoying bit. In fact when you repaint you know (or could know) two things. The first is the last known mouse pointer position, X/Y. The second is the current position of the fish. At the moment you don't record that information, but you easily could. Declare a couple of variables, oldX and oldY then replace those lines with something like:

    Java Code:
    oldX += ???X???;
    oldY += ???Y???;
    g.drawImage(Gvis, oldX, oldY, this);
    ???X??? is some expression that represents the number of pixels *towards* the mouse pointer. A little maths will give this to you. If you apply Pythagoras' theorem you can keep ???X???-squared + ???Y???-squared equal to a small constant. This will keep the object's speed limited to something moderate.

    -----

    It would be a good idea to use standard Java coding conventions and start variables with a lowercase letter. Also consider using JApplet rather than Applet: the Swing painting mechanism implements double buffering of components itself.

  3. #3
    Joey0412 is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: Object follow mouse

    Yeah I already thought something like that but I don't know how to save the oldX while not changing it to the current X
    for example if when i have "X = e.getX();" and I add "oldX = X" then oldX is still the same as X.

    oh and what is the difference between JApplet and applet?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default Re: Object follow mouse

    Quote Originally Posted by Joey0412 View Post
    what is the difference between JApplet and applet?
    What did you read about the two classes in the API? Which part of that did you have trouble understanding?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Joey0412 is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: Object follow mouse

    well im new with java and I actually dont even know what API means :p

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default Re: Object follow mouse

    Bookmark one of these, depending on which version you're using:
    Java Platform SE 7
    Java Platform SE 6

    Frankly, I'm shocked that you're being taught Applets or any kind of GUI stuff before being taught to use the API.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    Joey0412 is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: Object follow mouse

    When I use JApplet, when I move my fish it doesn't remove the previous pictures of the fish.
    It's now like this:

    My code is still the same as above but now with JApplet extension
    do you know how to solve this...?

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Object follow mouse

    Use clearRect() to paint the cyan background before you do the rest of the painting.

    Declare variables to hold the information you need. Thinking about it, X and oldX are not good names: mouseX and fishX might be better. My suggestion was not that you make fishX equal to mouseX, but, rather, that you do some maths and make fishX change a certain amount in the direction of mouseX.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default Re: Object follow mouse

    Quote Originally Posted by pbrockway2 View Post
    Use clearRect() to paint the cyan background before you do the rest of the painting.
    More in line with standard practice, place a call to the super implementation at the top of the painting method override.
    Java Code:
    public void paint(Graphics g) { //paintComponent in Swing
        super.paint(g) // paintComponent in Swing
        // custom painting code
    }
    Why do you have declarations for variables that you don't use?
    Java Code:
    Graphics bufferGraphics;
    Image Offscreen;
    Dimension dim;
    db
    pbrockway2 likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default Re: Object follow mouse

    Quote Originally Posted by Joey0412 View Post
    My code is still the same as above but now with JApplet extension
    It shouldn't be. In Swing you shouldn't ever do custom painting by overriding paint(...) in a top level container. Go through the Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Object follow mouse

    Quote Originally Posted by DarrylBurke View Post
    More in line with standard practice, place a call to the super implementation at the top of the painting method override.
    I stand corrected.

  12. #12
    Joey0412 is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: Object follow mouse

    Quote Originally Posted by DarrylBurke View Post
    Why do you have declarations for variables that you don't use?
    Java Code:
    Graphics bufferGraphics;
    Image Offscreen;
    Dimension dim;
    db
    wasnt finished with that yet.

  13. #13
    Joey0412 is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: Object follow mouse

    So I now got it to work with a limited speed with this code:
    Java Code:
    package visje;
    import java.awt.*;
    import java.awt.Color.*;
    import java.awt.event.*;
    import java.awt.Graphics.*;
    import java.awt.Image.*;
    import javax.swing.JApplet;
    
    public class Visje extends JApplet implements MouseMotionListener {
    
        private Image main_vis, MGroen, MBlauw;
        int mouseX, mouseY, fishX, fishY;
        Graphics bufferGraphics;
        Image Offscreen;
        Dimension dim;
        
        @Override
        public void init() {
            main_vis = getImage( getDocumentBase(), "main_rechts.png" );
            MGroen = getImage( getDocumentBase(), "MiniVisGroen.png" );
            MBlauw = getImage( getDocumentBase(), "MiniVisBlauw.png" );
           
            addMouseMotionListener(this);
            setSize(1000,600);
        }
        
        @Override
        public void paint( Graphics g ) {
            super.paint( g );
            g.setColor( new Color (30, 144, 255) ); 
            g.fillRect( 0, 0, 1000, 600 );
            
            g.drawImage( main_vis, fishX, fishY, this );     
            g.drawImage( MGroen, 120, 140, this );
            g.drawImage( MBlauw, 140, 80, this );
            g.drawImage( MBlauw, 180, 100, this );
            g.drawImage( MBlauw, 200, 90, this );
    
        }
    
        @Override
        public void mouseMoved(MouseEvent e) {
            mouseX = e.getX();
            mouseY = e.getY();
            
            if( fishX < mouseX ){
                fishX +=2;
                main_vis = getImage( getDocumentBase(), "main_rechts.png" );
            }
            if( fishX > mouseX ){
                fishX -=2;
                main_vis = getImage( getDocumentBase(), "main_links.png" );
            }
            if( fishY < mouseY ){ 
                fishY +=2;
            }
            if( fishY > mouseY ){
                fishY -=2;
            }
            repaint();
        }
    
        @Override
        public void mouseDragged(MouseEvent e) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }
    but now it only moves to the mouse when the mouse moves, but I want it to move to the mouse even if the mouse doesnt move.
    Any idea?
    Last edited by Joey0412; 04-18-2012 at 02:13 PM.

  14. #14
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default Re: Object follow mouse

    The first movement will start after a mouseMoved, right? Start a javax/swing/Timer and move towards the last recorded mouse location. Stop the Timer when you reach there. Start or restart the Timer in the mouseMoved method implementation.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  15. #15
    Joey0412 is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: Object follow mouse

    Isnt it easier to say if(mouseX != fishX) and then the move code?
    Altough i dont know where to put it to get it to work

  16. #16
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default Re: Object follow mouse

    I already told you: use a Timer.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  17. #17
    Joey0412 is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: Object follow mouse

    I can't figure out how to use the timer, could you please help me with that?

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Object follow mouse

    Quote Originally Posted by Joey0412 View Post
    I can't figure out how to use the timer, could you please help me with that?
    I assume that you've gone through the Swing Timer tutorial, but if not, please go through that first. Then if you're still stuck, please show us what you've tried and where you're stuck.

  19. #19
    Joey0412 is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: Object follow mouse

    Yeah I did, but im still stuck...

  20. #20
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Object follow mouse

    Quote Originally Posted by Joey0412 View Post
    Yeah I did, but im still stuck...
    OK, well best of luck then.

    And again as I stated above, if you are still stuck, you'll want to show us what you've tried and describe exactly where you're stuck.

Similar Threads

  1. How to make object stop moving with mouse click
    By mackavelirip in forum New To Java
    Replies: 3
    Last Post: 05-06-2011, 03:23 AM
  2. Writing easy to follow code...
    By N00Bie in forum New To Java
    Replies: 11
    Last Post: 02-21-2011, 10:40 PM
  3. Help with mouse moving object and colisions
    By macwadu in forum AWT / Swing
    Replies: 12
    Last Post: 07-01-2010, 11:18 PM
  4. Mouse Listener for mouse floating over object?
    By Krooger in forum AWT / Swing
    Replies: 1
    Last Post: 11-18-2009, 04:34 AM
  5. change object color on mouse click
    By gotenks05 in forum Java Applets
    Replies: 1
    Last Post: 04-05-2009, 07:14 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
  •