gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 13:10:44 25 4
gpt4 key购买 nike

正在开发一个绘制两只眼睛的小程序,并使用 MouseMotionListener 来移动它们的眼睛。此外,当鼠标退出内容 Pane 时,眼睛会直视。有一件事我正在努力解决的是我不知道如何限制瞳孔的运动以使其留在眼睛内。你们有任何建议都会很棒。

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

最佳答案

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

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

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

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

这些更改应该使左眼按照您描述的方式跟踪鼠标。您需要对右眼执行类似的操作,因为它具有不同的坐标。如果您有任何问题,请告诉我,我会尽力提供帮助。 :)

关于Java MouseMotionListener 和限制绘制形状的移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22890066/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com