Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

mouse listener

Posted on 2003-03-27
4
Medium Priority
?
219 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
  • 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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month11 days, 21 hours left to enroll

564 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