Mouse listener

Reply

Join Date: Mar 2008
Posts: 56
Reputation: complexcodes is an unknown quantity at this point 
Solved Threads: 1
complexcodes complexcodes is offline Offline
Junior Poster in Training

Mouse listener

 
0
  #1
Oct 31st, 2008
I am trying to write a program that displays a rectangle when user drag the mouse and when user release mouse, it displays the rectangle with green color.

I am terribly confused here is what I am thinking to do
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;

public class DragRectangle extends JApplet
{
public void paint(Graphics g)
{
super.paint(g)
}
public void mouseDragged(MouseEvent e)
{
e.setColor(Color.GREEN)
e.drawRect(I am not sure what the coordinate will be)
repaint();
}

Am I in right track? Any suggestion or help will be highly appreciated!!
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,563
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 196
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Mouse listener

 
0
  #2
Oct 31st, 2008
I've never used MouseListener, but your mouseDragged code is wrong. "e" refers to a MouseEvent object, NOT your rectangle. Lets say you had an Object that represented your rectangle, here's what your code should look similar to.


  1. //Note this shouldn't be of type object, I'm just showing that it is an object
  2. Object myRectangle;
  3.  
  4.  
  5. public void mouseDragged(MouseEvent e){
  6. Object source= e.getSource();
  7.  
  8. if (source == myRectangle){
  9. myRectangle.setColor(whatever.GREEN);
  10. //call some repaint method here
  11. }
  12.  
  13. }
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,438
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Mouse listener

 
0
  #3
Oct 31st, 2008
Correct. The mouse event will supply the current coordinates of the mouse, which you can use to calculate your new rectangle coordinates. The paintComponent() method can then draw a rectangle with those coordinates when you call repaint().
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 56
Reputation: complexcodes is an unknown quantity at this point 
Solved Threads: 1
complexcodes complexcodes is offline Offline
Junior Poster in Training

Re: Mouse listener

 
0
  #4
Oct 31st, 2008
Sorry, I didn't get you. I understand that I cant use e, So I am using 'event' as my object.
Where you get that myREctangle? is that an object????
Thanks ,
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 56
Reputation: complexcodes is an unknown quantity at this point 
Solved Threads: 1
complexcodes complexcodes is offline Offline
Junior Poster in Training

Re: Mouse listener

 
0
  #5
Oct 31st, 2008
Ezzaral, can you show me some steps please?
so, am I in the right track????
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,438
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Mouse listener

 
0
  #6
Oct 31st, 2008
This should give you a start to work from. Similar, but different enough that you'll have to figure out the rectangle part.
  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3. import java.awt.Point;
  4. import java.awt.event.MouseEvent;
  5. import java.awt.event.MouseMotionListener;
  6. import javax.swing.JApplet;
  7. import javax.swing.JPanel;
  8.  
  9. public class PaintApplet extends JApplet {
  10.  
  11. public void init() {
  12. getContentPane().add(new CanvasPanel());
  13. }
  14.  
  15. class CanvasPanel extends JPanel implements MouseMotionListener{
  16. Color backColor = Color.WHITE;
  17. Color foreColor = Color.BLUE;
  18.  
  19. Point p0 = new Point(0,0);
  20. Point p1 = new Point(0,0);
  21.  
  22. public CanvasPanel(){
  23. super();
  24. addMouseMotionListener(this);
  25. }
  26.  
  27. @Override
  28. protected void paintComponent(Graphics g) {
  29. g.setColor(backColor);
  30. g.fillRect(0, 0, getWidth(), getHeight());
  31. g.setColor(foreColor);
  32. g.drawLine(p0.x, p0.y, p1.x, p1.y);
  33. }
  34.  
  35. public void mouseDragged(MouseEvent e) {
  36. p1.x = e.getX();
  37. p1.y = e.getY();
  38. repaint();
  39. }
  40.  
  41. public void mouseMoved(MouseEvent e) {
  42. // nothing here - just want drag operations
  43. }
  44. }
  45. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 56
Reputation: complexcodes is an unknown quantity at this point 
Solved Threads: 1
complexcodes complexcodes is offline Offline
Junior Poster in Training

Re: Mouse listener

 
0
  #7
Nov 1st, 2008
so far I got upto this part! can anyone help me how to proceed from here:
/codeimport javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;

public class DragRectangle extends JApplet
{
private int previousX, previousY;
private boolean drag;
/* public void init()
{
addMouseDragged(this);
addMouseReleased(this);
}*/
public void paint(Graphics g)
{
super.paint(g);
}
public void mouseDragged(MouseEvent event)
{
//e.setColor(Color.GREEN)
//e.drawRect(
if(drag==false)
{
return;
}

int x = event.getX();
int y = event.getY();
previousX=x;
previousY=y;
//Graphics g = getGraphics();
//event.setColor(Color.YELLOW);
//event.drawRect(x,y,10,20);
}
public void mouseReleased(MouseEvent evt)
{
g.setColor(Color.YELLOW);
g.drawRect(previousX, previousY, getWidth(),getHeight());
repaint();
}

}/code
thanks
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1,175
Reputation: stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light 
Solved Threads: 125
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

Re: Mouse listener

 
0
  #8
Nov 1st, 2008
@complexcodes
Seems like you are confused in using CODE tags use [ CODE=java] and [ /CODE ] (without the spaces between CODE and brackets)to cover your java code.
Last edited by stephen84s; Nov 1st, 2008 at 10:30 am.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

"How to ask questions the smart way ?"
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,438
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Mouse listener

 
0
  #9
Nov 1st, 2008
Why not start with the applet that I wrote for you that is very close to what you want to do? Did I completely waste my time trying to give you a simple, solid starting point?

If you will glance at that code, notice that all operations using the Graphics class occur in the paintComponent() method and those method calls just use the coordinate variables in the class. The mouse listener alters those coordinates based upon the x and y values supplied by the mouse event and then calls repaint(), which in turn calls paintComponent() as part of the rendering process.

All you need to do is alter the code in the mouse listener and the paintComponent() method to draw your rectangle instead of the simple line I used as an example.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,609
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Mouse listener

 
0
  #10
Nov 1st, 2008
@stephen84s

To exclude a piece of text as being interpreted as BBCode, use the noparse tag. With the use of noparse, you can write something along the lines of [code=java]your code here[/code] and be sure it won't be interpreted by the BBCode parser of vBulletin.

Source for above: [noparse][code=java]your code here[/code][/noparse]
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC