我有一个圆圈,我正在屏幕上拖动。当它击中左边界时,它会停止。当它击中右边界时,它会停止。当它到达顶部边界时它停止。但是当它触及底部边界时,由于某种原因,它只是继续前进。请帮忙。
这是我的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;
}
}
这是我的Panel类:
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);
}
}
这是我的Events类:
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) {
}
}
如果有人知道我做错了什么,请帮忙。谢谢。
答案 0 :(得分:1)
简单回答,在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()的调用。