羊が犬に群れているアプレットを作成しています。犬のオブジェクトが羊に近づくと、羊はランダムな方向に遠ざかります。(このアプレットについて多くの質問をしてしまい申し訳ありません)。これまでのところ、羊のオブジェクトが設定した境界内にとどまらず、最終的に画面から完全に消えることを除いて、すべてが機能します。さまざまなことを試しましたが、何もうまくいかないようです。羊オブジェクトが境界内にとどまらない理由はありますか? どんな助けでも大歓迎です。コードは次のとおりです。2 つの別個の 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) {
}
}
シープドッグ.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);
}
}