【问题标题】:Moving object won't stay within set bounds?移动的物体不会停留在设定的范围内?
【发布时间】:2012-09-05 00:50:26
【问题描述】:

我正在创建一个小程序,其中一只羊被一只狗放牧,两者都是对象,因为狗对象靠近羊,羊在随机方向上移开。 (很抱歉,我知道我已经问了很多关于这个小程序的问题)。到目前为止一切正常,除了绵羊对象不会停留在我设置的范围内并最终完全从屏幕上消失。我尝试了很多不同的东西,但似乎没有任何效果。任何想法为什么绵羊对象不会停留在范围内?任何帮助将非常感激。 这是代码,有两个独立的java文件:

MainPanel.java

import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.Random;


import javax.swing.JPanel;

public class MainPanel extends JPanel implements MouseMotionListener
{

    private static final long serialVersionUID = 1L;
    Dog dog;
    Sheep sheep;
    int[] directionNumbersLeft = {0, 1, 3};
    int[] directionNumbersRight = {1, 2, 3};
    int[] directionNumbersUp = {0, 1, 2};
    int[] directionNumbersDown = {0, 2, 3};
    int x;
    int selection;
    int xposR;
    int yposR;
    int sheepx;
    int sheepy;
    int sheepBoundsx;
    int sheepBoundsy;
    int MAX_DISTANCE = 50;
    int direction;
    int distance;

    public MainPanel()
    {

        addMouseMotionListener(this);
        sheepx = 175;
        sheepy = 75;
        dog = new Dog(22,22);
        sheep = new Sheep(sheepx, sheepy);
        sheepBoundsx = 30;
        sheepBoundsy = 30;



    }


    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        dog.display(g);
        sheep.display(g);
        g.drawRect(20, 20, 600, 600);
    }



    public void mouseDragged(MouseEvent e)
    {
        xposR = e.getX();
        yposR = e.getY();
        dog.setLocation(xposR, yposR);
        sheep.setLocation(sheepx, sheepy);
        direction = (int)(Math.random()*4);
        Random rand = new Random();
        x = rand.nextInt(3);

        if (xposR > sheepx&& xposR < sheepx+sheepBoundsx && yposR > sheepy
                && yposR < sheepy+sheepBoundsy && direction == 0){
            sheepx = sheepx + 50;
        }
        if (xposR > sheepx&& xposR < sheepx+sheepBoundsx && yposR > sheepy
                && yposR < sheepy+sheepBoundsy && direction == 1){
            sheepy = sheepy + 50;
        }

        if (xposR > sheepx&& xposR < sheepx+sheepBoundsx && yposR > sheepy
                && yposR < sheepy+sheepBoundsy && direction == 2){
            sheepx = sheepx - 50;
        }

        if (xposR > sheepx&& xposR < sheepx+sheepBoundsx && yposR > sheepy
                && yposR < sheepy+sheepBoundsy && direction == 3){
            sheepy = sheepy - 50;
        }
        if (sheepx <= 20){
            direction = directionNumbersLeft[x];
        }
        if (sheepy <= 20){
            direction = directionNumbersUp[x];
        }
        if (sheepx >= 600){
            direction = directionNumbersRight[x];
        }
        if (sheepy >= 600){
            direction = directionNumbersDown[x];
        }
        if (xposR < sheepx){
            direction = directionNumbersLeft[x];
        }
        if (xposR > sheepx){
            direction = directionNumbersRight[x];
        }
        if (yposR > sheepy){
            direction = directionNumbersDown[x];
        }
        if (yposR < sheepy){
            direction = directionNumbersUp[x];
        }


        repaint();
    }



    public void mouseMoved(MouseEvent e) {


    }



}

SheepDog.java

import java.awt.Color;
import java.awt.Graphics;


import javax.swing.*;


public class SheepDog extends JApplet
{
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    /**
     *
     */


    MainPanel mainPanel;



    public void init()
    {
        mainPanel = new MainPanel();
        add(mainPanel);


    }




}
class Dog
{
    int xpos;
    int ypos;
    int circleWidth = 30;
    int circleHeight = 30;

    public Dog(int x, int y)
    {
        xpos = x;
        ypos = y;

    }

    public void setLocation(int lx, int ly)
    {
        xpos = lx;
        ypos = ly;
    }

    public void display(Graphics g)
    {
        g.setColor(Color.blue);
        g.fillOval(xpos, ypos, circleWidth, circleHeight);
    }
}

class Sheep
{
    int xpos;
    int ypos;
    int circleWidth = 30;
    int circleHeight = 30;

    public Sheep(int x, int y)
    {
        xpos = x;
        ypos = y;

    }

    public void setLocation(int lx, int ly)
    {
        xpos = lx;
        ypos = ly;
    }

    public void display(Graphics g)
    {
        g.setColor(Color.green);
        g.fillOval(xpos , ypos, circleWidth, circleHeight);
        g.drawOval(xpos - 10, ypos - 10, 50, 50);
    }


}

【问题讨论】:

  • 另见examples
  • 在我看来,这是逻辑错误。不是@trashgod指定的链接。能够理清思路,得到逻辑部分?以某种方式删除这么多 if - if else 语句,使其不那么复杂。

标签: java swing random applet


【解决方案1】:

尝试在长长的 if() 语句字符串中添加 else,如下所示:

    if (sheepx <= 20){
        direction = directionNumbersLeft[x];
    } else if (sheepy <= 20){
        direction = directionNumbersUp[x];
    } else if (sheepx >= 600){
        direction = directionNumbersRight[x];
    } else ...

按照您之前的方式,如果sheepx 剩下20 个,并且小于狗的x,它仍然会远离狗,即使它应该担心边界。这是因为两个 if 语句都会触发并修改 direction,但由于您首先检查边界,因此会被从狗身上逃跑的尝试覆盖。试试这个解决方案,告诉我它是如何工作的

【讨论】:

  • 感谢您的回复,我试过了,羊对象仍然超出范围。
  • 一开始,羊似乎会在到达边界然后离开边界时停留在边界内,但是在我和狗一起跟随它之后,它最终会离开边界界限。我不知道为什么它不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-18
  • 1970-01-01
相关资源
最近更新 更多