动态添加多个MouseListeners JPanel

时间:2014-09-28 00:19:39

标签: java swing mouselistener

我正在尝试使用JPanel将形状添加到窗口中,然后可以在窗口周围单击并拖动它们。如果我只有一个形状,这是有效的;但是当我添加更多形状时,点击和拖动非常时髦。 拖动但不是用鼠标拖动,它不是成比例的,不会用鼠标拖动。

感谢任何帮助。谢谢!

public class SimpleDraw {

    public static void main(String[] args) {

        JFrame frame = new UMLWindow();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(30, 30, 1000, 700);
        frame.getContentPane().setBackground(Color.white);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);

        // Display the window.
        frame.setVisible(true);

    }
}

class UMLWindow extends JFrame {
    Squares squares = new Squares();

    private static final long serialVersionUID = 1L;

    public UMLWindow() {
        addMenus();
    }

    public void addMenus() {

        getContentPane().add(squares);

        JMenuBar menubar = new JMenuBar();

        JMenu shapes = new JMenu("Shapes");

        JMenuItem rectangleMenuItem = new JMenuItem("New Rectangle");
        rectangleMenuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                squares.addSquare(10, 10, 100, 100);
            }
        });

        shapes.add(rectangleMenuItem);

        menubar.add(shapes);

        setJMenuBar(menubar);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

}

class Squares extends JPanel {
    private static final long serialVersionUID = 1L;

    private List<Path2D> squares = new ArrayList<Path2D>();
    // private Path2D rect = new Path2D.Double();
    int currentIndex;

    public void addSquare(int x, int y, int width, int height) {
        Path2D rect2 = new Path2D.Double();
        rect2.append(new Rectangle(getWidth() / 2 - width / 2, getHeight() / 2
                - height / 2, width, height), true);

        squares.add(rect2);
        // rect = rect2;
        MyMouseAdapter myMouseAdapter = new MyMouseAdapter();
        addMouseListener(myMouseAdapter);
        addMouseMotionListener(myMouseAdapter);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        this.setOpaque(true);
        this.setBackground(Color.WHITE);
        Graphics2D g2 = (Graphics2D) g;
        for (Path2D rect : squares) {
            g2.draw(rect);
        }
        repaint();
    }

    class MyMouseAdapter extends MouseAdapter {
        private boolean pressed = false;
        private Point point;

        @Override
        public void mousePressed(MouseEvent e) {
            if (e.getButton() != MouseEvent.BUTTON1) {
                return;
            }
            for (int i = 0; i < squares.size(); i++) {
                if (squares.get(i) != null
                        && squares.get(i).contains(e.getPoint())) {
                    currentIndex = i;
                    pressed = true;
                    this.point = e.getPoint();
                }
            }
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            if (pressed) {
                int deltaX = e.getX() - point.x;
                int deltaY = e.getY() - point.y;
                squares.get(currentIndex).transform(
                        AffineTransform.getTranslateInstance(deltaX, deltaY));
                point = e.getPoint();
                repaint();
            }
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            pressed = false;
        }
    }

}

1 个答案:

答案 0 :(得分:2)

很多问题......

  • 最重要的是,你不想在你的JPanel中添加一堆MouseListeners / MouseMotionListeners。您只想添加一个,并让它控制JPanel持有的所有方块。
  • 不要在paintComponent方法中放置repaint(),因为这是尝试创建动画循环的一种不好的方法,这是一个完全无法控制的循环。另外,没有必要。 MouseAdapter应该自动驱动所有动画。

class Squares extends JPanel {
   private static final long serialVersionUID = 1L;

   public Squares() {
      MyMouseAdapter myMouseAdapter = new MyMouseAdapter();
      addMouseListener(myMouseAdapter);
      addMouseMotionListener(myMouseAdapter);
   }

   private List<Path2D> squares = new ArrayList<Path2D>();
   // private Path2D rect = new Path2D.Double();
   int currentIndex;

   public void addSquare(int x, int y, int width, int height) {
      Path2D rect2 = new Path2D.Double();
      rect2.append(new Rectangle(getWidth() / 2 - width / 2, getHeight() / 2
            - height / 2, width, height), true);

      squares.add(rect2);
      repaint(); // !!
      // rect = rect2;
      // !! MyMouseAdapter myMouseAdapter = new MyMouseAdapter();
      // addMouseListener(myMouseAdapter);
      // addMouseMotionListener(myMouseAdapter);
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      this.setOpaque(true);
      this.setBackground(Color.WHITE);
      Graphics2D g2 = (Graphics2D) g;
      for (Path2D rect : squares) {
         g2.draw(rect);
      }
      // !! repaint();

}