首先按住鼠标作为圆心,然后拖动,拖动的距离就是半径,画一个圆。我总是画不对,该怎么写?

解决方案 »

  1.   

    求得 用获取到的鼠标坐标-圆心坐标 的距离,然后作为半径,直接g2.draw(new circle(圆心,半径))
      

  2.   

    晕倒,圆心有什么用,jdk中构造一个圆的函数不需要圆心!
      

  3.   

    看我的源程序
    (已测试通过)
    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;
    import java.awt.event.*;public class MouseDrawCircle
    {
    public static void main(String[] args)
    {
    DrawFrame frame=new DrawFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.show();
    }
    }class DrawFrame extends JFrame
    {
    public DrawFrame()
    {
    setTitle("DrawFrame");
    setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
    //setResizable(false); //add panel to frame
    DrawPanel panel=new DrawPanel();
    Container contentPane=getContentPane();
    contentPane.add(panel);
    } public static final int DEFAULT_WIDTH=500;
    public static final int DEFAULT_HEIGHT=500;
    }class DrawPanel extends JPanel
    {
    public DrawPanel()
    {
    addMouseListener(new MouseHandler());
    addMouseMotionListener(new MouseMotionHandler());
    } public void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    Graphics2D g2=(Graphics2D)g; Line2D line=new Line2D.Double(centerX,centerY,nowX,nowY);
    g2.draw(line); Ellipse2D circle=new Ellipse2D.Double();
    circle.setFrameFromCenter(centerX,centerY,centerX-radius,centerY-radius);
    g2.draw(circle); g2.draw(circle.getBounds2D());
    } private int radius;
    private int centerX;
    private int centerY;
    private int nowX;
    private int nowY; private class MouseHandler implements MouseListener
    {
    public void mousePressed(MouseEvent event)
    {
    centerX=event.getX();
    centerY=event.getY();
    } public void mouseClicked(MouseEvent event)
    { } public void mouseEntered(MouseEvent event)
    { } public void mouseExited(MouseEvent event)
    { } public void mouseReleased(MouseEvent event)
    { }
    } private class MouseMotionHandler implements MouseMotionListener
    {
    public void mouseMoved(MouseEvent event)
    { } public void mouseDragged(MouseEvent event)
    {
    nowX=event.getX();
    nowY=event.getY(); if(Math.abs(nowX-centerX)>Math.abs(nowY-centerY))
    radius=Math.abs(nowX-centerX);
    else
    radius=Math.abs(nowY-centerY); repaint();
    }
    }
    }
      

  4.   

    public class JOval extends JCloseShape {
        
        Ellipse2D.Double e2d = new Ellipse2D.Double();
        int[] temp = new int[2];    //临时数组,用来存放起始点
        /**
         * Creates a new instance of JOval
         */
        public JOval(int x, int y, boolean f) {
            setStartPoint(x, y);
            setNewPoint(x, y);
            this.setFill(f);
        }
        
        public void setStartPoint(int x, int y) {
            e2d.x = x; temp[0] = x;
            e2d.y = y; temp[1] = y;
        }
        
        public void setNewPoint(int x, int y) {
            e2d.width = Math.abs(x - temp[0]);
            e2d.height = Math.abs(y - temp[1]);
            e2d.x = Math.min(x, temp[0]);
            e2d.y = Math.min(y, temp[1]);
        }
        
        public int getLX() {
            return (int)e2d.getBounds2D().getX();
        }
        
        public int getLY() {
            return (int)e2d.getBounds2D().getY();
        }
        
        public int getWidth() {
            return (int)e2d.getBounds2D().getWidth();
        }
        
        public int getHeight() {
            return (int)e2d.getBounds2D().getHeight();
        }
        
        public void drawShape(Graphics2D g2d) {
            if (this.isFill == true ) {
                g2d.setColor(this.fillColor);
                g2d.fill(e2d);
            }//填充
            g2d.setColor(this.shapeColor);
            g2d.setStroke(this.sStr);
            g2d.draw(e2d);
        }
    }
    这个是椭圆类
    是按照拉出来矩形画的,拉的不是半径
    不过你可以看一下
    可以触类旁通的
      

  5.   

    setNewPoint()方法是用来不断更新 新的终点的
    如果是半径的话
    用一下你的数学知识就可以了
    呵呵~
      

  6.   

    非常感谢xzxwsk(笑灾星) 和moon_cool(月影剑),可惜我的分太少了,要不肯定多多给你们加分!