我试图弄清楚如何正确循环鼠标事件

时间:2013-12-04 18:10:50

标签: java

我正在处理图像移动的任务,当用户点击面板时图像停止。当用户再次点击面板时,图像开始。到目前为止,我只能在它继续运行之前启动和停止图像。我需要帮助来循环这个过程,以便用户可以继续启动和停止图像。

这是我的代码

主:

import javax.swing.*;

public class Rebound {
//-----------------------------------------------------------------
   //  Displays the main frame of the program.
   //-----------------------------------------------------------------
   public static void main (String[] args)
   {
      JFrame frame = new JFrame ("Rebound");
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

      frame.getContentPane().add(new ReboundPanel());
      frame.pack();
      frame.setVisible(true);
   }
}

面板:

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

public class ReboundPanel extends JPanel
{

   private final int DELAY = 10, IMAGE_SIZE = 35;

   private ImageIcon image;
   private Timer timer;
   private int x, y, moveX, moveY;

   public ReboundPanel()
   {
      timer = new Timer(DELAY, new ReboundListener());
      addMouseListener (new StopListener());
      image = new ImageIcon ("happyFace.gif");

      x = 0;
      y = 40;
      moveX = moveY = 3;

      setPreferredSize (new Dimension(1900, 1000));
      setBackground (Color.black);
      timer.start();
   }


   public void paintComponent (Graphics page)
   {
      super.paintComponent (page);
      image.paintIcon (this, page, x, y);
   }


   private class ReboundListener implements ActionListener
   {

      public void actionPerformed (ActionEvent event)
      {
         x += moveX;
         y += moveY;

         if (x <= 0 || x >= 1900-IMAGE_SIZE)
            moveX = moveX * -1;

         if (y <= 0 || y >= 1000-IMAGE_SIZE)
            moveY = moveY * -1;

         repaint();
      }
   }

   //  Represents the action listener for the timer.

   public class StopListener extends MouseAdapter
   {

       public void mouseClicked (MouseEvent event)
   {

            if (event.getButton() == MouseEvent.BUTTON1)
            {
                timer.stop();
            }

            addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent event) {
                    if(event.getButton() == MouseEvent.BUTTON1)
                    {
                        timer.start();
                        removeMouseListener(this);
                    }
                }
            });
    }

   }
}

2 个答案:

答案 0 :(得分:1)

在你StopListener类中有一个实例变量bool isMoving = true;

然后在你的处理程序中应该使用它来确定是否停止或启动计时器:

public void mouseClicked( MouseEvent event )
{
    if( event.getButton() == MouseEvent.BUTTON1 )
    {
        if( isMoving )
            timer.stop();
        else
            timer.start();

        isMoving = !isMoving;
    }
}

答案 1 :(得分:1)

您可以简单地重复使用相同的侦听器,并在启动/停止之前检查计时器的状态:

public void mouseClicked(MouseEvent event)
{
    if (event.getButton() == MouseEvent.BUTTON1)
    {
        if (timer.isRunning()) {
            timer.stop();
        }
        else {
            timer.start();
        }
    }
}