Java MouseMotionListener并限制绘制的形状移动

时间:2014-04-06 04:40:38

标签: java applet

使用吸引2只眼睛的小程序并使用MouseMotionListener移动它们的眼睛。此外,当鼠标退出内容窗格时,眼睛看起来笔直。一件事 我挣扎的是,我无法弄清楚如何限制瞳孔运动留在眼睛内。你们的任何建议都会很棒。

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

public class WatchMe extends JApplet
{
  private int leftMouseX = 130;
  private int leftMouseY = 155;
  private int rightMouseX = 305;
  private int rightMouseY = 155;
  private boolean mouseEntered;

  //init method
  public void init()
  {
    //set background to green
    getContentPane().setBackground(Color.green);

    //add mouse listener
    addMouseListener(new MyMouseListener());

    //add a motion listener
    addMouseMotionListener(new MyMouseMotionListener());
  }

  public void paint(Graphics g)
  {
    //call superclass paint method
    super.paint(g);

    //draw left eye
    g.setColor(Color.yellow);
    g.fillOval(75, 100, 150, 150);

    //draw left pupil
    g.setColor(Color.black);
    g.fillOval(leftMouseX, leftMouseY, 40, 40);

    //draw right eye
    g.setColor(Color.yellow);
    g.fillOval(250, 100, 150, 150);

    //draw right pupil
    g.setColor(Color.black);
    g.fillOval(rightMouseX, rightMouseY, 40, 40);

    //checks to see if the mouse is in the pane, if not
    //sets the x,y coordinates to look straight
    if (! mouseEntered)
    {
      leftMouseX = 130;
      leftMouseY = 155;
      rightMouseX = 305;
      rightMouseY = 155;
      repaint();
    }
  }

  private class MyMouseListener implements MouseListener
  {
    public void mousePressed(MouseEvent e)
    {
    }

    public void mouseClicked(MouseEvent e)
    {
    }

    public void mouseReleased(MouseEvent e)
    {
    }

    public void mouseEntered(MouseEvent e)
    { 
      mouseEntered = true;
      repaint();
    }

    public void mouseExited(MouseEvent e)
    {
      mouseEntered = false;
      repaint();
    }
  }

  private class MyMouseMotionListener implements MouseMotionListener
  {  

    public void mouseDragged(MouseEvent e)
    {
      leftMouseX = e.getX();
      leftMouseY = e.getY();
      rightMouseX = e.getX();
      rightMouseY = e.getY();
      repaint();
    }

    public void mouseMoved(MouseEvent e)
    {
      leftMouseX = e.getX();
      leftMouseY = e.getY();
      rightMouseX = e.getX();
      rightMouseY = e.getY();
      repaint();
    }
  } 
}

1 个答案:

答案 0 :(得分:1)

首先,我建议将您的眼睛尺寸放在变量中,如下所示。这样,您可以减少多次输入相同数字时出错的可能性,如果您以后决定更改眼睛大小或位置,则只需更改一次。

public static final int LEFT_X = 75;
public static final int LEFT_Y = 100;
public static final int EYE_SIZE = 150;

我们还需要左瞳孔独立于鼠标(所以它不会跟随鼠标离开眼睛)所以我们会这样做:

private leftPupilX = 130;
private leftPupilY = 155;

接下来,您需要确定鼠标当前是否在左眼中。这是将瞳孔限制在眼睛范围内的原因。为方便起见,我在方法中做到了这一点。

public void setLeftEye() {
     //Set the X Coord for the pupil
     //Mouse is to the left of the eye
     if(leftMouseX < LEFT_X) {
          leftPupilX = LEFT_X;
     //Mouse is to the right of the eye
     } else if(leftMouseX > LEFT_X + EYE_SIZE) {
          leftPupilX = LEFT_X + EYE_SIZE;
     //Mouse is in eye
     } else {
          leftPupilX = leftMouseX;
     }

     //Set the Y Coord for the pupil
     //Mouse is above the eye
     if(leftMouseY < LEFT_Y) {
          leftPupilY = LEFT_X;
     //Mouse is below the eye
     } else if(leftMouseY > LEFT_Y + EYE_SIZE) {
          leftPupilY = LEFT_Y + EYE_SIZE;
     //Mouse is in eye
     } else {
          leftPupilY = leftMouseY;
     }
}

最后,您需要更新绘制左瞳孔的代码以反映变量,并实际调用我们的新方法。

//draw left pupil
setLeftEye();
g.setColor(Color.black);
g.fillOval(leftPupilX, leftPupilY, 40, 40);

这些更改应使左眼跟踪您描述的方式。你需要为右眼做类似的事情,因为它有不同的坐标。如果您有任何问题,请告诉我,我会尽力提供帮助。 :)