Problem!!! Moving point along the line

hi,
I need to move the point along the line. I want move this point using mouse.
My program draws a line and a point that' s on this line.
Could someone help me.

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.event.MouseInputAdapter;
public class TraverseAgain extends JPanel {
    Point2D.Double[] points;
    Point2D.Double p1;
    Point2D.Double p2;
    /** An endPoint of one of the lines is being moved. */
    public void setEndPoint(int index, double x, double y) {
        int start;
        double ratio;
        switch(index) {
            case 0:
                start = index == 0 ? 1 : 0;
                ratio = getRatio(p1, start, index);
                resetMidPoint(p1, points[start], x, y, ratio);
                break;
            case 1:
                start = index == 0 ? 1 : 0;
                ratio = getRatio(p1, start, index);
                resetMidPoint(p1, points[start], x, y, ratio);
                start = index == 1 ? 2 : 1;
                ratio = getRatio(p2, start, index);
                resetMidPoint(p2, points[start], x, y, ratio);
                break;
            case 2:
                start = index == 1 ? 2 : 1;
                ratio = getRatio(p2, start, index);
                resetMidPoint(p2, points[start], x, y, ratio);
                break;
            default:
                System.out.println("illegal index: " + index);
        points[index].setLocation(x, y);
        repaint();
     * Design assumption/decision:
     * Preserve this ratio when repositioning a midPoint between the
     * end points of its line when one of the end points is being moved.
    private double getRatio(Point2D.Double p, int start, int end) {
        double d1 = points[start].distance(p);
        double d2 = points[start].distance(points[end]);
        return d1 / d2;
    private void resetMidPoint(Point2D.Double p, Point2D.Double end,
                               double x, double y, double ratio) {
        double dy = y - end.y;
        double dx = x - end.x;
        double theta = Math.atan2(dy, dx);
        double length = end.distance(x, y);
        x = end.x + length*ratio*Math.cos(theta);
        y = end.y + length*ratio*Math.sin(theta);
        p.setLocation(x, y);
    /** A midPoint is being moved. */
    public void setMidPoint(int number, double x, double y) {
        switch(number) {
            case 1:
                double dx = points[1].x - points[0].x;
                double dy = points[1].y - points[0].y;
                if(points[0].x < x && x < points[1].x ||
                   points[1].x < x && x < points[0].x)
                    y = points[0].y + (x - points[0].x)*(dy / dx);
                else if(points[0].y < y && y < points[1].y ||
                        points[1].y < y && y < points[0].y)
                    x = points[0].x + (y - points[0].y)*(dx / dy);
                else
                    return;
                p1.setLocation(x, y);
                break;
            case 2:
                dx = points[2].x - points[1].x;
                dy = points[2].y - points[1].y;
                if(points[1].x < x && x < points[2].x ||
                   points[2].x < x && x < points[1].x)
                    y = points[1].y + (x - points[1].x)*(dy / dx);
                else if(points[1].y < y && y < points[2].y ||
                        points[2].y < y && y < points[1].y)
                    x = points[1].x + (y - points[1].y)*(dx / dy);
                else
                    return;
                p2.setLocation(x, y);
                break;
            default:
                System.out.println("illegal number " + number);
        repaint();
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
        if(points == null)
            initGeoms();
        g2.setPaint(Color.blue);
        for(int j = 1; j < points.length; j++)
            g2.draw(new Line2D.Double(points[j-1].x, points[j-1].y,
                                      points[j].x,   points[j].y));
        g2.setPaint(Color.red);
        for(int j = 0; j < points.length; j++)
            mark(points[j], g2);
        g2.setPaint(Color.green.darker());
        mark(p1, g2);
        mark(p2, g2);
    private void mark(Point2D.Double p, Graphics2D g2) {
        g2.fill(new Ellipse2D.Double(p.x-2, p.y-2, 4, 4));
    private void initGeoms() {
        int w = getWidth();
        int h = getHeight();
        points = new Point2D.Double[3];
        points[0] = new Point2D.Double(w/4, h/3);
        points[1] = new Point2D.Double(w/16, h/2);
        points[2] = new Point2D.Double(w*13/15, h*3/4);
        double cx = points[0].x + (points[1].x - points[0].x)/2;
        double cy = points[0].y + (points[1].y - points[0].y)/2;
        p1 = new Point2D.Double(cx, cy);
        cx = points[1].x + (points[2].x - points[1].x)/2;
        cy = points[1].y + (points[2].y - points[1].y)/2;
        p2 = new Point2D.Double(cx, cy);
    public static void main(String[] args) {
        TraverseAgain test = new TraverseAgain();
        PointMover mover = new PointMover(test);
        test.addMouseListener(mover);
        test.addMouseMotionListener(mover);
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(test);
        f.setSize(400,400);
        f.setLocation(200,200);
        f.setVisible(true);
class PointMover extends MouseInputAdapter {
    TraverseAgain traverseAgain;
    int selectedEndIndex = -1;
    int selectedMidNumber = -1;
    Point2D.Double offset = new Point2D.Double();
    boolean dragging = false;
    final int MIN_DIST = 5;
    public PointMover(TraverseAgain ta) {
        traverseAgain = ta;
    public void mousePressed(MouseEvent e) {
        Point p = e.getPoint();
        Point2D.Double[] points = traverseAgain.points;
        for(int j = 0; j < points.length; j++) {
            if(points[j].distance(p) < MIN_DIST) {
                selectedEndIndex = j;
                rigForDrag(points[j], p);
                break;
        if(selectedEndIndex == -1) {
            if(traverseAgain.p1.distance(p) < MIN_DIST) {
                selectedMidNumber = 1;
                rigForDrag(traverseAgain.p1, p);
            } else if(traverseAgain.p2.distance(p) < MIN_DIST) {
                selectedMidNumber = 2;
                rigForDrag(traverseAgain.p2, p);
    private void rigForDrag(Point2D.Double p, Point loc) {
        offset.x = loc.x - p.x;
        offset.y = loc.y - p.y;
        dragging = true;
    public void mouseReleased(MouseEvent e) {
        dragging = false;
        selectedEndIndex = -1;
        selectedMidNumber = -1;
    public void mouseDragged(MouseEvent e) {
        if(dragging) {
            double x = e.getX() - offset.x;
            double y = e.getY() - offset.y;
            if(selectedEndIndex != -1)
                traverseAgain.setEndPoint(selectedEndIndex, x, y);
            else
                traverseAgain.setMidPoint(selectedMidNumber, x, y);
}

Similar Messages

  • I have just redeemed my iTunes card of 25 dollars, I have 23 dolIars left now, I tried downloading a few songs later, but it says something along the lines of credit mismatch mobile message, I can't download anything with credit. How do I fix the problem?

    I really need help, I purchased an iTunes card about an hour ago and downloaded two things, I tried to download a couple songs but it won't let me, every time I try it says something along the lines of credit mismatch mobile message. How do I fix this? It says I have 23 dollars left but it's not working. How do I fix the problem? Please help!

    its very annoying but you have to log out of your account. than try to buy the sone. it will ask you to sign in. do it. the sond will start to download. it only works once, so you have to do it over and over for every song :/

  • ITunes unable to recognize iPod, error message along the lines if "iTunes has detected an iPod but unable to recognise" I have followed all the trouble shooting tips and am still stuck? I am now being told by a friend I need a link sent to me to fix this

    Hi,
    I am need of help! I have had this issue for a while now and it's becoming frustrating.
    My iTunes installed on my HP Windows 8 Netbook is unable to recognize my iPod, the error message along the lines of "iTunes has detected an iPod but unable to recognise"
    I have followed all the trouble shooting tips and am still stuck? I am now being told by a friend I need a link sent to me to fix this??
    Someone please help me fix this problem.
    Many thanks.
    Matt.

    Hi
    So just close iTunes and re-open iTunes thats it and that worked for you?
    When you say reset your settings do you mean to factory settings? As my iPod was swapped at the store for a new one and the problem is still there...
    Thanks.

  • Suddenly I have problems moving files to the trash...  get a dialog saying Finder wants to make changes and I must provide the password... Any explanation or ideas how I can get this to stop?

    Suddenly I have problems moving files to the trash...  get a dialog saying Finder wants to make changes and I must provide the password... Any explanation or ideas how I can get this to stop?

    Please take these steps if you're prompted for a password when moving items in your home folder to the Trash.
    1. Triple-click anywhere in the line below on this page to select it:
    ~/.Trash
    2. Right-click or control-click the highlighted line and select
    Services ▹ Show Info
    from the contextual menu.* An Info dialog should open.
    3. The dialog should show "You can read and write" in the Sharing & Permissions section. If that's not what it shows, click the padlock icon in the lower right corner of the window and enter your password when prompted. Use the plus- and minus-sign buttons to give yourself Read & Write access and "everyone" No Access. Delete any other entries in the access list.
    4. In the General section, uncheck the box marked Locked if it's checked.
    5. From the action menu (gear icon) at the bottom of the dialog, select Apply to enclosed items and confirm.
    6. Close the Info window and test.
    *If you don't see the contextual menu item, copy the selected text to the Clipboard by pressing the key combination command-C. Open a TextEdit window and paste into it (command-V). Select the line you just pasted and continue as above.

  • I think my computer has a virus, when I get on the Internet, ads will pop up all the time, I think it is called adware?  Also pages will pop up saying something along the lines of my computer is at risk for some security issue. What should I do?

    I think my computer has a virus, when I get on the Internet, ads will pop up all the time, I think it is called adware?  Also pages will pop up saying something along the lines of my computer is at risk for some security issue. What should I do?

    Remove Browser Pop-up Problems
         AdwareMedic
         Adblock Plus 1.8.9
         remove adware that displays pop-up ads and graphics on your Mac
    Helpful Links Regarding Malware Problems
    If you are having an immediate problem with ads popping up see The Safe Mac » Adware Removal Guide and AdwareMedic. If you require anti-virus protection Thomas Reed recommends using ClamXAV. (Thank you to Thomas Reed for this recommendation.)
    Open Safari, select Preferences from the Safari menu. Click on Extensions icon in the toolbar. Disable all Extensions. If this stops your problem, then re-enable them one by one until the problem returns. Now remove that extension as it is causing the problem.
    The following comes from user stevejobsfan0123. I have made minor changes to adapt to this presentation.
    Fix Some Browser Pop-ups That Take Over Safari.
    Common pop-ups include a message saying the government has seized your computer and you must pay to have it released (often called "Moneypak"), or a phony message saying that your computer has been infected, and you need to call a tech support number (sometimes claiming to be Apple) to get it resolved. First, understand that these pop-ups are not caused by a virus and your computer has not been affected. This "hijack" is limited to your web browser. Also understand that these messages are scams, so do not pay any money, call the listed number, or provide any personal information. This article will outline the solution to dismiss the pop-up.
    Quit Safari
    Usually, these pop-ups will not go away by either clicking "OK" or "Cancel." Furthermore, several menus in the menu bar may become disabled and show in gray, including the option to quit Safari. You will likely have to force quit Safari. To do this, press Command + option + esc, select Safari, and press Force Quit.
    Relaunch Safari
    If you relaunch Safari, the page will reopen. To prevent this from happening, hold down the 'Shift' key while opening Safari. This will prevent windows from the last time Safari was running from reopening.
    This will not work in all cases. The shift key must be held at the right time, and in some cases, even if done correctly, the window reappears. In these circumstances, after force quitting Safari, turn off Wi-Fi or disconnect Ethernet, depending on how you connect to the Internet. Then relaunch Safari normally. It will try to reload the malicious webpage, but without a connection, it won't be able to. Navigate away from that page by entering a different URL, i.e. www.apple.com, and trying to load it. Now you can reconnect to the Internet, and the page you entered will appear rather than the malicious one.

  • I bought my I pod touch 3-4 months ago and I got this message all of a sudden, it said something along the lines of provisonal complete_Distrib... will expire in 30 days. What is this it's under setting-general-Profile should I be worried?

    I bought my I pod touch 3-4 months ago and I got this message all of a sudden, it said something along the lines of provisonal complete_Distrib... will expire in 30 days. What is this it's under setting-general-Profile should I be worried? Should I just let it expire.

    Unless you have a company or school iPod, that profile was installed by an app you installed. It is likely that the app will no longer function when that date is reached. You can Google the name of the profile and that may provide some more information.

  • Iphone 4s stuck during OS upgrade 1/2 way along the line under the Apple LOGO.  what can i do to unstick it.

    during an upgrade to the latest OS version on our IPHONE 4S, it stopped updating about 1/2 way along the line under the APPLE LOGO.  what can I do to complete, or restart the update?

    Hi ..
    Depends on whether you're doing this wirelessley or using iTunes >  Update your iPhone, iPad, or iPod touch
    iOS: Unable to update or restore

  • Problems moving applications to the trash

    I'm having trouble with moving some items from my harddrive to the trash. I get the message that the item cannot be moved to the trash because it is "owned by the root". Does this sound familiar to anyone? I'd love to figure out what the deal is and how I can detete these files. Thanks!

    Elizabeth.....
    Use Terminal to ...
    Force-delete an item from desktop or Trash....this also will remove a locked
    file/folder and a “file that is in use”
    1. Launch Terminal...type/copy/paste...space after rm
    sudo rm -rf <space> ..this deletes file or folder
    sudo rm -R <space> this deletes file or folder
    2. Drag the offending item into the Terminal window. The path to it will appear.
    •To force-delete more than one item from the trash....click a item and press Shift or Command as you click additional items .
    3. Click on Terminal window, press Enter
    4. type your administrator password. Press Enter
    5. Click on the Desktop or the Trash window and the file will vanish.
    Note.. If you attempt the above again within five minutes, step 4 are not required.
    • If this does not work, then visit The XLab FAQs ....
    http://www.thexlab.com/faqs/faqs.html
    and read the FAQ on Trash problems for additional solutions.
    george

  • Along the lines of image resizing

    was having similar issues with "grouped" graphics (lines, rectangle, circles) created in Keynote. I tried to resize the collection and it went " winkey" (techo-speak for 'didn't respond as expected'). I tried toggling the constrained proportions options to no effect, I also noticed, when I went to key in a size, instead of filling the height/ width box, the keyed enter would end up on the screen like a text box. ? ? ?
    The old ms Windows trick of exiting the application a restating seemed to "fix" the key enter problem for a little while.
    Any ideas comments, remedies?
    Mac Pro intel Mac OS X (10.5.6) iworks 09 (demo)

    Simmilar to whom, MM Holden?
    This thread will give you some background: Resizing
    Other than that use guides and smaller groups to help with alignments. Until you provide specifics hard to help more than that.

  • Problem Moving Clips in the Timeline

    When I edited previously, I was able to drag one clip close to another, and it would automatically go directly next to that clip in the timeline. Also, when using the razor tool, bringing the razor close to the playhead would cause the cut to be made exactly where the playhead was in the timeline. This suddenly stopped happening--now when placing one clip next to another, it does not automatically stop where the next clip begins, and I have to be very careful so that they don't overlap in the timeline. The razor tool is likewise not drawn to the playhead.
    I'm sure that I must have inadvertently hit a button that caused this, but cannot figure out what I did. I've looked around trying to find out how to fix the problem, but I haven't been able to find anything. I'm sure the answer is probably quite obvious, so I appreciate the understanding, and I would be very grateful for any help.

    Welcome to the forum!
    Sounds like you have inadvertently turn Snapping off. The n key toggles it off and on. There should also be a button near the upper right of the Timeline window for it.
    -DH

  • ...along the lines of Quiz Results...Captivate3

    Greetings all,
    Now I have another problem that I think you advanced users can solve; it has to do with reviewing quiz results.  The blurred screen shot is above.
    As you can see this is the bottom of a question the student answered.  The problem is the rectangle box; it cannot be resized!  I've done as one person suggested on another discussion which seemed like a simple fix; to simply enlarge the review area on the storyboard.  I had all ready thought of that but it still did not work.
    I have expanded the 'review area' on the storyboard slide but this does not have an effect on the published version.  This is for a fill in the blank question and there are three blanks on the screen.   One might think this would be the problem but it happens even with multiple quess questions; I cannot find the reporting box for this.  I think it might be because the Quiz Preferences has a default set and cannot be edited for longer answers.
    Thanks so much,
    Captv8Novice

    Your firmware may have not worked ...in which case you'll need to create a CD from which to boot up with and then try the process again. This article explains it a little better...
    http://www.apple.com/support/downloads/firmwarerestorationcd12.html

  • Hi, I am having trouble with my Internet on my Macbook. Every time I try and go to a site it has a pop up block that says something along the lines of "Safari can't verify the identity of the website."

    Below that there is more, it says," The certificate for this website is invalid. You might be connecting to a website that is pretending to be (Whatever the name of the site is) which could put your confidential info at risk. Would you like to connect to the website anyway?" and it give the options of show certificate, cancel, and continue. But the thing i dont understand is that it does this for basically EVERY page i visit. even facebook. and when i click "continue" it alows me to go to the page but limits what i can do and only shows certain things. i'm very frusterated with it. Help Me!

    This issue was resolved! I didnt think about restoring my computer to factory settings but I did and now my computer works like brand new. basically, just back up your files and do the following:
    http://www.youtube.com/watch?v=yPmT5Xbb8p0

  • HT1923 Each time i connect my ipod touch i get a message along the lines of 'This ipod is not reconised in this version od itunes. Uninstall this version and reinstall the 64-bit version'. I have done this from Apple's website twice now but I still get th

    Hi
    Can anyone help? I am trying to connect my sons iPod to burn some music to it but I keep getting a message pop up stating that this version of iTunes is not compatible with this iPod and I need to remove my version of iTunes and install the 64-bit version. I have done this twice now, both times using the 64-bit download from Apple's download page, but the same message appears and the iPod is not recognized.
    If anyone knows where I am going wrong I would be grateful for your advice.
    Ta.

    Hi Tintin01,
    Thanks for visiting Apple Support Communities.
    If you're being repeatedly prompted to install the 64-bit version of iTunes, I'd suggest first completely uninstalling iTunes and all its related components using the steps found here:
    Removing and reinstalling iTunes, QuickTime, and other software components for Windows Vista or Windows 7
    http://support.apple.com/kb/HT1923
    Once you've uninstalled iTunes and its components, install the latest version of iTunes found here:
    iTunes 11.0.4 for Windows 64
    http://support.apple.com/kb/DL1615
    Best Regards,
    Jeremy

  • Converting iPhone 4S to iTouch and would rather not reset it.  How can I disable the phone number from the list of valid iMessage and FaceTime contact points?  The line item is shaded in each.

    I've changed the Apple IDs for iCloud, iMessage and FaceTime.  I've turned cellular off.  I even changed the cellphone number in the phone settings to a 555 garbage number.  I'm guessing the number is some how attached to the SIM card?  Is there any way to solve this short of resetting the phone and starting from scratch?  Backups to iCloud and iTunes were done before any changes took place.

    Bump

  • Along the lines of How to burn DVD's

    I was trying to burn a simple data CD. Dragged the files to the CD in Finder saw the alias, hit burn. After a moment (spinning pinwheel) I get an error message saying:
    Burning the disc failed because one or more items in "Desktop.fpbf" could not be read.
    Could not read '/Library/Caches/com.apple.IntlDataCache.Ie.4294967294
    Is this an error with the disk (they are old Maxell CD-Rs), error with the files being cooked, they are med. res jpegs or as I fear a missing/corrupt file in the OS?
    Any help is appreciated.

    MH Holden wrote:
    I was trying to burn a simple data CD. Dragged the files to the CD in Finder saw the alias, hit burn.
    After a moment (spinning pinwheel) I get an error message saying:
    Burning the disc failed because one or more items in "Desktop.fpbf" could not be read.
    I presume you created a burn folder.
    Could not read '/Library/Caches/com.apple.IntlDataCache.Ie.4294967294
    Is this an error with the disk (they are old Maxell CD-Rs), error with the files being cooked, they are med. res jpegs or as I fear a missing/corrupt file in the OS?
    Any help is appreciated.
    Why not really simplify it? Insert a blank, after it mounts double click it to open it and drag a file into the window. It will be an alias of the actual file which is correct and normal.
    Select File->Burn Disc and see what happens.
    If you want to save money, use a R/W disk for starters.

Maybe you are looking for

  • Radio Remote Not Working As It Should

    tonight i bought a radio remote so I can listen to the radio. i plug it in, it works. however when i go to change the channel, it wont change. the volume can be turned up and down from both the ipod itself and the remote. also the radio can be turned

  • I can't copy and paste. Try to follow instructions to fix, but get stuck.

    How do I fix this? I tried to follow the instructions, but when I get to the part of copying the lines of text they give me, I , of course, can't. Help please. Unprivileged scripts cannot access Cut/Copy/Paste programatically for security reasons. Cl

  • SPA112 Crashes - No dialtone, restart needed - new firmware

    Hi All, I  have a  SPA112  brain new,  with  latest  fw  loaded as  per  Cisco site  Version 1.2.1 (004) Since  the  first  installation  has  started to  show  the  same   problem as  described  also  in " SPA112 Crashes - No dialtone, restart neede

  • How do you unlock a dissabled ipod

    can not remember my password. ive tried alot. now it says "iPod is disabled. Connect to iTunes" what do i do?

  • Netflow Nexus 7000

    Hi all, A few months ago I have configured netflow on a Nexus 7000 with NX-OS version 6.0.2. This was my config: flow exporter Fluke_NetflowTracker   description export netflow to Fluke_NetflowTracker   destination x.x.x.x use-vrf management   transp