Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Text Drag With Mouse is OK but Circle Drag Fails

  1. #1
    Member
    Join Date
    Apr 2024
    Posts
    34
    Thanks
    12
    Thanked 1 Time in 1 Post

    Default Text Drag With Mouse is OK but Circle Drag Fails

    Hello Everybody.

    I'm trying to solve the following problem:


    (Two movable vertices and their distances)

    Write a program that displays
    two circles with radius 10 at
    location (40, 40) and (120, 150)
    with a line connecting the two circles, as shown in Figure.

    The distance between the circles
    is displayed along the line.

    The user can drag a circle.

    When that happens, the
    circle and its line are moved and
    the distance between the circles is updated.



    I've started by examining a similar JavaFX program.

    This JavaFX program allows the user to drag a Text object inside the Pane.

    That program to drag a Text is working well.

    This is the code of that program.



     
     
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package Chapter15.Section15dot8MouseEvents;
     
    /**
     *
     * @author Rogerio Biscaia
     */
     
     
     
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.layout.Pane;
    import javafx.scene.text.Text;
    import javafx.stage.Stage;
     
     
     
    public class MouseEventDemo extends Application {
     
        Text text;
     
     
        @Override
        public void start(Stage primaryStage) throws Exception {
     
     
            /*
            Create a pane and set its properties
            */
     
            Pane pane = new Pane();
     
            text = new Text(20, 20, "Programming is fun");
     
     
     
            pane.getChildren().clear();
     
            pane.getChildren().addAll( text );
     
     
     
            /*
            Create the handler
            */
     
            text.setOnMouseDragged( e -> {
     
                text.setX( e.getX() );
     
                text.setY( e.getY() );
     
            } ); //end of text.setOnMouseDragged( e -> {} );
     
     
     
            /*
            Create a scene and
            place it in the Stage
            */
     
            Scene scene = new Scene(pane, 300, 100);
     
            primaryStage.setTitle( "MouseEventDemo" );
     
            primaryStage.setScene(scene);
     
            primaryStage.show();
     
     
     
        } //end of the method public void start(Stage primaryStage)
     
     
     
        /*
        The main method is only needed for the IDE with
        limited JavaFX support.
        Not needed for running from the
        command line
        */
     
        public static void main(String[] args) {
     
     
            try {
     
                Application.launch(args);
     
            } catch(Exception ex1) {
     
                System.out.println("Exception in the main in the " +
                        "class MouseEventDemo");
     
                System.out.println( ex1.toString() );
     
            }
     
     
     
        } //end of main
     
     
     
    } //end of the class MouseEventDemo


    Then I've decided to create the new program for the two circles based in the previous program for the Text.

    And that is where I get my problem.

    I have to go now.

    In my next post I will place the new classes I've created.

    Thank you,

    Rogério

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,114
    Thanks
    65
    Thanked 2,718 Times in 2,668 Posts

    Default Re: Text Drag With Mouse is OK but Circle Drag Fails

    One problem I see with the posted code is that the initial movement is a jump to the cursor's location.
    For example position the cursor under the word "is" and drag it, the text jumps so that the "P" is at the cursor location instead of "is". I'd like it better if the click point on the text stayed at the cursor when it moved. See the logic in the setOnMouseDragged() method.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2024
    Posts
    34
    Thanks
    12
    Thanked 1 Time in 1 Post

    Default Re: Text Drag With Mouse is OK but Circle Drag Fails

    Hello Norm.

    Thank you for your comment.

    Yes that is an error in the program to move the Text object.

    I will change that program to move the Text so that this problem is solved.

    However, I would like to talk about the problem that made me create the thread in the first place.

    The program to move the Text object is visibly moving the Text object in the Pane.

    Based on the program to move the Text object, I've created a new Program to solve the programming exercise and move the two circles.

    Initially I've created only the part to move the circle1.

    When I run the program and I press the button and drag the circle1, most of the times it does not move the circle at all.

    So the program to move the Text moves the Text, but the program to move the Circle does not move the Circle.

    I would like to understand why this happens.

    These are the three Classes that are used in my Program to move the Circles.

    This is the Custom Exception InvalidValues


     
     
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package Chapter15.EndofChapter15.Exerc15dot16;
     
     
     
     
     
     
    /**
     *
     * @author Rogério Biscaia
     */
     
     
    public class InvalidValues extends Exception {
     
     
     
        /*
        Construct an exception
        */
     
        public InvalidValues(String message) {
     
            super("One or several variables have received Invalid Values\n" +
                    "Please read the following message\n" +
                    message);
     
     
        }
     
    } //end of the class InvalidValues



    This is the Rational Class with Big Integers and Big Decimals



     
     
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package Chapter15.EndofChapter15.Exerc15dot16;
     
     
    /**
     *
     * @author Rogerio Biscaia
     */
     
     
    /*
     
    (Use BigInteger for the Rational class)
     
    Redesign and implement the
    Rational class in Listing 13.13 using BigInteger for the
    numerator and denominator.
     
    */
     
     
     
    import java.math.*;
     
     
    public class Rational extends Number implements Comparable<Rational> {
     
     
     
        //Data fields for numerator and denominator
        private BigInteger numerator;
        private BigInteger denominator;
     
     
        /*
        Construct a rational with
        numerator 0 and
        denominator 1
        */
     
        public Rational() throws Exception {
     
            this(BigInteger.ZERO, BigInteger.ONE);
     
        }
     
     
     
        /*
        Construct a Rational Object from a
        certain double value
        */
     
        public static Rational getFraction(double value) throws Exception {
     
     
     
     
            int i;
     
     
            long tenPower;
     
     
     
     
            String valueString;
     
     
            String regex;
     
     
     
            String[] inputSplit;
     
     
     
     
            BigInteger tenToPower;
     
            BigInteger numerator;
     
            BigInteger denominator;
     
     
            Rational fraction;
     
     
     
            regex = "[,.]";
     
     
            valueString = Double.toString(value);
     
     
            inputSplit = valueString.split(regex);
     
     
     
     
     
            /*
            Iterate through the inputSplit array
            */
     
     
     
            //System.out.println("Iterating through the inputSplit array");
     
     
            /*
     
            for(i = 0; i < inputSplit.length; i++) {
     
                //System.out.println( inputSplit[i] );
     
            }
     
            */
     
     
            tenPower = inputSplit[1].length();
     
     
     
            tenToPower =  new BigInteger  (  (long) ( Math.pow(10, tenPower) ) + ""  ) ;
     
     
     
            numerator = new BigInteger( (  (long) ( value * tenToPower.doubleValue() ) ) + "" );
     
     
            //System.out.println("The numerator of the fraction is " + numerator);
     
     
            denominator =  tenToPower;
     
     
            //System.out.println("The denominator of the fraction is " + denominator);
     
     
            fraction = new Rational (numerator, denominator);
     
     
            //System.out.println("The fraction number is " + fraction);
     
     
            /*
            System.out.println("The value of the fraction as double is " +
                    fraction.doubleValue() );
     
            */
     
            return fraction;
     
     
        } //end of the method public static Rational getFraction(double value)
     
     
     
     
        /*
        Construct a rational with
        specified long numerator and
        denominator
        */
     
     
         public Rational(long numerator, long denominator) throws Exception {
     
             this( new BigInteger(numerator + ""), new BigInteger(denominator + "") );
     
         }
     
     
     
        /*
        Construct a rational with
        specified BigInteger numerator and
        denominator
        */
     
        public Rational(BigInteger numerator, BigInteger denominator) throws Exception {
     
     
     
            String exceptionMessage;
     
     
            BigInteger gcd;
     
     
            BigInteger multiplier;
     
     
            BigInteger newNumerator;
     
            BigInteger newDenominator;
     
     
     
            if(denominator == BigInteger.ZERO) {
     
                exceptionMessage = "in a fraction, the denominator cannot be 0";
     
                System.out.println(exceptionMessage);
     
                throw new InvalidValues(exceptionMessage);
     
            }
     
     
            gcd = gcd(numerator, denominator);
     
     
            if( denominator.compareTo( BigInteger.ZERO ) == 1 ) {
     
                multiplier = BigInteger.ONE;
     
            } else {
     
                multiplier = new BigInteger(-1 + "");
     
            }
     
     
            newNumerator = ( ( multiplier.multiply(numerator) ).divide(gcd) );
     
     
     
            newDenominator = ( denominator.abs() ).divide(gcd);
     
     
     
     
            this.numerator = newNumerator;
     
            this.denominator = newDenominator;
     
     
        } //end of the constructor public Rational(BigInteger numerator, BigInteger denominator)
     
     
     
     
        /*
        Find the GCD - Greatest Common Divisor
        of two numbers
        */
     
        private static BigInteger gcd(BigInteger n, BigInteger d) {
     
     
            BigInteger gcd;
     
     
            gcd = n.gcd(d);
     
     
            return gcd;
     
     
        } //end of the method private static long gcd(long n, long d)
     
     
     
        /*
        Return numerator
        */
     
        public BigInteger getNumerator() {
     
            return numerator;
     
        }
     
     
        /*
        Return denominator
        */
     
        public BigInteger getDenominator() {
     
            return denominator;
     
        }
     
     
        /*
        Add a rational number to this rational
        */
     
        public Rational add(Rational secondRational) throws Exception {
     
     
            BigInteger n;
     
            BigInteger d;
     
     
            n = (  (getNumerator() ).multiply(secondRational.getDenominator() ).add
            ( (getDenominator() ).multiply(secondRational.getNumerator() ) )  );
     
     
            d = getDenominator().multiply(secondRational.getDenominator() );
     
     
            return new Rational(n, d);
     
     
        } //end of the method public Rational add(Rational secondRational)
     
     
     
     
        /*
        Subtract a rational number from this rational
        */
     
     
        public Rational subtract(Rational secondRational) throws Exception {
     
     
            BigInteger n;
     
            BigInteger d;
     
     
            n = (  (getNumerator() ).multiply(secondRational.getDenominator() ).subtract
            ( (getDenominator() ).multiply(secondRational.getNumerator() ) )  );
     
     
            d = getDenominator().multiply(secondRational.getDenominator() );
     
     
            return new Rational(n, d);
     
     
        } //end of the method public Rational add(Rational secondRational)
     
     
     
        /*
        Multiply a rational number by this rational
        */
     
        public Rational multiply(Rational secondRational) throws Exception {
     
            BigInteger n;
     
            BigInteger d;
     
     
            n = getNumerator().multiply(secondRational.getNumerator() );
     
     
            d = getDenominator().multiply(secondRational.getDenominator() );
     
     
            return new Rational(n, d);
     
     
        } //end of the method public Rational multiply(Rational secondRational)
     
     
     
        /*
        Divide a rational number by this rational
        */
     
        public Rational divide(Rational secondRational) throws Exception {
     
     
            BigInteger n;
     
            BigInteger d;
     
     
            n = getNumerator().multiply(secondRational.getDenominator() );
     
            d = getDenominator().multiply(secondRational.getNumerator() );
     
     
            return new Rational(n, d);
     
        } //end of the method public Rational divide(Rational secondRational)
     
     
     
        /*
        Get the exponent of a Rational to
        the power of an int value
        */
     
        public Rational exponentiation(int exponent) throws Exception {
     
     
     
            BigDecimal newNumerator;
     
            BigDecimal newDenominator;
     
            BigDecimal division;
     
            BigDecimal result;
     
     
            Rational fraction;
     
     
            newNumerator = new BigDecimal(getNumerator() );
     
            newDenominator = new BigDecimal(getDenominator() );
     
            division = newNumerator.divide(newDenominator, 16, RoundingMode.UP);
     
     
            result = division.pow(exponent);
     
     
            fraction = Rational.getFraction( result.doubleValue() );
     
     
            return fraction;
     
     
        } //end of the method public Rational exponentiation(int exponent)
     
     
        /*
        Override toString
        */
     
        @Override
        public String toString() {
     
     
            if( getDenominator().compareTo( BigInteger.ONE )  == 0) {
     
                return getNumerator() + "";
     
            } else {
     
                return getNumerator() + "/" + getDenominator();
     
            }
     
     
        } //end of the method public String toString()
     
     
     
        /*
        It is not possible to
        Override the
        equals method as it was in
        the Object class
        because the new method throws Exception
        */
     
        @Override
        public boolean equals(Object other) {
     
            boolean equalObjects;
     
            equalObjects = false;
     
     
            try {
     
     
                if(other instanceof Rational) {
     
                    equalObjects = objectsEquals(other);
     
                }
     
     
     
            } catch(Exception ex1) {
     
                System.out.println(ex1.toString());
     
            }
     
     
            return equalObjects;
     
     
        } //end of the method public boolean equals(Object other)
     
     
     
     
        private boolean objectsEquals(Object other) throws Exception {
     
     
     
     
            if( this.subtract((Rational) (other) ).getNumerator() == BigInteger.ZERO) {
     
                return true;
     
            } else {
     
                return false;
     
            }
     
     
        } //end of the method private boolean objectsEquals(Object other) throws Exception
     
     
     
        /*
        Implement the abstract inValue method in Number
        */
     
        @Override
        public int intValue() {
     
            return (int)doubleValue();
     
        }
     
     
        /*
        Implement the abstract floatValue method in Number
        */
     
        @Override
        public float floatValue() {
     
            return (float)doubleValue();
     
        }
     
     
     
        /*
        Implement the abstract doubleValue method in Number
        */
     
        @Override
        public double doubleValue() {
     
     
            BigDecimal newNumerator;
     
            BigDecimal newDenominator;
     
            BigDecimal division;
     
     
            double value;
     
     
            newNumerator = new BigDecimal(getNumerator() );
     
            newDenominator = new BigDecimal(getDenominator() );
     
            division = newNumerator.divide(newDenominator, 16, RoundingMode.UP);
     
     
     
            value = division.doubleValue();
     
     
            return value;
     
        }
     
     
     
     
        /*
        Implement the abstract longValue method in Number
        */
     
        @Override
        public long longValue() {
     
            return (long)doubleValue();
     
        }
     
     
     
        /*
        Implement the compareTo method in Comparable
        */
     
        @Override
        public int compareTo(Rational o) {
     
            int testResult;
     
            testResult = 0;
     
            try {
     
                testResult = compareTwoRationals(o);
     
            } catch(Exception ex1) {
     
                System.out.println(ex1.toString());
     
            }
     
            return testResult;
     
        }
     
     
     
        private int compareTwoRationals(Rational o) throws Exception {
     
     
            int result;
     
            result = (this.compareTo(o) );
     
     
            return result;
     
     
        } //end of the method public int compareTwoRationals(Rational o)
     
     
     
     
    } //end of the class Rational



    And this is the class of the Java FX program to display and move the circles.


     
     
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package Chapter15.EndofChapter15.Exerc15dot16;
     
    /**
     *
     * @author Rogerio Biscaia
     */
     
     
    /*
     
     
    (Two movable vertices and their distances)
     
    Write a program that displays
    two circles with radius 10 at
    location (40, 40) and (120, 150)
    with a line connecting the two circles, as shown in Figure.
     
    The distance between the circles
    is displayed along the line.
     
    The user can drag a circle.
     
    When that happens, the
    circle and its line are moved and
    the distance between the circles is updated.
     
     
    */
     
     
     
     
    import java.util.*;
    import java.math.*;
    import javafx.event.Event;
    import javafx.application.Application;
    import javafx.collections.ObservableList;
    import javafx.scene.Node;
    import javafx.stage.Stage;
    import javafx.scene.shape.Line;
    import javafx.scene.shape.Circle;
    import javafx.scene.Scene;
    import javafx.scene.input.MouseEvent;
    import javafx.scene.input.MouseButton;
    import javafx.scene.layout.Pane;
    import javafx.scene.paint.Color;
     
     
     
     
    public class MoveTwoCircles extends Application {
     
     
        private Circle circle1;
     
     
        private Circle circle2;
     
     
        private Line lineConnect;
     
     
        private final int CIRCLESRADIUS = 10;
     
     
        private ObservableList <Node> listNodes;
     
     
     
     
     
        @Override
        public void start(Stage primaryStage) throws Exception {
     
     
     
     
            int maxX;
     
            int maxY;
     
            double circle1CenterX, circle1CenterY;
     
            double circle2CenterX, circle2CenterY;
     
     
     
     
            maxX = 301;
     
            maxY = 301;
     
     
     
            circle1CenterX = 40.0;
     
            circle1CenterY = 40.0;
     
     
            System.out.println("The circle1 has center X " + circle1CenterX +  " and Y " + circle1CenterY);
     
     
            circle2CenterX = 120.0;
     
            circle2CenterY = 150.0;
     
     
     
            System.out.println("The circle2 has center X " + circle2CenterX +  " and Y " + circle2CenterY);
     
     
     
            /*
            Create the two Circles
            */
     
            circle1 = new Circle( circle1CenterX, circle1CenterY, CIRCLESRADIUS );
     
            circle1.setStroke(Color.BLACK);
     
            circle1.setFill(null);
     
     
            circle2 = new Circle( circle2CenterX, circle2CenterY, CIRCLESRADIUS );
     
     
            circle2.setStroke(Color.BLACK);
     
            circle2.setFill(null);
     
     
            /*
            Create the line connecting the
            two circles
            */
     
            lineConnect = getLine(circle1CenterX, circle1CenterY, circle2CenterX, circle2CenterY);
     
     
     
            /*
            Create the pane
            */
     
            Pane pane = new Pane();
     
     
            /*
            Place the Circles and Line
            in the pane
            */
     
            pane.getChildren().clear();
     
     
            pane.getChildren().addAll( circle1, circle2, lineConnect );
     
     
            //listNodes = pane.getChildren();
     
     
            //listNodes.add(circle1);
     
     
     
            /*
            Create the handler
            for dragging Circle1
            */
     
     
     
            circle1.setOnMouseDragged( event -> {
     
     
                double getX;
     
                double getY;
     
     
                getX = event.getX();
     
                getY = event.getY();
     
                System.out.println("Clicked Circle in Pane");
     
     
                System.out.println( "Button: " + event.getButton() );
     
                System.out.println("Event To String: " + event.toString() );
     
                System.out.println("Event Target: " + event.getTarget() );
     
     
                circle1.setCenterX(getX);
     
                circle1.setCenterY(getY);
     
                System.out.println("Updated the Circle 1 center x to " + getX + " and center y to " + getY);
     
     
     
            } ); //end of circle1.setOnMouseDragged( e -> {} );
     
     
            /*
            Create the handler
            for dragging Circle2
            */
     
     
     
     
            /*
            Create a scene and place it in the Stage 
            */
     
            Scene scene = new Scene(pane, maxX + 50, maxY + 50);
     
     
            primaryStage.setTitle("Display Two Circles And Line");
     
            primaryStage.setScene(scene);
     
            primaryStage.show();
     
     
     
        } //end of the method public void start(Stage primaryStage)
     
     
     
        /*
        main
     
        The main method is only needed for the IDE with
        limited JavaFX support.
        Not needed for running from the
        command line
     
        */
     
     
        public static void main (String[] args)  {
     
            try {
     
                Application.launch(args);
     
            } catch(Exception ex1) {
     
                System.out.println("Exception in the main in the " +
                        "class MoveTwoCircles");
     
     
                System.out.println(ex1.toString());
     
     
            }
     
     
     
            //Application.launch(args);
     
     
        } //end of main
     
     
     
        /*
        Create the line connecting the
        two circles
        */
     
     
        public Line getLine( double circle1CenterX, double circle1CenterY,  double circle2CenterX, double circle2CenterY ) {
     
     
     
     
            double lowerCircleX, lowerCircleY;
     
            double higherCircleX, higherCircleY;
     
     
     
            double angleToXAxis;
     
     
            double anglePointLowerCircle;
     
            double pointLowerCircleX;
     
            double pointLowerCircleY;
     
     
     
            double anglePointHigherCircle;
     
            double pointHigherCircleX;
     
            double pointHigherCircleY;
     
     
     
            Rational slope;
     
            Rational slopeMultiplyX;
     
            Rational b;
     
     
     
     
            angleToXAxis = 0.0;
     
     
            /*
            We have to calculate the
            linear equation for the line
     
     
     
    Enter x1, y1, x2, y2, x3, y3, x4, y4: 2 2 5 -1.0 4.0 2.0 -1.0 -2.0
     
    Slope = (rise) / (run)
     
    Slope = (y2 - y1) / (x2 - x1)
     
    (-1 - 2) / (5 - 2)
     
    (-3) / (3)
     
    -1
     
    y = mx + b
     
    m = Slope
     
    2 = (-1) * 2 + b
     
    b = 4
     
    y = (-1) * x + 4
     
     
     
            */
     
     
            //slope = (randomStartY - randomEndY) / (  randomStartX - randomEndX  );
     
     
     
     
     
     
            try {
     
     
     
                slope = new Rational();
     
     
                b = new Rational();
     
     
     
                Rational slopeNumerator =  Rational.getFraction(circle1CenterY - circle2CenterY);
     
     
                //System.out.println("After creating the Rational for slopeNumerator");
     
                Rational slopeDenominator =  Rational.getFraction( circle1CenterX - circle2CenterX  );
     
                //System.out.println("After creating the Rational for slopeDenominator");
     
     
                Rational lineStartX = Rational.getFraction(circle1CenterX);
     
                Rational lineStartY = Rational.getFraction(circle1CenterY);
     
     
                slope = slopeNumerator.divide(slopeDenominator);
     
     
     
                slopeMultiplyX = slope.multiply(lineStartX);
     
     
                b = lineStartY.subtract(slopeMultiplyX);
     
     
                System.out.println("The Linear Equation for this line is y = " + slope + " * x + " + b);
     
     
     
     
                angleToXAxis =  Math.toDegrees(Math.atan( slope.doubleValue() ) );
     
     
            } catch(Exception ex1) {
     
     
                System.out.println("Exception when trying to create a Rational Object " +
                        " in the class MoveTwoCircles");
     
     
                System.out.println(ex1.toString());
     
            }
     
     
            /*
            Now that we have the linear equation for the
            line
     
            If one of the lines of the
            angle between two lines is y= mx + b and
            the other line is the x-axis, then
     
            θ= Tan m elevated to -1.
     
            */
     
     
            System.out.println("The angle between the line between two circles and the X Axis is " + angleToXAxis);
     
     
     
            /*
            Define the Lower Circle
            that has the Higher Y
            */
     
     
     
            if(circle1CenterY > circle2CenterY) {
     
                lowerCircleX = circle1CenterX;
     
                lowerCircleY = circle1CenterY;
     
     
                higherCircleX = circle2CenterX;
     
                higherCircleY = circle2CenterY;
     
     
            } else {
     
                lowerCircleX = circle2CenterX;
     
                lowerCircleY = circle2CenterY;
     
     
                higherCircleX = circle1CenterX;
     
                higherCircleY = circle1CenterY;
     
     
     
            }
     
     
     
            /*
            Get the Starting points for the
            line on the lower circle and
            higher circle
            */
     
     
            anglePointLowerCircle = 0;
     
            anglePointHigherCircle = 0;
     
     
            if(angleToXAxis < 0) {
     
                anglePointLowerCircle = 90 - Math.abs(angleToXAxis);
     
                anglePointHigherCircle = 270 - Math.abs(angleToXAxis);
     
            } else {
     
     
                anglePointLowerCircle = 0 - (90 - angleToXAxis);
     
                anglePointHigherCircle = 180 - (90 - angleToXAxis);
     
            }
     
     
     
     
            pointLowerCircleX = lowerCircleX + (CIRCLESRADIUS * Math.sin(   Math.toRadians( anglePointLowerCircle ) ) );
     
            pointLowerCircleY = lowerCircleY - (CIRCLESRADIUS * Math.cos( Math.toRadians( anglePointLowerCircle ) ) );
     
     
     
            pointHigherCircleX = higherCircleX + (CIRCLESRADIUS * Math.sin(   Math.toRadians( anglePointHigherCircle ) ) );
     
            pointHigherCircleY = higherCircleY - (CIRCLESRADIUS * Math.cos(   Math.toRadians( anglePointHigherCircle ) ) );
     
     
            /*
            Create the
            line Connecting the Two circles
            but not
            entering inside the circles
            */
     
     
            lineConnect = new Line(pointLowerCircleX, pointLowerCircleY, pointHigherCircleX, pointHigherCircleY);
     
            lineConnect.setStroke(Color.BLUE);
     
     
            return lineConnect;
     
     
     
        } //end of the method public Line getLine( double circle1CenterX, double circle1CenterY,  double circle2CenterX, double circle2CenterY )
     
     
     
     
     
     
    } //end of hte class MoveTwoCircles



    I wish you a good weekend.

    Thank you,

    Rogério

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,114
    Thanks
    65
    Thanked 2,718 Times in 2,668 Posts

    Default Re: Text Drag With Mouse is OK but Circle Drag Fails

    most of the times it does not move the circle at all.
    It never moves a circle for me.
    Did you change the program after it was moving the circle? Did it work before the change and has never worked after the change? What was in the code when it worked?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Apr 2024
    Posts
    34
    Thanks
    12
    Thanked 1 Time in 1 Post

    Default Re: Text Drag With Mouse is OK but Circle Drag Fails

    Hello Norm.

    In the java program to move the circle, the program never dragged the Circle Correctly.

    I'm sorry to have to repeat myself.

    Most of the times it does not move the circle but sometimes it does.

    Sometimes I run the program 5 times and only at the fifth time it moves.

    Today, it moved the Circle the first time I've run the program.

    Here it is the Output:



    The circle1 has center X 40.0 and Y 40.0
    The circle2 has center X 120.0 and Y 150.0
    The Linear Equation for this line is y = 11/8 * x + -15
    The angle between the line between two circles and the X Axis is 53.9726266148964

    Clicked Circle in Pane
    Button: PRIMARY
    Event To String: MouseEvent [source = Circle[centerX=40.0, centerY=40.0, radius=10.0, fill=null, stroke=0x000000ff, strokeWidth=1.0], target = Circle[centerX=40.0, centerY=40.0, radius=10.0, fill=null, stroke=0x000000ff, strokeWidth=1.0], eventType = MOUSE_DRAGGED, consumed = false, x = 36.0, y = 32.0, z = 0.0, button = PRIMARY, primaryButtonDown, pickResult = PickResult [node = Pane@2663aa5c[styleClass=root], point = Point3D [x = 36.0, y = 32.0, z = 0.0], distance = 654.974916728338]
    Event Target: Circle[centerX=40.0, centerY=40.0, radius=10.0, fill=null, stroke=0x000000ff, strokeWidth=1.0]
    Updated the Circle 1 center x to 36.0 and center y to 32.0

    Clicked Circle in Pane
    Button: PRIMARY
    Event To String: MouseEvent [source = Circle[centerX=36.0, centerY=32.0, radius=10.0, fill=null, stroke=0x000000ff, strokeWidth=1.0], target = Circle[centerX=36.0, centerY=32.0, radius=10.0, fill=null, stroke=0x000000ff, strokeWidth=1.0], eventType = MOUSE_DRAGGED, consumed = false, x = 239.0, y = 148.0, z = 0.0, button = PRIMARY, primaryButtonDown, pickResult = PickResult [node = Pane@2663aa5c[styleClass=root], point = Point3D [x = 239.0, y = 148.0, z = 0.0], distance = 654.974916728338]
    Event Target: Circle[centerX=36.0, centerY=32.0, radius=10.0, fill=null, stroke=0x000000ff, strokeWidth=1.0]
    Updated the Circle 1 center x to 239.0 and center y to 148.0

    Clicked Circle in Pane
    Button: PRIMARY
    Event To String: MouseEvent [source = Circle[centerX=239.0, centerY=148.0, radius=10.0, fill=null, stroke=0x000000ff, strokeWidth=1.0], target = Circle[centerX=239.0, centerY=148.0, radius=10.0, fill=null, stroke=0x000000ff, strokeWidth=1.0], eventType = MOUSE_DRAGGED, consumed = false, x = 241.0, y = 149.0, z = 0.0, button = PRIMARY, primaryButtonDown, pickResult = PickResult [node = Pane@2663aa5c[styleClass=root], point = Point3D [x = 241.0, y = 149.0, z = 0.0], distance = 654.974916728338]
    Event Target: Circle[centerX=239.0, centerY=148.0, radius=10.0, fill=null, stroke=0x000000ff, strokeWidth=1.0]
    Updated the Circle 1 center x to 241.0 and center y to 149.0

    Clicked Circle in Pane
    Button: PRIMARY
    Event To String: MouseEvent [source = Circle[centerX=241.0, centerY=149.0, radius=10.0, fill=null, stroke=0x000000ff, strokeWidth=1.0], target = Circle[centerX=241.0, centerY=149.0, radius=10.0, fill=null, stroke=0x000000ff, strokeWidth=1.0], eventType = MOUSE_DRAGGED, consumed = false, x = 241.0, y = 150.0, z = 0.0, button = PRIMARY, primaryButtonDown, pickResult = PickResult [node = Pane@2663aa5c[styleClass=root], point = Point3D [x = 241.0, y = 150.0, z = 0.0], distance = 654.974916728338]
    Event Target: Circle[centerX=241.0, centerY=149.0, radius=10.0, fill=null, stroke=0x000000ff, strokeWidth=1.0]
    Updated the Circle 1 center x to 241.0 and center y to 150.0

    Clicked Circle in Pane
    Button: PRIMARY
    Event To String: MouseEvent [source = Circle[centerX=241.0, centerY=150.0, radius=10.0, fill=null, stroke=0x000000ff, strokeWidth=1.0], target = Circle[centerX=241.0, centerY=150.0, radius=10.0, fill=null, stroke=0x000000ff, strokeWidth=1.0], eventType = MOUSE_DRAGGED, consumed = false, x = 242.0, y = 150.0, z = 0.0, button = PRIMARY, primaryButtonDown, pickResult = PickResult [node = Pane@2663aa5c[styleClass=root], point = Point3D [x = 242.0, y = 150.0, z = 0.0], distance = 654.974916728338]
    Event Target: Circle[centerX=241.0, centerY=150.0, radius=10.0, fill=null, stroke=0x000000ff, strokeWidth=1.0]
    Updated the Circle 1 center x to 242.0 and center y to 150.0


    Thank you,

    Rogério

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,114
    Thanks
    65
    Thanked 2,718 Times in 2,668 Posts

    Default Re: Text Drag With Mouse is OK but Circle Drag Fails

    Compare this program's creation of the clickable object to what is done in the other program that does move text.
    Note there is not a call to setFill() for the text program. Try changing that.

    Where is the cursor when the circle moves? Is it on the boundary or inside?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Apr 2024
    Posts
    34
    Thanks
    12
    Thanked 1 Time in 1 Post

    Default Re: Text Drag With Mouse is OK but Circle Drag Fails

    Hello Norm.

    I'm happy to say that this problem is solved.

    And I have to say that the merit belongs to someone else (at least partially).

    I was trying to solve the problem with the text that you, Norm, told me.


    I found this blog:


    Java-Buddy


    And I found the following page:

    Java-Buddy: JavaFX: Drag and Move something


    When I applied this code to the Text program the Text moves better, even if I click in the middle of it.

    Then, I've applied it to my Circle program, but the program continued with the same problem.

    Then I found the cause of my problem.

    By assigning null to the setFill() method the circle is transparent.

    That is why the line between the two circles does not enter inside any of the circles.

    But that was causing the problem.

    I've changed that line to:


    circle1.setFill(Color.TRANSPARENT);


    Now the two circles are being dragged correctly.

    Thank you,

    Rogério

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,114
    Thanks
    65
    Thanked 2,718 Times in 2,668 Posts

    Default Re: Text Drag With Mouse is OK but Circle Drag Fails

    I am glad you found the problem and were able to fix it.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    Rogerm (October 14th, 2024)

Similar Threads

  1. Drag and Drop in JTrees
    By helloworld922 in forum Java Swing Tutorials
    Replies: 4
    Last Post: March 21st, 2014, 11:16 AM
  2. [SOLVED] Question: Drag and drop tut.
    By PierreD in forum AWT / Java Swing
    Replies: 2
    Last Post: June 6th, 2011, 07:24 AM
  3. How to drag the shape to move?
    By ice in forum AWT / Java Swing
    Replies: 21
    Last Post: December 15th, 2010, 06:45 PM
  4. [SOLVED] Drag and drop in MVC
    By Alice in forum Object Oriented Programming
    Replies: 9
    Last Post: December 9th, 2010, 10:16 PM
  5. Drag and Drop in JTrees
    By helloworld922 in forum Java Code Snippets and Tutorials
    Replies: 2
    Last Post: February 20th, 2010, 03:52 PM