gpt4 book ai didi

java - 形状未停止于下边界

转载 作者:行者123 更新时间:2023-11-30 08:16:44 25 4
gpt4 key购买 nike

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

这是我的 MouseEvents 类:

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

}

这是我的面板类:

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

}

}

这是我的事件类:

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!

为什么其他情况还能工作?其他情况实际上也有同样的问题,但你看不到它,因为你稍后在下一个 if 情况下调用了 hitBound() 。这将按照计划停止边界处圆圈的位置。

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

关于java - 形状未停止于下边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29549431/

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