Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By pbrockway2

Thread: Position of image

  1. #1
    Join Date
    Dec 2011
    Location
    Belgium
    Posts
    25
    Rep Power
    0

    Default Position of image

    Hey,

    I have this code that should help me to get the position of an image.


    Java Code:
    public class Position {
        
        public int startXCoordinaat;
        public int startYCoordinaat;
        public int endXCoordinaat;
        public int endYCoordinaat;
        public int mousePositionX;
        public int mousePositionY;
        public Position[] position;
    
        public Position() {
    
        }
        public Position(int startXCoordinaat,int startYCoordinaat){
            this.endXCoordinaat = startXCoordinaat + 100;
            this.endYCoordinaat = endYCoordinaat + 100;
        }
    
         public int getImagePostion(Position[] position,int mousePositionX, int mousePositionY){
            int positionGetter = 0;
            this.position = position;
            this.mousePositionX = mousePositionX;
            this.mousePositionY = mousePositionY;
            for(int count = 0; count <= this.position.length; count++)
            {
            positionGetter++;
            //this.position[count].StartXCoordinaat gives a malformed expression error. But I want it to display a certain value.
            if(this.mousePositionX >= this.position[count].startXCoordinaat && this.mousePositionX <= this.position[count].endXCoordinaat)
            {
                System.out.println("count coordinaat: " + this.position[count].startXCoordinaat);
                if(this.mousePositionY >= this.position[count].startYCoordinaat && this.mousePositionY <= this.position[count].endYCoordinaat);
                System.out.println("position found: " + positionGetter);
            }
            }
            return positionGetter;
        }
    }
    This part of the code isn't working:

    I want it to display a value but get a malformed expression error.
    Java Code:
    this.position[count].startXCoordinaat
    I get the value from a view Class

    It's declared like this:
    Java Code:
    public Position[] position;
    where I placed this in a constructor public View()
    Java Code:
    position = new Position[31];
    And this is where I get the values (the count is from a for loop so I know wich opbject Postion has that startXCoordinate and StartYCoordinate.
    Java Code:
      position[count] = new Position(10 + z,130);
    Last edited by The original stinger; 05-10-2012 at 09:31 PM. Reason: placing code tags

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Position of image

    What is a Positie? In the getImagePosition() method the position argument is declared as Position[] but then its value is assigned to the instance variable of type Positie[].

  3. #3
    Join Date
    Dec 2011
    Location
    Belgium
    Posts
    25
    Rep Power
    0

    Default Re: Position of image

    that's postion not positie it's an array of position objects (of this class). Or can't you do such a thing?

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Position of image

    Yes, having a Position[] instance variable is fine.

    Where is beginXCoordinaat declared? And startYCoordinaat? It's a good idea to copy, paste and post the actual code you're using.

  5. #5
    Join Date
    Dec 2011
    Location
    Belgium
    Posts
    25
    Rep Power
    0

    Default Re: Position of image

    beginXCoordinaat = startXCoordinaat I adjusted it.

    The values should come from the mousepostion in the view. The mouse position is passed along just fine. But the statement:
    this.position[count].startXCoordinaat

    gives me nothing.

    If I run the applet the System.out.println("count coordinaat: " + this.position[count].startXCoordinaat); returns a value of 0 wich isn't correct.
    What am I doing wrong here?

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Position of image

    returns a value of 0 wich isn't correct.
    What am I doing wrong here?
    If it prints a value of zero, that's because it has a value of zero.

    If that value is "wrong" - that is, you intend it to have a different value - where did you think you were assigning that value to position[count].startXCoordinaat?

    -----

    One thing you can do is print out the elements of the position array (or t least their startX coordinates) to see what they are:

    Java Code:
    public int getImagePostion(Position[] position,int mousePositionX, int mousePositionY){
        System.out.println("At getImagePosition(), position array start coordinates are:");
        for(int ndx = 0; ndx < position.length; ndx++) {
            System.out.print(position[ndx].startXCoordinaat + " ");
        }
        int positionGetter = 0;
        // etc
    Look at you Position constructor and observe that it does not initialise the value of startXCoordinaat the way it does the end?Coordinaat variables. (Please don't change it in your post, as that makes it well neigh impossible for anyone reading this to follow what's being said. Repost the code instead if it matters.) Also read the second line of that constructor and check for typos.
    Fubarable likes this.

Similar Threads

  1. position cue with cursor
    By stevo812 in forum Java 2D
    Replies: 0
    Last Post: 04-27-2012, 10:06 PM
  2. How to position text?
    By Ike in forum New To Java
    Replies: 2
    Last Post: 10-08-2011, 06:05 PM
  3. get position in string from caret position
    By helloworld111 in forum AWT / Swing
    Replies: 5
    Last Post: 02-19-2009, 01:36 AM
  4. BorderFactory to set position?
    By aleplgr in forum AWT / Swing
    Replies: 2
    Last Post: 01-30-2008, 12:46 PM
  5. Use the mouse position
    By susan in forum Java Applets
    Replies: 1
    Last Post: 07-28-2007, 11:10 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
  •