Results 1 to 5 of 5
  1. #1
    Don Quixote is offline Member
    Join Date
    Aug 2007
    Posts
    2
    Rep Power
    0

    Default The mouse and the cheese

    Hello people. I'm having a big problem on how to move an object, like a mouse in an matrix seeking for the cheese. I don't know if java supports this, but if you know, please let me know...
    Thanks

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    How do you have in mind to make the mouse move? With keyboard input, animation, dragging with the mouse? I would say that you can move the mouse with any of these means.

  3. #3
    cruxblack is offline Senior Member
    Join Date
    Jul 2007
    Posts
    130
    Rep Power
    0

    Default

    If ur gonna move the mouse (ur application mouse) using the mouse (the computer mouse that is) u can use the MouseListener and MouseMotionListener, though further details are available in the tutorials

    But if ur gonna use keyboard, well, i was about to ask the same thing too, how do u capture keyboard inputs actually?:confused:
    What listener to use? an example would be great :)

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    See How to Use Key Bindings
    and How to Use the Focus Subsystem for more information.
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.Ellipse2D;
    import javax.swing.*;
    
    public class MouseKeyMotion extends JPanel {
        Ellipse2D.Double ball = new Ellipse2D.Double(200,200,40,40);
        double dx = 3.0;
        double dy = 3.0;
    
        public MouseKeyMotion() {
            registerKeys();
            setFocusable(true);
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setPaint(Color.red);
            g2.fill(ball);
        }
    
        private void registerKeys() {
            int c = JComponent.WHEN_IN_FOCUSED_WINDOW;
            getInputMap(c).put(KeyStroke.getKeyStroke("UP"), "UP");
            getActionMap().put("UP", upAction);
            getInputMap(c).put(KeyStroke.getKeyStroke("LEFT"), "LEFT");
            getActionMap().put("LEFT", leftAction);
            getInputMap(c).put(KeyStroke.getKeyStroke("DOWN"), "DOWN");
            getActionMap().put("DOWN", downAction);
            getInputMap(c).put(KeyStroke.getKeyStroke("RIGHT"), "RIGHT");
            getActionMap().put("RIGHT", rightAction);
            getInputMap(c).put(KeyStroke.getKeyStroke("ESCAPE"), "ESCAPE");
            getActionMap().put("ESCAPE", escapeAction);
        }
    
        private void moveBall(int xInc, int yInc) {
            double x = ball.x + dx*xInc;
            double y = ball.y + dy*yInc;
            ball.setFrameFromDiagonal(x, y, x+ball.width, y+ball.height);
            repaint();
        }
    
        public static void main(String[] args) {
            JFrame f = new JFrame("Use arrow keys");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(new MouseKeyMotion());
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    
        private Action upAction = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                moveBall(0,-1);
            }
        };
    
        private Action leftAction = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                moveBall(-1,0);
            }
        };
    
        private Action downAction = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                moveBall(0,1);
            }
        };
    
        private Action rightAction = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                moveBall(1,0);
            }
        };
    
        private Action escapeAction = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        };
    }

  5. #5
    Don Quixote is offline Member
    Join Date
    Aug 2007
    Posts
    2
    Rep Power
    0

    Default Answer

    Yes, it was something like this that i had in mind... Thanks alot fellow... Now i just have to implement the code for the colision detection, cause i want to create a labyrinth, and the rat is suppose to chase the cheese...
    Got the idea, well alone this time, i'll try to do it... if i have problems i'll post something...
    Thanks

Similar Threads

  1. Mouse dragg not working
    By Preethi in forum AWT / Swing
    Replies: 1
    Last Post: 02-08-2008, 05:51 AM
  2. How to mouse-drag a JWindow?
    By cruxblack in forum New To Java
    Replies: 3
    Last Post: 08-06-2007, 10:52 AM
  3. mouse over on JButton
    By gradon in forum Java Applets
    Replies: 1
    Last Post: 08-04-2007, 06:50 AM
  4. Use the mouse position
    By susan in forum Java Applets
    Replies: 1
    Last Post: 07-29-2007, 12:10 AM
  5. Mouse over JButton
    By sandor in forum AWT / Swing
    Replies: 1
    Last Post: 05-17-2007, 10:15 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
  •