Results 1 to 10 of 10
  1. #1
    Fireking is offline Member
    Join Date
    Jul 2008
    Posts
    14
    Rep Power
    0

    Default addMouseMotionListener

    As mentioned in my previous post i following a tutorial on 2d graphics and im stuck :(
    the tutorial is java.sun.com/docs/books/tutorial/uiswing/painting/step3.html
    Im adding in code about addMouseMotionListener and it is red lining (im using netbeans)
    Java Code:
    package movement;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    
    public class mypanel extends JPanel{
        
        redSquare redSquare = new redSquare();
        
        public mypanel() {
            //set border size
            setBorder(BorderFactory.createLineBorder(Color.black));
            
            addMouseListener(
                    new MouseAdapter(){
                        public void mousePressed(MouseEvent e){
                            moveSquare(e.getX(), e.getY());
                    }
            });
            
            addMouseMotionListener(
                    new MouseAdapter(){ //*redline*
                        public void mouseDragged(MouseEvent e){ //*redline*
                            moveSquare(e.getX(), e.getY()); //*redline*
                        }
            })
            
        }// constructor
        
        
    public void paintComponent(Graphics g) {
            super.paintComponent(g);       
    
            // Draw Text
            g.drawString("This is my custom Panel!",10,20);
            
            //Draw Red Square
            redSquare.paintSquare(g);
            
        }  
    
    
        public Dimension getPreferredSize() {
            return new Dimension(500,400);
        }
        
        private void moveSquare(int x, int y){
            final int CURR_X = redSquare.getX();
            final int CURR_Y = redSquare.getY();
            final int CURR_W = redSquare.getWidth();
            final int CURR_H = redSquare.getHeight();
            final int OFFSET = 1;
            
            if ((CURR_X != x) || (CURR_Y != y)){ //checks if square has moved
                
                //repaints the background over the old square
                repaint(CURR_X, CURR_Y, CURR_W + OFFSET, CURR_H + OFFSET); 
                
                //update new coordiantes
                redSquare.setX(x);
                redSquare.setY(y);
                
                //repaint square at new location
                repaint(redSquare.getX(), redSquare.getY(), 
                        redSquare.getWidth() + OFFSET, 
                        redSquare.getHeight() + OFFSET);
                
                
            }
        }
        
    
    
    }
    the error it gives me is "addMouseMotionListener(java.awt.event.MouseMotion Listener) in java.awt.Component cannot be applied to (<anonymous java.awt.event.MouseAdapter>)"

    any help with why it would be doing this would be appreciated.

    Thx

    Fireking

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,277
    Rep Power
    25

    Default

    Only error I get is for a missing ; near end of constructor
    Can you post the full text of the error message?

  3. #3
    Fireking is offline Member
    Join Date
    Jul 2008
    Posts
    14
    Rep Power
    0

    Default

    sorry i did forget the ; when i rewrote it.
    i added the ; back in and i still get the 3 red lines and when i run it it gives

    "init:
    deps-jar:
    Compiling 2 source files to /Users/david/NetBeansProjects/movement/build/classes
    /Users/david/NetBeansProjects/movement/src/movement/mypanel.java:29: addMouseMotionListener(java.awt.event.MouseMotionL istener) in java.awt.Component cannot be applied to (<anonymous java.awt.event.MouseAdapter>)
    addMouseMotionListener(
    1 error
    BUILD FAILED (total time: 0 seconds)"

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,277
    Rep Power
    25

    Default

    As I said, I didn't get that error when I compiled the program. I did get undefined symbols for missing classes.
    How are you compiling the program? What version of the JDK do you have?
    Have you tried compiling it from the command prompt?

  5. #5
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    He's probably using an older version of the JDK, try downloading a new one. :)
    I die a little on the inside...
    Every time I get shot.

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

    Default

    Java Code:
            addMouseMotionListener(
                    new MouseAdapter(){ //*redline*
    You cannot use a MouseAdapter for/as a MouseMoionAdapter.
    Change the above to:
    Java Code:
            addMouseMotionListener(
                    new MouseMotionAdapter(){

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

    Default

    Quote Originally Posted by hardwired View Post
    You cannot use a MouseAdapter for/as a MouseMotionAdapter.
    Why not? The API tells me that I can as MouseAdapter implements the MouseMotionListener (which is what I think you meant) interface.

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

    Default

    I do see an unrelated problem here:
    Java Code:
     redSquare redSquare = new redSquare();
    There's some naming confusion here where your object name matches the class name. I strongly recommend that you don't do this, that you make the class name start with an upper-case letter and the object remain lower-case.

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

    Default

    Why not? The API tells me that I can as MouseAdapter implements the MouseMotionListener
    You're right. I hadn't noticed the addition of the MouseMotionListener and MouseWheelListener interfaces to MouseAdapter in j2se 1.6
    which is what I think you meant
    Yes, that is what I meant: I thought they were still mutually exclusive.
    Please disregard post #6.

  10. #10
    Fireking is offline Member
    Join Date
    Jul 2008
    Posts
    14
    Rep Power
    0

    Talking

    Right ok i think i may definatly be out of date, because what hardwired suggested worked (thank you very much) :) so i will look at updating.

    Thx Fubarable I will do this in the future :)

    Thx for everybodys help, much appreciated :D

    Fireking

Posting Permissions

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