| | |
Mouse listener
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2008
Posts: 57
Reputation:
Solved Threads: 1
Ezzarel, I did run the applet with the code you provided. I am confused if I have to use that canvas mentioned in your code.
Here is my updated code, and I am getting one compile error. Can you plese check if my logic is correct :-
compile error:-
C:\Documents and Settings\Owner\My Documents\java\DragRectangle.java:6: DragRectangle is not abstract and does not override abstract method mouseExited(java.awt.event.MouseEvent) in java.awt.event.MouseListener
public class DragRectangle extends JApplet implements MouseListener
^
1 error
Tool completed with exit code 1
Here is my updated code, and I am getting one compile error. Can you plese check if my logic is correct :-
Java Syntax (Toggle Plain Text)
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.net.*; public class DragRectangle extends JApplet implements MouseListener { private int previousX, previousY; private boolean drag; public void init() { addMouseListener(this); } public void paint(Graphics g) { super.paint(g); g.setColor(Color.YELLOW); g.drawRect(previousX, previousY, getWidth(), getHeight()); } public void mouseDragged(MouseEvent event) { if(drag==false) { return; } int x = event.getX(); int y = event.getY(); previousX=x; previousY=y; } public void mouseReleased(MouseEvent evt) { repaint(); } }
compile error:-
C:\Documents and Settings\Owner\My Documents\java\DragRectangle.java:6: DragRectangle is not abstract and does not override abstract method mouseExited(java.awt.event.MouseEvent) in java.awt.event.MouseListener
public class DragRectangle extends JApplet implements MouseListener
^
1 error
Tool completed with exit code 1
•
•
Join Date: Sep 2008
Posts: 1,654
Reputation:
Solved Threads: 206
That error is because when you implement a class (which you did by saying DragRectangle implements MouseListener) you have to have all of the methods from the class.
Example: lets say these are the methods from MouseListener:
int mouseStuff();
Object moreMouseStuff();
Object otherMouseStuff();
That would mean that any time you write a class that 'implements MouseListener', your class would have to include the methods mouseStuff, moreMouseStuff, and otherMouseStuff. Specifically, the compiler is complaining because you need to have a method in your class that is declared as follows:
(Side note: what is the point of the code in your mouseDragged method that says if (drag==false)? It seems to me that you never initialized the variable, dragged. Also, it seems like the variable is useless anyway, since if mouseDragged gets called, its guaranteed that the mouse was dragged.)
Note: to the kid who is having trouble, you can ignore everything I say after this line... pay attention to what I said above. Everything after this point is directed at Ezzaral & others.
Documentation for the class can be found here:
http://java.sun.com/j2se/1.4.2/docs/...eListener.html
If you read the documentation, it says that mouseDragged is NOT a method supported by the class, so I am confused why he implemented it. It says to use MouseMotionListener. Am I looking at the wrong documentation somehow?
edit: I looked at the documentation for MouseMotionListener & back at Ezzeral's code, and it turns out that the kid just implemented the wrong class. So nevermind my confusion.
Example: lets say these are the methods from MouseListener:
int mouseStuff();
Object moreMouseStuff();
Object otherMouseStuff();
That would mean that any time you write a class that 'implements MouseListener', your class would have to include the methods mouseStuff, moreMouseStuff, and otherMouseStuff. Specifically, the compiler is complaining because you need to have a method in your class that is declared as follows:
Java Syntax (Toggle Plain Text)
public void mouseExited(MouseEvent e){ }
(Side note: what is the point of the code in your mouseDragged method that says if (drag==false)? It seems to me that you never initialized the variable, dragged. Also, it seems like the variable is useless anyway, since if mouseDragged gets called, its guaranteed that the mouse was dragged.)
Note: to the kid who is having trouble, you can ignore everything I say after this line... pay attention to what I said above. Everything after this point is directed at Ezzaral & others.
Documentation for the class can be found here:
http://java.sun.com/j2se/1.4.2/docs/...eListener.html
If you read the documentation, it says that mouseDragged is NOT a method supported by the class, so I am confused why he implemented it. It says to use MouseMotionListener. Am I looking at the wrong documentation somehow?
edit: I looked at the documentation for MouseMotionListener & back at Ezzeral's code, and it turns out that the kid just implemented the wrong class. So nevermind my confusion.
Last edited by BestJewSinceJC; Nov 1st, 2008 at 4:33 pm.
•
•
Join Date: Sep 2008
Posts: 1,654
Reputation:
Solved Threads: 206
Actually, you DO need to erase the part where it says "MouseListener", but not for the reason that you think. You were supposed to implement MouseMotionListener. So where it says MouseListener, change it MouseMotionListener. Then, go to the Java API for that class. (Link: http://java.sun.com/j2se/1.4.2/docs/...nListener.html). Go to the method summary and make sure that you have each of the methods in your code. Currently, you only have the mouseDragged method in your code, so you will have to add the mouseMoved method to your code.
Also, since you don't yet know how implementing classes works, GO READ THE JAVA TUTORIALS!
Interfaces:
http://java.sun.com/docs/books/tutor...interface.html
Also, since you don't yet know how implementing classes works, GO READ THE JAVA TUTORIALS!
Interfaces:
http://java.sun.com/docs/books/tutor...interface.html
Last edited by BestJewSinceJC; Nov 1st, 2008 at 4:27 pm.
•
•
Join Date: Sep 2008
Posts: 1,654
Reputation:
Solved Threads: 206
Read the link I gave you about MouseMotionListener. The method detail for mouseDragged states that as the user drags the mouse, MouseEvents continue to occur until the user stops by unclicking. Therefore, if you want the Rectangle to move wherever the user drags it, then you would have to call the repaint() method inside your mouseDragged code. The code that you posted earlier, in mouseDragged in post #11, will be useful for knowing where the mouse currently is. If you read the method details for mouseDragged, you will also notice that it will probably not help you with the part about "I want the rectangle to be green when the user stops dragging it." To make the Rectangle green, you will need to look into other methods. Hint: look into the MouseListener interface. You can do this by googling for Java MouseListener.
Last edited by BestJewSinceJC; Nov 1st, 2008 at 6:10 pm.
•
•
Join Date: Mar 2008
Posts: 57
Reputation:
Solved Threads: 1
I dont want my rectangle to be filled with green while dragging, rather I want my recatngle to be filled when I release my mouse(done with dragging). I dont think I need repaint method in mouseDragged method, do I?
Also, in mouse moved method, do I need to keep track of the coordinates?
This thing is so so so confusing! first time I am doin something related to mouseListener class
Also, in mouse moved method, do I need to keep track of the coordinates?
This thing is so so so confusing! first time I am doin something related to mouseListener class
•
•
Join Date: Sep 2008
Posts: 1,654
Reputation:
Solved Threads: 206
What I was saying in my last post is that you want the Rectangle to be moved to wherever you drag your mouse on the screen. This can be done by putting the correct code in mouseDragged. You also want the Rectangle to be colored green when the user unclicks. This can be done by implementing MouseListener, then putting the correct code in the correct method (read the description of the methods in the MouseListener javadoc!). So, in summary, you have to implement both MouseMotionListener and MouseListener.
When repaint() is called, I'm pretty sure it calls paintComponent. So you would have to write the code that does the actual redrawing of the Rectangle based on its position in paintComponent. But when you want to change the Rectangle's color, you can just put the code to change the color in the method that detects when the user unclicks. Then, right after that, you'd say repaint() so that the color change takes effect.
If you need clarification of anything I just said, I will clarify. Beyond that, I won't help anymore unless you post code and ask specific questions about it.
When repaint() is called, I'm pretty sure it calls paintComponent. So you would have to write the code that does the actual redrawing of the Rectangle based on its position in paintComponent. But when you want to change the Rectangle's color, you can just put the code to change the color in the method that detects when the user unclicks. Then, right after that, you'd say repaint() so that the color change takes effect.
If you need clarification of anything I just said, I will clarify. Beyond that, I won't help anymore unless you post code and ask specific questions about it.
Last edited by BestJewSinceJC; Nov 1st, 2008 at 7:20 pm.
•
•
Join Date: Mar 2008
Posts: 57
Reputation:
Solved Threads: 1
how can I make the rectangle disappear after I click the mouse anywhere in applet?
what line of codes do I need to add?
Thanks,
here is my working version of code:-
Thanks,
what line of codes do I need to add?
Thanks,
here is my working version of code:-
Java Syntax (Toggle Plain Text)
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class DragRectangle extends JApplet { private Point coordinate1[]=new Point[20]; private Point coordinate2[]=new Point[20]; private Point beg=new Point(); private Point last=new Point(); private int pointCounter=0; Rectangle rect = new Rectangle(); public void DrawRectangle() { addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent ev) { last = ev.getPoint(); rect.setFrameFromDiagonal(beg,last); repaint(); } }); addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { beg = e.getPoint(); } public void MouseReleased(MouseEvent ev) { coordinate1[pointCounter]=beg; coordinate2[pointCounter]= ev.getPoint(); pointCounter ++; rect.setFrameFromDiagonal(beg,beg); repaint(); } }); } public void paint(Graphics g) { super.paint(g); Graphics2D g2 = (Graphics2D)g; g2.setPaint(Color.black); g2.draw(rect); g2.setPaint(Color.yellow); Rectangle rect2 = new Rectangle(); for (int i =0; i < pointCounter; i++) { rect2.setFrameFromDiagonal(coordinate1[i], coordinate2[i]); g2.fill(rect2); } } }
Thanks,
![]() |
Similar Threads
- No action under action listener (ActionListener added!) (Java)
- Mouse Cursor Image (Java)
- JSpinner mouse event (Java)
- Adding mouse listener to a frame (Java)
- Java Visualizations with media (Game Development)
- Applet Help (Java)
- Is there a JOptionPane Listener? (Java)
- Java GUI--Dynamically Adding Buttons to a Menu (Java)
Other Threads in the Java Forum
- Previous Thread: can anyone find my mistake in TrainingZone.java
- Next Thread: Need help from java expert
Views: 2712 | Replies: 22
| Thread Tools | Search this Thread |
Tag cloud for Java
account android api apple applet application arguments array arrays automation binary bluetooth chat class classes client code columns component data database draw eclipse error event exception expand file filechooser fractal game givemetehcodez google graphics gui helpwithhomework homework html ide image inheritance input integer j2me java javaprojects jlabel jme jmf jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile monitoring netbeans newbie nullpointerexception number object open-source oracle print problem program programming project property recursion ria scanner screen search server set size sms socket sort sourcelabs splash sql sqlite static string swing test testautomation threads time transfer tree windows






