Results 1 to 10 of 10
Thread: addMouseMotionListener
- 09-03-2008, 04:38 PM #1
Member
- Join Date
- Jul 2008
- Posts
- 14
- Rep Power
- 0
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)
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>)"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); } } }
any help with why it would be doing this would be appreciated.
Thx
Fireking
- 09-03-2008, 05:28 PM #2
Only error I get is for a missing ; near end of constructor
Can you post the full text of the error message?
- 09-03-2008, 05:33 PM #3
Member
- Join Date
- Jul 2008
- Posts
- 14
- Rep Power
- 0
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)"
- 09-03-2008, 07:47 PM #4
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?
- 09-03-2008, 09:36 PM #5
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
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.
- 09-03-2008, 11:41 PM #6
You cannot use a MouseAdapter for/as a MouseMoionAdapter.Java Code:addMouseMotionListener( new MouseAdapter(){ //*redline*
Change the above to:
Java Code:addMouseMotionListener( new MouseMotionAdapter(){
-
-
I do see an unrelated problem here:
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.Java Code:redSquare redSquare = new redSquare();
- 09-04-2008, 06:47 AM #9
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.
- 09-04-2008, 02:53 PM #10
Member
- Join Date
- Jul 2008
- Posts
- 14
- Rep Power
- 0


LinkBack URL
About LinkBacks


Bookmarks