Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8450761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:03:39+00:00 2026-06-10T11:03:39+00:00

package CreatingWindows; import javax.swing.*; import java.awt.geom.*; import java.awt.*; import javax.swing.event.MouseInputAdapter; import java.awt.event.MouseEvent; public class

  • 0
package CreatingWindows;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.*;
import javax.swing.event.MouseInputAdapter;
import java.awt.event.MouseEvent;
public class myCurveApplet extends JApplet{
    @Override
    public void init(){
        this.add(pane);
        pane.addMouseListener(new MouseHandler());
        pane.addMouseMotionListener(new MouseHandler());
    }
    private class CurvePane extends JComponent{
        @Override
        public void paint(Graphics g){
            Graphics2D g2D = (Graphics2D)g;
            if(a!=null&&b!=null&&ctrl!=null){
                System.out.println("Repainted!");
                String text;
                aMark = new Marker(a);
                bMark = new Marker(b);
                ctrlMark = new Marker(ctrl);
                quadCurve = new QuadCurve2D.Double(a.x, a.y, ctrl.x, ctrl.y, b.x, b.y);

                aMark.draw(g);
                g2D.setPaint(Color.BLACK);
                text = "A"+"\n"+"("+a.x+","+a.y+")";
                g2D.drawString(text,(int)a.x,(int)a.y+30);
                bMark.draw(g);
                g2D.setPaint(Color.BLACK);
                text = "B"+"\n"+"("+b.x+","+b.y+")";
                g2D.drawString(text,(int)b.x,(int)b.y+30);
                ctrlMark.draw(g);
                g2D.setPaint(Color.BLACK);
                text = "Control"+"\n"+"("+ctrl.x+","+ctrl.y+")";
                g2D.drawString(text,(int)ctrl.x,(int)ctrl.y+30);

                g2D.setPaint(Color.BLACK);
                g2D.draw(quadCurve);
            }
        }
    }
    private class MouseHandler extends java.awt.event.MouseAdapter{
        @Override
        public void mouseClicked(MouseEvent e){
            if(a==null||b==null||ctrl==null){
                if(a==null){
                    a = new Point2D.Double(e.getX(), e.getY());
                }else if(b==null){
                    b = new Point2D.Double(e.getX(), e.getY());
                }else{
                    ctrl = new Point2D.Double(e.getX(), e.getY());
                }
            }else if(aMark!=null&&bMark!=null&&ctrlMark!=null){
                if(aMark.contains(e.getX(),e.getY())){
                    selectedPoint = a;
                }else if(bMark.contains(e.getX(),e.getY())){
                    selectedPoint = b;
                }else if(ctrlMark.contains(e.getX(),e.getY())){
                    selectedPoint = ctrl;
                }
            }
        }
        @Override
        public void mouseDragged(MouseEvent e){
            if(selectedPoint!=null){
                selectedPoint.x = e.getX();
                selectedPoint.y = e.getY();
            }
        }
        @Override
        public void mouseReleased(MouseEvent e){
            selectedPoint = null;
            pane.repaint();
        }
        private Point2D.Double selectedPoint;
    }
    private class Marker{
        public Marker(Point2D.Double center){
            this.center.setLocation(center);
            circle = new Ellipse2D.Double(center.x,center.y,r,r);
        }
        public void draw(Graphics g){
            Graphics2D g2D = (Graphics2D)g;
            if(circle!=null){
                g2D.setPaint(selectedColor);
                g2D.draw(circle);
            }
        }
        public boolean contains(int x,int y){
            return circle.contains(x, y);
        }
        public void setLocation(int x,int y){
            center.x = x;
            center.y = y;
            circle = new Ellipse2D.Double(center.x,center.y,r,r);
        }
        public Point2D.Double center = new Point2D.Double();
        private Ellipse2D.Double circle;
        private static final double r = 6;
    }

    private Point2D.Double a;
    private Point2D.Double b;
    private Point2D.Double ctrl;
    private QuadCurve2D.Double quadCurve;
    Marker aMark;
    Marker bMark;
    Marker ctrlMark;
    private Color selectedColor = Color.orange;
    private CurvePane pane = new CurvePane();
}

This is the source code to create this:
enter image description here

And the thing here is to allow the user to change the curve by dragging the points across the screen but somehow Im unable to do it. Please tell me what changes to make and why!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:03:41+00:00Added an answer on June 10, 2026 at 11:03 am

    There are several problems there.

    The first reason because your program doesn’t work it’s the following:

    You assign to selectedPoint a copy of an object (a or b or ctrl ).
    When you modify it inside mouseDragged you are actually modifying the copy of one of those objects (selectedPoint) and not the original one. So when you rebuild the curve:

    quadCurve = new QuadCurve2D.Double(a.x, a.y, ctrl.x, ctrl.y, b.x, b.y);
    

    a b and ctrl have always the same initial values, since you modified only a copy of one of them (selectedPoint).

    To fix this you need to modify directly the desired object. In your case, I’d add a field (es. string name) to your Marker, and I’d save the marker instead of the point.
    Something like:

    if(aMark!=null&&bMark!=null&&ctrlMark!=null){
         if(aMark.contains(e.getX(),e.getY())){
             selectedMarker = aMark;
         }else if(bMark.contains(e.getX(),e.getY())){
             selectedMarker = bMark;
         }else if(ctrlMark.contains(e.getX(),e.getY())){
             selectedMarker = ctrlMark;
         }
    }
    

    Then when you drag the mouse check which marker is active and change directly the position:

    public void mouseDragged(MouseEvent e){
            if(selectedMarker !=null){
                if (0 == selectedMarker.getName().compareTo("ctrlMark"))
                    ctrl.x = e.getX();
                    ctrl.y = e.getY();
            }
    }
    

    The second reason is that mouseListener and mouseMotionListener uses 2 different instances of MouseHandler.
    So you have 2 copies of selectedPoint (or better selectedMarker), one used by MouseDragged(mouseMotionListener) and the other used by MouseClicked (MouseListener).

    You could declare selectedMarker as static to solve this problem.


    You have also other mistakes inside your program.
    Particularly:

    • override paint directly is wrong. Override instead paintComponent and call the base method too: public void paintComponent(Graphics g){super.paintComponent(g);
    • unuseful creation of objects at frametime: you are instancing new objects inside paintComponent method. That’s potentially a big overhead. Instance them once, and just modify the needed ones when you actually modify objects coordinates
    • name conventions: use capital letter for classes.

    Here’s a working snippet with few fixes:

    import javax.swing.*;
    import java.awt.geom.*;
    import java.awt.*;
    import javax.swing.event.MouseInputAdapter;
    import java.awt.event.MouseEvent;
    public class MyCurveApplet extends JApplet{
         private static Marker selectedMarker;
        @Override
        public void init(){
            this.add(pane);
            pane.addMouseListener(new MouseHandler());
            pane.addMouseMotionListener(new MouseHandler());
        }
        private class CurvePane extends JComponent{
            @Override
            public void paintComponent(Graphics g){
                super.paintComponent(g);
                Graphics2D g2D = (Graphics2D)g;
                if(a!=null&&b!=null&&ctrl!=null){
                    System.out.println("Repainted!");
                    String text;
                    aMark = new Marker(a,"a");
                    bMark = new Marker(b,"b");
                    ctrlMark = new Marker(ctrl,"ctrl");
                    quadCurve = new QuadCurve2D.Double(a.x, a.y, ctrl.x, ctrl.y, b.x, b.y);
    
                    aMark.draw(g);
                    g2D.setPaint(Color.BLACK);
                    text = "A"+"\n"+"("+a.x+","+a.y+")";
                    g2D.drawString(text,(int)a.x,(int)a.y+30);
                    bMark.draw(g);
                    g2D.setPaint(Color.BLACK);
                    text = "B"+"\n"+"("+b.x+","+b.y+")";
                    g2D.drawString(text,(int)b.x,(int)b.y+30);
                    ctrlMark.draw(g);
                    g2D.setPaint(Color.BLACK);
                    text = "Control"+"\n"+"("+ctrl.x+","+ctrl.y+")";
                    g2D.drawString(text,(int)ctrl.x,(int)ctrl.y+30);
    
                    g2D.setPaint(Color.BLACK);
                    g2D.draw(quadCurve);
                }
            }
        }
        private class MouseHandler extends java.awt.event.MouseAdapter{
            @Override
            public void mousePressed(MouseEvent e){
    
                if(a==null||b==null||ctrl==null){
                    System.out.println("FOO");
                    if(a==null){
                        a = new Point2D.Double(e.getX(), e.getY());
                    }else if(b==null){
                        b = new Point2D.Double(e.getX(), e.getY());
                    }else{
                        ctrl = new Point2D.Double(e.getX(), e.getY());
                    }
                }else if(aMark!=null&&bMark!=null&&ctrlMark!=null){
                    if(aMark.contains(e.getX(),e.getY())){
                        System.out.println("SelecteMarker A");
                        selectedMarker = aMark;
                    }else if(bMark.contains(e.getX(),e.getY())){
                        System.out.println("SelecteMarker B");
                        selectedMarker = bMark;
                    }else if(ctrlMark.contains(e.getX(),e.getY())){
                        System.out.println("SelecteMarker CTRL");
                        selectedMarker = ctrlMark;
                    }
                }
            }
            @Override
            public void mouseDragged(MouseEvent e){
                if(selectedMarker!=null){
                    if (0 == selectedMarker.name.compareTo("a")){
                        a.x = e.getX();
                        a.y = e.getY(); 
                    }else if (0 == selectedMarker.name.compareTo("b")){
                        b.x = e.getX();
                        b.y = e.getY();
                    }else if (0 == selectedMarker.name.compareTo("ctrl")){
                        ctrl.x = e.getX();
                        ctrl.y = e.getY();
    
                    }
    
                    pane.repaint();
                }
            }
            @Override
            public void mouseReleased(MouseEvent e){
                System.out.println("SelectedMark NULL");
                selectedMarker = null;
                pane.repaint();
            }
    
        }
        private class Marker{
            public String name;
            public Marker(Point2D.Double center, String name){
                this.name = name;
                this.center.setLocation(center);
                circle = new Ellipse2D.Double(center.x,center.y,r,r);
            }
            public void draw(Graphics g){
                Graphics2D g2D = (Graphics2D)g;
                if(circle!=null){
                    g2D.setPaint(selectedColor);
                    g2D.draw(circle);
                }
            }
            public boolean contains(int x,int y){
                return circle.contains(x, y);
            }
            public void setLocation(int x,int y){
                center.x = x;
                center.y = y;
                circle = new Ellipse2D.Double(center.x,center.y,r,r);
            }
            public Point2D.Double center = new Point2D.Double();
            private Ellipse2D.Double circle;
            private static final double r = 6;
        }
    
        private Point2D.Double a;
        private Point2D.Double b;
        private Point2D.Double ctrl;
        private QuadCurve2D.Double quadCurve;
        Marker aMark;
        Marker bMark;
        Marker ctrlMark;
        private Color selectedColor = Color.orange;
        private CurvePane pane = new CurvePane();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

package test; import java.awt.*; import java.awt.event.*; import java.awt.geom.Ellipse2D; import java.awt.image.BufferedImage; import javax.swing.*; public class
package loan; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class loan extends JApplet {
package Sartre.Connect4; import javax.swing.*; public class ChatGUI extends JDialog { public ChatGUI(){ setTitle(Chat); }
package jtextareatest; import java.io.FileInputStream; import java.io.IOException; import javax.swing.*; public class Jtextareatest { public static
package pkg411project; import javax.swing.ButtonGroup; public class CrudMain extends javax.swing.JInternalFrame { public CrudMain() { initComponents();
package classes.events { import flash.events.Event; public class ASSEvent extends Event { public static const
package vaannila; import java.util.ArrayList; import com.opensymphony.xwork2.ActionSupport; public class RegisterAction extends ActionSupport { private String
package { import flash.display.Sprite; import flash.events.Event; import flash.events.KeyboardEvent; import flash.ui.Keyboard; public class helloworld extends
package samples.flexstore { import flash.events.Event; public class ProductThumbEvent extends Event { public static const
package src; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Command { public static

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.