How can I drag points ?

hi, all
I am trying to achieve the following: I want to draw two points on a picture in the Jpanel with mouse, and wenn I release the mouse, there must be a line between the two points. if I just use mouse draged anyone of the point , it should be move with the mouse, and the line too. Can someone tell me how can I do it?
thank you very much!

import javax.swing.JFrame;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.Point;
import java.awt.event.MouseMotionListener;
public class TrivialApplication extends JFrame implements MouseListener, MouseMotionListener {
     private static MyPanel myPanel;
     private static PointGroupList pointList;
     private static Point currentPoint = null;
     public static void main(String args[]) {
          JFrame f = new TrivialApplication();
          f.show();
     public TrivialApplication() {
          setSize(400,400);
          pointList = new PointGroupList();
          myPanel = new MyPanel(400,400,pointList);
          getContentPane().add(myPanel);
          myPanel.addMouseListener(this);
          myPanel.addMouseMotionListener(this);
     public void mouseClicked(MouseEvent e) {
     public void mouseExited(MouseEvent e) {
     public void mouseEntered(MouseEvent e) {
     public void mousePressed(MouseEvent e) {
          int x = e.getX();
          int y = e.getY();
          currentPoint = pointList.getPointAt(x,y);
          if (currentPoint == null) {
               pointList.addPoint(x,y);
               myPanel.repaint();
     public void mouseReleased(MouseEvent e) {
     public void mouseMoved(MouseEvent e) {
     public void mouseDragged(MouseEvent e) {
          if (currentPoint != null) {
               currentPoint.x = e.getX();
               currentPoint.y = e.getY();
               myPanel.repaint();
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.Point;
public class MyPanel extends JPanel {
     private PointGroupList pointList;
     public MyPanel(int w, int h, PointGroupList pointList) {
          setPreferredSize(new Dimension(w,h));
          this.pointList = pointList;
     public void paintComponent(Graphics g) {
          super.paintComponent(g);
          g.setColor(Color.white);
        g.fillRect(0,0,getSize().width, getSize().height);
          for (int i = 0; i < pointList.size(); i++) {
               PointGroup group = pointList.getPointGroupAt(i);
               g.setColor(Color.red);
               Point one = group.getFirstPoint();
               g.fillRect(one.x-3,one.y-3,6,6);
               if (group.size() == 2 ) {
                    Point two = group.getSecondPoint();
                    g.fillRect(two.x-3,two.y-3,6,6);
                    g.setColor(Color.blue);
                    g.drawLine(one.x,one.y,two.x,two.y);
import java.util.Vector;
import java.awt.Point;
public class PointGroupList {
     private Vector list;
     private int counter = 0;
     public PointGroupList() {
          list = new Vector();
     public void addPoint(int x, int y) {
          if (counter == 0) {
               PointGroup group = new PointGroup();
               group.addPoint(x,y);
               list.add(group);
          } else {
               PointGroup group = getPointGroupAt(size()-1);
               group.addPoint(x,y);
          counter = (counter + 1) % 2;
     public PointGroup getPointGroupAt(int index) {
          PointGroup group = null;
          if (index < size() && size() != 0) group = (PointGroup) list.elementAt(index);
          return group;
     public int size() {
          return list.size();
     public Point getPointAt(int x, int y) {
          Point returner = null;
          for (int i = 0; i < size(); i++) {
               PointGroup group = getPointGroupAt(i);
               for (int j = 0; j < group.size(); j++) {
                    Point point = group.getPointAt(j);
                    if ( ( (point.x <= x+5) && (point.x >= x-5) ) &&
                          ( (point.y <= y+5) && (point.y >= y-5) ) ) {
                         returner = point;     
          return returner;
import java.awt.Point;
import java.util.Vector;
public class PointGroup {
     private Vector list;
     public PointGroup() {
          list = new Vector();
     public void addPoint(int x, int y) {
          Point point = new Point(x,y);
          list.add(point);
     public Point getPointAt(int index) {
          Point point = (Point) list.elementAt(index);
          return point;
     public Point getFirstPoint() {
          return getPointAt(0);
     public Point getSecondPoint() {
          return getPointAt(1);
     public int size() {
          return list.size();
}

Similar Messages

  • In Imovie '11, how can I drag more than one set of video clips to my project?

    In  Imovie '11, how can I drag more than one set of video clips into my project.  I know how to highlight in yellow and drag the set, but can I drag a group of sets?  Thanks

    To select multiple clips, hold down the command key as you click multiple clips. Then you can drag them all at once.
    To drag all clips at once, select one, then Edit/Select All. Then drag them all.

  • How can I drag a photo to the desktop

    How can I drag a photo to the desktop

    Assuming you are using iPhoto....
    You want to select the event that has the photo in it then click once on the photo so it is highlighted with a yellow ring around it and then click and drag it to the desktop.
    If you are finding it difficult to move the file that way you can select the picture in iPhoto and select
    File > Export (Command + Shift + E) and select how you want to export it (format) and where you want to put it .
    Hopefully this helps you.
    GL !

  • How can I drag and drop a Tidal job into another group?

    How can I drag and drop a Tidal job into another group? In Tidal Help, it says-
    In the Jobs pane, press and hold the Ctrl key and at the same time right-click and either individually select individual jobs within a job group or drag your mouse cursor over a block of jobs that you wish to select. You can only move multiple jobs between job groups if the jobs being moved are on the same level.
    Keeping the right mouse button depressed, drag the cursor to the job group that you wish to move the jobs to and release the mouse button.
    When moving jobs from one job group to another, you must decide if the moved jobs keep their original job group attributes or assume the calendar and agent characteristics of its new job group.
    A confirmation dialog, offers three inheritance options for the jobs moved to the "new" job group parent. Select one of the following options:
    However, when I attempt to follow those instructions, it doesn't work at all and there is no indication that it's even trying (i.e., I don't see the selected job following my cursor at all and all I get is the normal menu when you right click anywhere on the screen.)
    Is there another way to do this or is there a master setting that needs to be changed so that ability is enabled?
    Thanks.

    Go into edit mode for the job group group and just manually edit the section for Parent job/Group.
    Cheers

  • How can I drag and drop an icon/image into a panel??

    Dear Friends:
    How can I drag and drop an icon/image from one panel into another target panel at any position in target panel at my will??
    any good example code available??
    I search for quite a while, cannot find a very good one.
    please help
    Thanks
    Sunny

    [url http://java.sun.com/developer/JDCTechTips/2003/tt0318.html#1]DRAGGING TEXT AND IMAGES WITH SWING

  • How can I drag contacts into numbers like the old version?

    How can I drag contacts into numbers like the old version?

    Copy and paste works.  But drag and drop is also back in version 3.2. It works well here. Just drag and drop onto the canvas.Make sure you've upgraded to version 3.2 as earlier versions of Number 3 did not support drag and drop from Contacts.
    Note that the column headers are misaligned if you are dragging contacts that do not contain an image.  But the information in the body of the table drags in correctly.
    SG

  • How can i give  points to others

    plz tell how can i give points to others

    Hi,
    The place where you see my name,
    there will be stars of different colors, by selecting a star, you can assign points to them.
    If you think i have been helpful, click on the black star for me.
    Regards.

  • How can i drag videos off of my iPhone 5 onto my MacBook Pro?

    how can i drag videos off of my iPhone 5 onto my MacBook Pro?

    you'll need to import them
    try using iPhoto, if you don't have iPhoto then you can use image capture. If you can't find image capture type it into the finder.

  • How can I drag to the bookmarks bar?

    How can I drag to the bookmarks bar with iPad?

    You can't - Safari on the iPad can't be personalised in that way, all you can do is add bookmarks via the icon of the square with the arrow coming out of it

  • How can I drag & drop / copy & paste COPY Calendar event on iPad iOS6?

    How can I copy Calendar events on the iPad iOS 6?
    I have read various forums on this subject and have not found an answer on how to perform this basic function.
    To be clear, I am NOT asking how to drag &amp; drop MOVE an event, rather how to duplicate an event.
    In this way, I would not have to create a new event on a different date that is very similar to an existing one.
    If I have missed this in the manual just tell me where it is discussed and I will check it again.
    Thank you!

    Hi,
    I don't think it is possible to do that in iOS 5.
    Best wishes
    John M

  • How can I drag files from my macbook to my external hard drive?

    Hi! I can't seem to drag files to my hard drive using my macbook pro? How can I do that?

    Welcome to Apple Support Communities
    What happens when you try to drag a file to the external drive? If it just doesn't copy, it's because the external drive is formatted in NTFS, and OS X can't write in NTFS hard drives. There are some solutions:
    1. Buy a third-party application like Paragon NTFS, which will allow you to write in your external drive.
    2. Format the external drive with exFAT or FAT32, so you will be able to use the external drive with PCs and Macs.
    If you choose the last one, copy all the data from the external drive to a Mac or PC before erasing the disk. I recommend you to use exFAT because it hasn't got the limitation of files bigger than 4 GB, so you have to format the external drive on a PC. That's because, if you do it in the Mac, PCs won't recognize it

  • How can I drag my emails so they become appointments or tasks?

    At work, I use PC; At home, Apple.  I have always preferred my Apple to my PC - until this week!  I took a course on Working Smart with Outlook and have learned how I can just drag emails and copy/move them to my calendar or to-do list.  I cannot figure out how to do it with my Apple...  Anyone knows if (1) it is possible and (2) how to do it?
    Thanks,

    http://support.apple.com/kb/HT2109
    app data is part of the iphone backup.

  • How can I win points in my apple ID?

    How can I wın poınts ın my apple ID?

    Sorry, what does "win points" mean? Are you referring to points on this forum? If so, see the link below.
    https://discussions.apple.com/static/apple/tutorial/reputation.html

  • How can I assign points to the alternatives in a question?

    Hi! I am building my first quiz in Captivate and I have this little problem I haven´t been able to solve. I want a make a test that meassures how stress you are att work. It looks like a survey, I´ll have about 20 or more questions with the same type of alternatives, no right or wrong alternatives, but you choose the one you agree the most with. The thing is that i want to give a value point to each alternative and be able to have a score at the end. See the example.
    How often do you feel stressed att work?
    1. never : 0 points
    2. one a week: 2 points
    3. three times a week: 3 points
    4. Everyday: 4 points
    Att the end, the score you get tells you how stressed you are, but instead of showing a result in term of points, you´ll get a text giving you feedback and recomendations for better health. Can I do that in Captivate? I haven´t been able to find a way to do that yet...
    Thanks for any help you can give me!

    Hello,
    Tried putting something together that could be possible, concentrated on the workflow. There will be some repetitive tasks to be done however, problem is that a click box cannot stay for a whole project, which means that a click box gets its proper ID on each slide. Will try to explain, it is up to you to judge if this is acceptable, or if you'd go better with a Flash app.
    Created (for the moment, perhaps you'll need more if p.e. for Q1-Q10 you need a separate result for A and for B, result for Q11-Q20...) two user variables:
    v_AClick     to store the number of clicks on A-option
    v_BClick     to store the number of clicks on B-option
    The results can then be used later on for a condition (did not write it yet).
    Created a first slide with these objects:
    dummy Text Caption 'Option A' (will be replaced later on with question text)
    click box covering up this Text Caption, named it (ID) ClickA1 (this is the tedious part, has to be done for each CB; ClickA2, ClickA3...)
    dummy Text Caption 'Option B' (will be replaced later on with question text)
    click box covering up this Text Caption, named it (ID) ClickB1 (this is the tedious part, has to be done for each CB; ClickB2, ClickB3...)
    imported a 'sign' to be showed after the user clicked into the Library, and put an instance to the right of each Text Caption, those were labeled VinkA and VinkB. Very important: set those instances to 'Show for the entire project', which avoids to have them duplicated to each slide, we will hide and show them as necessary.
    button 'Next' which on Succes jumps to next slide.
    The blue Text Caption was for me, to check if the actions were functioning, shouldn't be there in the end file.
    I created a first Advanced action, labeled it HideVink (sorry for the Dutch, Vink = the green symbol), to be triggered on entering the slide:
    Hide VinkA
    Hide VinkB
    Second/third action, labeled A_Action1/B_Action1 to be triggered by clicking on the ClickA1/ClickB2 with these actions:
    increment v_AClick/v_BClick with 1
    hide the other clickbox, thus ClickB1/ClickA1 to avoid that the user could click on both options
    show AVink/BVink
    Here is an example, action A_Action1
    Those two actions have to be duplicated for each Question slide (A_Action2, B_Action2,....). What has to be changed in the duplicates is only the number of the Click Box, that is why I labeled them to make the process easier.
    Now I duplicated the slide as many times as necessary. The actions for button and entering slide are OK, but the actions for the click boxes have to be adapted (tedious).
    Why did I use 'dummy' Text Captions: I should export (File menu) the Text Captions to a Word-document, change the dummy texts (left column) to the real captions (much easier in one doc) and re-import them into the CP-file.
    That was my homework, as promised. Hope it helps, even if you choose to go the Flash-way after all.
    Lilybiri

  • How can I drag multiple photos into a new iPhoto library from search box?

    I lost but found my list of photos that were in my iPhoto library. I am unable to drag more than one photo at a time from the search box into the new iPhoto library box. Am I doing something wrong or do I have to drag one at a time? (That would take hours.) Please help if you can!
    Thanks,
    Laure

    Roam,
    Thanks again. I know how to highlight and drag multiple photos or a list of files, but the only place I found my list was by going to Finder>File>Find then a dialogue box comes up. In the dialogue box I put in iPhoto and checked that then put in "kind" "is" "image" and "size" "is greater than" "30" KB. When I click search, the entire list from my library comes up but that is when I am unable to drag more than one file at a time. The box that comes up when I search isn't (at least I don't think it is) a "folder" as such. Any suggesttions?
    Laure

Maybe you are looking for

  • Dynamic generation of form to show table entries

    Dear experts, I am aware of how to dynamically program for form generation when the layout static and is already defined in transaction SFP. However I have a requirement wherein the form layout is not available . But the form should get generated dyn

  • Stop Depreciation for a specific Dep Area

    All I am able to stop depreciation for a specific period. My requirement is to stop depreciation for a specific Depriciation Area -01. My client has 4 dep area's (01,02 & ..). Depriciation has stopped in both the depriciation areas (01&02). I had act

  • InputVerifier for JTextArea problem

    Hello, I am having a problem with using an InputVerifier to check a max character limit on a JTextArea. It seems that occasionally after the verify of the JTextArea fails, I lose the next character I type into it! Here is the code, followed by a desc

  • Arch from hdd to usb-stick?

    Hi all. After installing and playing with Arch on a normal desktop computer I have the required functionality and it work very well. But i'm struggling with how i should make the USB stick based on the installation i already have. root ~ # df -h File

  • Oracle8 Install Problems on Redhat 5.1 PC

    I just downloaded all the 805ship.* files & started the installation with cat 805ship.* | tar -zv . The installation quits in the middle & gives me an error. I dont have a clipping of the error with me. I see that some of the directories have been cr