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

  • SEARCH
  • Home
  • 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 8466525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:22:04+00:00 2026-06-10T15:22:04+00:00

I’m trying to create an applet in which the user gets a sheep into

  • 0

I’m trying to create an applet in which the user gets a sheep into a pen by moving a dog towards the sheep and making the sheep move away from the dog in a random direction. The sheep and dog are defined as objects.

The applet is still in its very early stages. So far I can drag the dog object around the screen and when the dog object comes close to the sheep object it moves but only within a certain area (within bounds I have set). I’m not looking for the solution I’m just looking for some help.

What I would like help with is making the sheep object move in a random direction away from the dog when the dog object comes within the bounds I have set and not just move within the set bounds. Any help or tips would be greatly appreciated. Here is my code:

    package mandAndDog;

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;


public class SheepDog extends Applet implements ActionListener, MouseListener, MouseMotionListener
{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    /**
     * 
     */

    Dog dog;
    Sheep sheep;
    int xposR;
    int yposR;
    int sheepx;
    int sheepy;
    int sheepBoundsx;
    int sheepBoundsy;

    public void init()
    {
        addMouseListener(this);
        addMouseMotionListener(this);
        dog = new Dog(10, 10);
        sheep = new Sheep(200, 100);
        sheepx = 175;
        sheepy = 75;
        sheepBoundsx = 50;
        sheepBoundsy = 50;


    }
    public void paint(Graphics g)
    {
        dog.display(g);
        sheep.display(g);

    }
    public void actionPerformed(ActionEvent ev)
    {}
    public void mousePressed(MouseEvent e)
    {}
    public void mouseReleased(MouseEvent e)
    {}
    public void mouseEntered(MouseEvent e)
    {}
    public void mouseExited(MouseEvent e)
    {}
    public void mouseMoved(MouseEvent e)
    {
    }
    public void mouseClicked(MouseEvent e)
    {}
    public void mouseDragged(MouseEvent e)
    {
        dog.setLocation(xposR, yposR);
        if (xposR > sheepx&& xposR < sheepx+sheepBoundsx && yposR > sheepy
                && yposR < sheepy+sheepBoundsy){
            sheep.setLocation(xposR + 50, yposR + 50);
        }

        xposR = e.getX();
        yposR = e.getY();
        repaint();

    }
}

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 = 10;
    int circleHeight = 10;

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


}
  • 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-10T15:22:05+00:00Added an answer on June 10, 2026 at 3:22 pm

    You aren’t looking for code, so here is what what I would do. This is very basic, and could yield predicable results due to the randomness of the JVM however, I like to use the built-in Math.random() function to generate a random number and the modulus operator to bound the random number to some integer value between 0 and an upper bound.

    I would recommend calculating the direction and distance of travel as a vector.

      int MAX_DISTANCE = 50;
      int direction = (int) (Math.random() * 360) % 360;
      int distance = (int) (Math.random() * MAX_DISTANCE) % MAX_DISTANCE;
    

    Here distance is a measure of pixels, and direction is a measure of the angle of travel in degrees.

    since we are working in vectors, we can calculate the new pixel location using the angle and distance, and some very basic trig.

    I would suggest putting this into a function to calculate the new target location, this way, the function can calculate the new location, and if that location is too close to the dog, you can decide on a new path.

    If I can make a suggestion, I suggest putting the sheep class into a Timer class that recalculates the sheep’s next movement. In this way, the sheep can wander around. While wandering, the sheep might have a much smaller MAX_DISTANCE for each movement. Thereby simulating a level of threat.

    If you really want to get into the simulation, you might measure the distance of the dog to the sheep, and based on the distance, scale the speed of the sheeps movements, so that the closer the dog gets, the fast the sheep moves (not to exceed a maximum distance). If you do this, I would recommend you consider using Math.log() to scale the movements so that a 10 pixel reduction in distance from dog to sheep has a smaller effect on the sheep’s movement when the dog is 200 pixels away than when the dog is 15 pixels away.

    hope this helps.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to select an H1 element which is the second-child in its group
I'm trying to create an if statement in PHP that prevents a single post
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
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into

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.