?
Solved

mouse listener

Posted on 2003-03-27
4
Medium Priority
?
213 Views
Last Modified: 2010-03-31
Hi all,
I want to paint on a panel using the mouse.
I have used the class MouseListener and MouseMotionListener in the following way:

    addMouseListener (
      new MouseAdapter (){
        public void mousePressed (MouseEvent e)
        {
          x1 = e.getX();
          y1 = e.getY();
        }
        public void mouseReleased (MouseEvent e)
        {
          x2 = e.getX();
          y2 = e.getY();
          LineData lineData = new LineData (x1,y1,x2,y2);
          vector.addElement (lineData);
          repaint();
        }
      }
    );

    addMouseMotionListener (
      new MouseMotionAdapter (){
        public void mouseDragged (MouseEvent e)
        {
          x2 = e.getX();
          y2 = e.getY();
          repaint();
        }
      }
    );
  }
  public void paintComponent (Graphics g)
  {
    super.paintComponent(g);
    g.drawLine(x1,y1,x2,y2);



The problem is that this way I'm painting only lines between the first and last points whereas I want to draw all the line I made sequentialy (not straight lines but curved ones).
Any help would be welcome.
Thanks,
edgar
0
Comment
Question by:etgaroy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8217661
You'll have to save the coordinates in a collection and paint them in sequence
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 60 total points
ID: 8217703
You're adding the lineData objects to the vector. Then why ar you writing g.drawLine ( x1, y1, x2, y2 ) ; ?? Like CEHJ said, you have to save the co-ordinates in a collection. Now since you're already doing that, in your paintComponent () method, please iterate through the vector and draw the lines represented by every element of the vector.

Mayank.
0
 

Author Comment

by:etgaroy
ID: 8217757
The vector is used for something else. I'm saving in the vector lines because I want to display of the lines drawn up to present.
I thought there is some way to do it without saving each point of the line in an array. Isn't there any option to do it?
Thanks,
edgar
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8217788
.... if you draw a line just when the mouse is released, between (x1, y1) and (x2, y2).

Mayank.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month13 days, 13 hours left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question