形状不会停在下边界

Shape not stopping at the down boundary

我在屏幕上拖动了一个圆圈。当它到达左边界时,它会停止。当它到达右边界时,它停止。当它到达顶部边界时它会停止。但当它触及底部边界时,由于某种原因,它会继续前进。请帮忙。

这是我的 MouseEvents class:

public class MouseEvents {

int x1;
int x2;
int y1;
int y2;
int dBMPPAFX;// difference-between-mouse-pointer-position-and-first-x-position
int dBMPPAFY;// difference-between-mouse-pointer-position-and-first-y-position
int width;
int height;
static boolean inside_;
static String whichBound = "";
static String text = "";

MouseEvents(int x1, int y1, int width, int height) {

    this.x1 = x1;
    this.y1 = y1;
    this.width = width;
    this.height = height;
    this.x2 = x1 + width;
    this.y2 = y1 + height;

}

static String hitBound(MouseEvents e, int width, int height) {
    whichBound = "";

    if (e.x1 <= 0) {
        whichBound = "left";
        e.x1 = 0;
        e.x2 = e.x1 + e.width;

    }
    if (e.x2 >= width) {
        whichBound = "right";
        e.x2 = width;
        e.x1 = e.x2 - e.width;

    }
    if (e.y1 <= 0) {
        whichBound = "up";
        e.y1 = 0;
        e.y2 = e.y1 + e.height;

    }
    if (e.y2 >= height) {
        whichBound = "down";
        e.y2 = height;
        e.y1 = e.y2 - e.height;

    }

    return whichBound;

}

static void showLocationOfXAndY(MouseEvents e, int x, int y) {

    e.text = Integer.toString(x) + "," + Integer.toString(y);

}

static boolean inside_(MouseEvents e, int x, int y) {
    if (x > e.x1 && x < e.x2 && y > e.y1 && y < e.y2) {
        e.dBMPPAFX = x - e.x1;
        e.dBMPPAFY = y - e.y1;
        e.x1 = x - e.dBMPPAFX;
        e.y1 = y - e.dBMPPAFY;
        e.x2 = e.x1 + e.width;
        e.y2 = e.y1 + e.height;

        return true;
    } else {
        return false;
    }

}

static void dragShape(MouseEvents e, int x, int y) {

    e.x1 = x - e.dBMPPAFX;
    e.y1 = y - e.dBMPPAFY;
    e.x2 = e.x1 + e.width;
    e.y2 = e.y1 + e.height;
}

}

这是我的面板class:

public class Panel extends JPanel {
int panelWidth;
int panelLength;

static Panel panel = new Panel(400, 400);
static JFrame frame = new JFrame("Monster");
static MouseEvents circle = new MouseEvents(250, 250, 50, 50);
static Events here = new Events();

@Override
public Dimension getPreferredSize() {
    return new Dimension(panelWidth, panelLength);
}

Panel(int width, int length) {
    panelWidth = width - 9;
    panelLength = length - 9;

}

public void paintComponent(Graphics g) {

    super.paintComponent(g);

    g.setColor(Color.BLACK);

    g.drawOval(circle.x1, circle.y1, circle.width, circle.height);
    g.drawString(circle.text, 100, 100);

}

public static void main(String args[]) {

    frame.add(panel);
    panel.addMouseListener(here);
    panel.addMouseMotionListener(here);

    Frame.showFrame(frame, false);

}

}

这是我的活动class:

public class Events implements ActionListener, MouseListener, MouseMotionListener {
String s;

public void mouseDragged(MouseEvent e) {

    if (MouseEvents.inside_ && MouseEvents.hitBound(Panel.circle,400,400).equals("")) {
        dragShape(Panel.circle, e.getX(), e.getY());
        s = MouseEvents.hitBound(Panel.circle,400,400);
        System.out.println(s);

    }

    if (MouseEvents.inside_ && MouseEvents.hitBound(Panel.circle,400,400).equals("left")) {
        if(Panel.circle.x1>=0){
            dragShape(Panel.circle, e.getX(), e.getY());
        }

    }
    if (MouseEvents.inside_ && MouseEvents.hitBound(Panel.circle,400,400).equals("right")) {
        if(Panel.circle.x2<=400){
            System.out.println("What...."+Panel.circle.x2);
            dragShape(Panel.circle, e.getX(), e.getY());
            showLocationOfXAndY(Panel.circle, Panel.circle.x2, Panel.circle.y2);
        }
    }
    if (MouseEvents.inside_ && MouseEvents.hitBound(Panel.circle,400,400).equals("up")) {
        if(Panel.circle.y1>=0){
            dragShape(Panel.circle, e.getX(), e.getY());
        }
    }
    if (MouseEvents.inside_ && MouseEvents.hitBound(Panel.circle,400,400).equals("down")) {
        if(Panel.circle.y2<=400){
            System.out.println("What...."+Panel.circle.y2);

            dragShape(Panel.circle, e.getX(), e.getY());
            showLocationOfXAndY(Panel.circle, Panel.circle.x2, Panel.circle.y2);

        }
    }

    Panel.frame.repaint();



}

public void mouseMoved(MouseEvent e) {
    showLocationOfXAndY(Panel.circle, Panel.circle.x2, Panel.circle.y2);

    Panel.frame.repaint();

}

public void mouseClicked(MouseEvent e) {

}

public void mouseEntered(MouseEvent e) {

}

public void mouseExited(MouseEvent e) {

}

public void mousePressed(MouseEvent e) {

    Panel.circle.inside_ = inside_(Panel.circle, e.getX(), e.getY());
    System.out.println(Panel.circle.inside_);
    System.out.println(hitBound(Panel.circle,400,400));

}

public void mouseReleased(MouseEvent e) {

    Panel.circle.inside_ = false;



    System.out.println("x1:"+Panel.circle.x1+"\ny1:"+Panel.circle.y1+"\nx2:"+Panel.circle.x2+"\ny2:"+Panel.circle.y2);

}

public void actionPerformed(ActionEvent e) {

}

}

如果有人知道我做错了什么,请帮忙。谢谢。

简单的答案,在你的 mouseDragged() 方法的底部,就在 repaint() 之前,添加一个 hitBound(),如下所示:

...
MouseEvents.hitBound(Panel.circle,400,400);
Panel.frame.repaint();
...

那么为什么会这样呢?在 mouseDragged 方法中的每种情况下,您检查 hitBound 是否等于不同的边缘情况之一。这很好用,代码相应地进入了这些部分。当您处于其中一种情况时,您会检查圆圈是否处于可移动的有效位置,如果是,您只需让用户再次移动圆圈即可。

这就是罪魁祸首。 例如:在最后一个不起作用的情况下,您检查以下内容:

MouseEvents.hitBound(Panel.circle,400,400).equals("down")

这是真的。那你继续查:

if(Panel.circle.y2 <= 400)

这也是正确的,因为 hitBound() 调用会将 y2 设置为等于 400!

那为什么其他case可以呢?其他case其实也有同样的问题,但是你看不到,因为你在next if后面调用了hitBound()个案。这将按计划停止圆圈在边界的位置。

正如我之前提到的;解决此问题的一个简单方法是在重绘之前添加对 hitBound() 的调用。