Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8508619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:12:40+00:00 2026-06-11T03:12:40+00:00

I’m creating an applet in which a sheep is herded by a dog, both

  • 0

I’m creating an applet in which a sheep is herded by a dog, both are objects as the dog object moves closer the sheep the sheep moves away in a random direction. (I’m sorry I know I’ve asked a lot of questions about this applet). So far everything works except the sheep object wont stay within the bounds I’ve set and eventually disappears off the screen entirely. I’ve tried lots of different things but nothing seems to work. Any ideas why the sheep object wont stay within the bounds? Any help would be much appreciated.
Here is the code, there are two separate java files:

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


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T03:12:42+00:00Added an answer on June 11, 2026 at 3:12 am

    Try adding elses to your long string of if() statements, like so:

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

    The way you had it before, if sheepx was left then 20, and less than the dog’s x, it would still go away from the dog, even though it should’ve been worried about the boundaries. This was because both if statements would trigger, and modify direction, but since you check’d for boundary first, that would be overwritten by the attempt to run from the dog. Try this solution, tell me how it works

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary
I have a text area in my form which accepts all possible characters from
I'm trying to select an H1 element which is the second-child in its group
i got an object with contents of html markup in it, for example: string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.