0

I need to draw a simple circle by using mouse clicks First click will be the center Second will be the radius and the circle would be draw by the 2.

Thanks in advance

Spartacus
  • 378
  • 1
  • 4
  • 12
  • 2
    This is how to ask this question correctly: http://stackoverflow.com/questions/1836440/how-to-draw-circle-on-jpanel-java-2d – maksimov Apr 20 '12 at 10:53
  • 1
    possible duplicate of [Java: Is there any easy way to draw a circle onto a JPanel?](http://stackoverflow.com/questions/10219349/java-is-there-any-easy-way-to-draw-a-circle-onto-a-jpanel) – Perception Apr 20 '12 at 10:57
  • 1
    Just provide me with a billing address to send my invoice to ... put a bit more effort in your question and perhaps it will remain open – Robin Apr 20 '12 at 10:58
  • 1
    I need new system for my eshop. Thanks in advance. – Gatekeeper Apr 20 '12 at 11:01

1 Answers1

8

Good question! The code below is a self contained example (I'm using dragging to define the radius):

screenshot

Code that produces screenshot above:

public static void main(String[] args) throws IOException {
    JFrame frame = new JFrame("Test");
    frame.add(new JComponent() {
        Point p; int r; 
        {
            addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    p = e.getPoint(); r = 0; repaint();
                }
                public void mouseReleased(MouseEvent e) {
                    r = (int) Math.round(e.getPoint().distance(p));
                    repaint();
                }
            });
            addMouseMotionListener(new MouseMotionAdapter() {
                public void mouseDragged(MouseEvent e) {
                    r = (int) Math.round(e.getPoint().distance(p));
                    repaint();
                }
            });
            setPreferredSize(new Dimension(400, 300));
        }
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if(p != null) g.drawOval(p.x - r, p.y - r, 2 * r, 2 * r); 
        }
    });
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}

Example using 2 clicks:

public static void main(String[] args) throws IOException {
    JFrame frame = new JFrame("Test");
    frame.add(new JComponent() {
        Point p1, p2;
        {
            addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    if (p1 == null || p2 != null) { 
                        p1 = e.getPoint();
                        p2 = null;
                    } else {
                        p2 = e.getPoint();
                    } 
                    repaint();
                }
            });
            setPreferredSize(new Dimension(400, 300));
        }
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if(p1 != null && p2 != null) {
                int r = (int) Math.round(p1.distance(p2));
                g.drawOval(p1.x - r, p1.y - r, 2 * r, 2 * r);
            }
        }
    });
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}
dacwe
  • 43,066
  • 12
  • 116
  • 140