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

  • SEARCH
  • Home
  • 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 7996779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:47:27+00:00 2026-06-04T14:47:27+00:00

How to draw circle on mouse dragged event, and then how to move that

  • 0

How to draw circle on mouse dragged event, and then how to move that circle on mouse dragged event in Java?

My code is below.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class r extends JPanel {

    public int x1, x2, y1, y2, r, w, h,xDist,yDist;
    public static boolean flag = false, pressFlag = false;

    public r() {
        setBackground(Color.WHITE);
        addMouseListener(new MouseAdapter() {

            public void mousePressed(MouseEvent m) {
//                pressFlag = true;
                if (r > (int) Math.sqrt(Math.abs(m.getX() - x1) * Math.abs(m.getX() - x1) + Math.abs(m.getY() - y1) * Math.abs(m.getY() - y1))) {
                    flag = true;
                    yDist=xDist=x2 = y2 = 0;
                } else {
                    x1 = y1 = 0;
                    r=x2 = y2 = 0;
                    x1 = m.getX();
                    y1 = m.getY();
                }
                 repaint();
            }

            public void mouseReleased(MouseEvent m) {
                w = x2 - x1;
                h = y2 - y1;
                r = (int) Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
                flag = false;
            }
        });
        addMouseMotionListener(new MouseMotionAdapter() {

            public void mouseDragged(MouseEvent m) {
                if (flag && (x2!=0 && y2!=0)) {
                    xDist=(m.getX()-x2);
                    yDist=(m.getY()-y2);
                }
                x2 = m.getX();
                y2 = m.getY();

                repaint();
            }
        });
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (flag) {
            x1=x1+xDist;
            y1=y1+yDist;
            g.drawOval(x1, y1, w, h);
        } else {
            g.drawOval(x1, y1, x2 - x1, y2 - y1);
        }

    }
}

public class q extends JFrame {

    public static void main(String[] args) {
        JFrame jFrame = new JFrame();
        jFrame.setSize(300, 300);
        jFrame.add(new r());
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
  • 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-04T14:47:28+00:00Added an answer on June 4, 2026 at 2:47 pm

    Something along those lines works nicely for me:

    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Point;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.geom.Ellipse2D;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    class DragCircle extends JPanel {
    
        private final class MouseDrag extends MouseAdapter {
            private boolean dragging = false;
            private Point last;
    
            @Override
            public void mousePressed(MouseEvent m) {
                last = m.getPoint();
                dragging = isInsideEllipse(last);
                if (!dragging) {
                    x = last.x;
                    y = last.y;
                    width = 0;
                    height = 0;
                }
                repaint();
            }
    
            @Override
            public void mouseReleased(MouseEvent m) {
                last = null;
                dragging = false;
                repaint();
            }
    
            @Override
            public void mouseDragged(MouseEvent m) {
                int dx = m.getX() - last.x;
                int dy = m.getY() - last.y;
                if (dragging) {
                    x += dx;
                    y += dy;
                } else {
                    width += dx;
                    height += dy;
                }
                last = m.getPoint();
                repaint();
            }
        }
    
        private int x;
        private int y;
        private int width;
        private int height;
    
        private MouseDrag mouseDrag;
    
        public DragCircle() {
            setBackground(Color.WHITE);
            mouseDrag = new MouseDrag();
            addMouseListener(mouseDrag);
            addMouseMotionListener(mouseDrag);
        }
    
        public boolean isInsideEllipse(Point point) {
            return new Ellipse2D.Float(x, y, width, height).contains(point);
        }
    
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawOval(x, y, width, height);
        }
    
        public static void main(String[] args) {
            JFrame jFrame = new JFrame();
            jFrame.setSize(300, 300);
            jFrame.add(new DragCircle());
            jFrame.setVisible(true);
            jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

import java.awt.*; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.event.*;
import javax.swing.SwingUtilities; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.BorderFactory; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics;
this is the code: import java.awt.*; import java.awt.event.*; import java.applet.*; import java.util.Vector; public class
So far I have a java app where I draw a circle(player) and then
Why does this code only draw a circle once? I cannot for the life
I am trying to draw a circle using mouse on the canvas using mouse
I want to draw a circle in GWT, with some mouse over and drag-and-drop
I've written a fairly simple java application that allows you to drag your mouse
I create a subclass of NSView that draw a red filled circle and i'd
I need to draw Line, Circle, and rectangle by mouse drag on canvas and

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.