Results 1 to 5 of 5
  1. #1
    kaiouji1 is offline Member
    Join Date
    Feb 2017
    Posts
    2
    Rep Power
    0

    Default how to getElement for a particular objects in java.awt

    Hi. so I am constructing my shapes with java.awt.geom.*

    Is there any type of method I can use to get the element at a certain point on my field. Thanks

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: how to getElement for a particular objects in java.awt

    What do you mean by "get the element ... on my field?" Since java.awt.geom are not components you can't add them to a panel or frame you have to draw them.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: how to getElement for a particular objects in java.awt

    Please show the definitions for the element and the field.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    kaiouji1 is offline Member
    Join Date
    Feb 2017
    Posts
    2
    Rep Power
    0

    Default Re: how to getElement for a particular objects in java.awt

    so for example. I want to be able to detect other objects from this class. I drew the objects in another class, and the Arraylist of Human objects are located in the MainApplication



    import java.awt.Color;
    import java.util.Random;
    import java.awt.geom.*;

    public class Human {
    private Environment theEnvironment;
    private int myID;
    private Color myColor;
    private HumanPoint myPos;
    private Direction myDir;
    private int myLength;
    private int myHeight;
    private int halfLength;
    private int halfHeight;
    private static int nextAvailableID=1;
    private boolean upDownDir;

    private int wellBeing;
    private int posSupp;
    private int negSupp;
    private static Random generator = new Random();
    private static final int MIN_DISTANCE = 3;
    private static final int MAX_DISTANCE = 70;

    public Human(Environment theEnv){
    this(theEnv, Color.white);
    }

    public Human(Environment theEnv, Color color){
    theEnvironment = theEnv;
    myID = nextAvailableID++;
    myColor=color;
    initSize();
    initPos();

    }
    public void setHumanColor(Color color){
    myColor = color;
    }

    public int id(){
    return myID;
    }
    public Color color(){
    return myColor;
    }
    public HumanPoint position(){
    return myPos;
    }
    public int length(){
    return myLength;
    }
    public int height(){
    return myHeight;
    }

    public boolean facingRight(){
    return myDir.equals(Direction.EAST);

    }
    public String toString(){
    String s = new String();
    String dir = "R";
    s=s+myID+myPos+dir+" ";
    return s;
    }
    private void initSize(){
    myLength = 30;
    myHeight = 30;
    halfLength = myLength/2;
    halfHeight = myHeight/2;
    }
    private void initPos(){
    int padding = 20;
    int myX = generator.nextInt(theEnvironment.width()-myLength - padding) + padding/2;
    int myY = generator.nextInt(theEnvironment.height()-myHeight - padding) + padding/2;
    upDownDir = generator.nextBoolean();

    myX += halfLength;
    myY += halfHeight;
    myPos = new HumanPoint(myX, myY);
    if(generator.nextBoolean()){
    myDir = Direction.EAST;
    }else{
    myDir = Direction.WEST;
    }


    }
    public int distanceToWall()
    {
    int leftEdgeOfHuman = myPos.xCoord();
    int rightEdgeOfHuman = myPos.xCoord() + myLength;
    if ( facingRight() )
    return (theEnvironment.width() - rightEdgeOfHuman);
    else
    return leftEdgeOfHuman + myLength; // since left edge of aquarium is 0
    }

    public boolean atWall()
    {
    return (distanceToWall() <= halfLength);
    }


    public boolean atSurface()
    {
    int topOfHuman = myPos.yCoord() + halfLength ;
    return (topOfHuman <= 0);
    }


    public boolean atBottom()
    {
    int bottomOfFish = myPos.yCoord() + (myHeight);
    return (bottomOfFish >= (theEnvironment.height()));
    }
    public boolean checkCollision(){
    return true;//dont know how to detect the collisions
    }

    public void changeDir()
    {
    myDir = myDir.reverse();

    }

    public void ascend()
    {
    myPos.rise(3);
    }

    public void descend()
    {
    myPos.sink(3);
    }
    public void moveForward()
    {

    int moveAmt = MIN_DISTANCE;
    if ( moveAmt >= distanceToWall() )
    moveAmt = distanceToWall();
    if ( facingRight() )
    myPos.moveRight(moveAmt);
    else
    myPos.moveLeft(moveAmt);
    if(upDownDir == true){
    this.ascend();
    }else{
    this.descend();
    }
    }
    public void move(){


    if(this.atSurface()){
    upDownDir = false;
    this.descend();
    }
    if(this.atBottom()){
    upDownDir = true;
    this.ascend();

    }
    if(this.checkCollision()){

    }


    this.moveForward();

    if(this.atWall()){
    this.changeDir();



    }
    }

    }

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: how to getElement for a particular objects in java.awt

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    I want to be able to detect other objects from this class
    Is an object an instance of a class (all classes derive from the Object class)
    What do you mean by "detect"? For example if the object extends a GUI class and the cursor is moved over the part of the screen where the object is being shown you want to "detect" what object is under the cursor.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 12
    Last Post: 04-29-2012, 02:55 PM
  2. sorting objects in java
    By nn12 in forum New To Java
    Replies: 3
    Last Post: 01-08-2011, 09:18 AM
  3. Java 2d objects
    By Egomeh in forum New To Java
    Replies: 2
    Last Post: 02-02-2009, 06:53 PM
  4. Replies: 1
    Last Post: 01-22-2009, 04:25 PM
  5. com objects in java
    By boy22 in forum New To Java
    Replies: 1
    Last Post: 07-14-2007, 06:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •