制作圆圈,然后在鼠标拖动事件上移动圆圈

时间:2012-05-24 09:24:12

标签: java swing mouseevent geometry paintcomponent

如何在鼠标拖动事件上绘制圆圈,然后如何在Java中用鼠标拖动事件移动该圆圈?

我的代码如下。

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 个答案:

答案 0 :(得分:3)

这些方面的东西很适合我:

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);
    }
}