Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Mouse location
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Jonny Tight Lips




PostPosted: Fri Sep 09, 2005 3:42 pm   Post subject: Mouse location

I was wondering what / if there is a command to find the location of the mouse. In turing it is mousexy() or something like that. Of course being Java it has to be harder then that. I've been looking at some examples online but non of them make sence to me. So any help you can give would be nice. Thx
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Fri Sep 09, 2005 3:51 pm   Post subject: (No subject)

Have your class implement MouseMotionListener.

http://java.sun.com/j2se/1.5.0/docs/api/java/awt/event/MouseMotionListener.html
Jonny Tight Lips




PostPosted: Fri Sep 09, 2005 6:16 pm   Post subject: (No subject)

Well it compiles now but nothing happens. Now remember I'm new at this so there are probably a lot of mistakes so feel free to poit them out.
code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
                       
    public class pong extends JComponent implements MouseMotionListener  {
        Image ball = new ImageIcon("ball.gif").getImage();
        Image paddle = new ImageIcon("paddle.gif").getImage();
        int x = 100;
        int y = 100;
        int px = 10;
        int py = 10;
        int dx = 2;
        int dy = 2;
       
               
        public void begin(){
                       
                while (true){
            run2();

            }
        }
        public void mouseMoved(MouseEvent e){
                px = e.getX();
                py = e.getY();
        }
        public void mouseDragged(MouseEvent e){
        }
       
        public void paint(Graphics g) {
                g.drawImage(ball, x, y, this);
                g.drawImage(paddle,px,py,this);
        }

        public void run2() {

                 try {
                        x = x+dx ;
                        y = y+dy;
                        if (x+19 > 800){
                            dx = dx*-1;
                        }
                        if (y+19 > 500){
                            dy = dy*-1;
                        }
                        if (x<0){
                            dx = dx*-1;
                        }
                        if (y<0){
                            dy = dy*-1;
                        }
                       
                        repaint();
                        Thread.sleep(10);
                   
               
            } catch (Exception e) {
            }
        }
   
        public static void main(String[] args) {
            pong app = new pong();
                       
            JFrame frame = new JFrame();
           
            frame.getContentPane().add(app);
            frame.setSize(800, 500);
            frame.setVisible(true);
            app.begin();

        }
       
    }


as you can see I'm trying to make pong and use the mouse to move my paddle. I havn't got very far so yes there is still a lot missing. Just want to get the paddle to move to where the mouse is. Thx again for the help.
rizzix




PostPosted: Fri Sep 09, 2005 11:38 pm   Post subject: (No subject)

ok couple of things... now.. hmm bear with me... but it is really this complex..
Actually, nevermind... just to make things simple follow this simple rule always: "never modify the swing components outside the event handlers.." that will ensure ur application works as expected.. always..
Jonny Tight Lips




PostPosted: Sat Sep 10, 2005 10:33 pm   Post subject: (No subject)

As I said I am very new at this and Im not sure what all these terms mean. Mainly I really dont understand what the event handler is so if you could explain that it would be nice. Thx.
rizzix




PostPosted: Sat Sep 10, 2005 10:50 pm   Post subject: (No subject)

ehm... ok... let's see.. now where shall i start.. yep.. do you know what an interface it?
Jonny Tight Lips




PostPosted: Sun Sep 11, 2005 6:23 pm   Post subject: (No subject)

If by interface you mean User Interface then yes if you mean something else then no. Hmm I seem to be getting to feeling that I've gotten ahead of myself . I seem to be missing some important information that I should really know. If you can fill me in, that would be great but if you don't want to directing me to someplace that can would be nice. Thx for your help.
rizzix




PostPosted: Sun Sep 11, 2005 6:25 pm   Post subject: (No subject)

well there's a lot of stuff on this site that explains all this..

start of with wtd's An Introduction To Java and then there's my Abstract Class, Interface and Nested Class tutorial
Sponsor
Sponsor
Sponsor
sponsor
Jonny Tight Lips




PostPosted: Mon Sep 12, 2005 5:12 pm   Post subject: (No subject)

Finally got it working. Used a nested class. Thanks for all your help.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: