Results 1 to 10 of 10
  1. #1
    Bongeh is offline Member
    Join Date
    Apr 2010
    Posts
    16
    Rep Power
    0

    Default Making a pointer to objects

    Hi there,

    I'm primarily an objective-c coder, with experience in php and very little javascript.

    I have recently embarked apon a little journey into making a java app because i needed a stand alone application that would run on macs/windows.

    It's more of a utility to aid in the creation of an iphone app. (it generates code based on positions of items on scene).

    Now, so far, i've got a scrollingPanel, with images inside that, i can track their position, and move them with them with the arrow keys. updating their position to where i need it.

    My problem is, i can't seem to work out how to select which image i want to move.

    When an item is added into my panel, i add an item to a JList, i was hoping to be able to select an item within this list, and doing so would 'select' that item and let me move it.

    I think i can do that, i just need to know how to make a pointer to an object, so in my keypressed codes it would always refer to a pointer called

    selectedItem;

    Whenever i select an item in my JList, i need to be able to say something like

    selectedItem = image3;

    Have i overlooked something? i can't seem to work out how this is done.

    Thanks in advance for any hints, tips or help! :)

  2. #2
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    if u can get their position, and the position of the mouse
    then all you gota do is in a for loop.
    Something like this:

    int[] mousePos;
    for (int i = 0; i<listOfImages.lenght;i++){
    cImg = listOfImages[i];
    if (mousePos.x > cImg.Position.x && mousePos.x < cImg.Position.x + cImg.getWidth()){

    if (mousePos.y > cImg.Position.y && mousePos.y < cImg.Position.y + cImg.getHeight()){
    selectedImg = cImg;
    break;
    }
    }
    }

    But since you have dont java and php I guess u know this and have a reason for not being able to do so..
    But I still wanted to give it a shot as see if it helps :D

  3. #3
    Bongeh is offline Member
    Join Date
    Apr 2010
    Posts
    16
    Rep Power
    0

    Default

    Yeah, occasionally i'll have items ontop of each other, using the mouse to pic the item is really what i require, selecting an item on my list will work, i just need to find a way of setting a pointer, and say, if x is selected on my list, x is the object that gets moved.

    So selecting x would set the pointer to x.

    if that makes any sense...

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default

    We use references, not pointers.
    But if you know which one in the list it is, then simply use that reference, which is what Addez's code is doing by setting selectedImg to the one that fits the xy of the mouse click.

  5. #5
    Bongeh is offline Member
    Join Date
    Apr 2010
    Posts
    16
    Rep Power
    0

    Default

    I have listeners for the up arrow and down arrow keys.

    In the code for them, i want to say,

    selectedObject (move up on your Y axis by 1pixel)

    or

    selectedObject (move down on your Y axis by 1 pixel)


    Clicking into my list, i would have something that does something like..

    selectedObject = image3;

    or

    selectedObject = image8.

    Presuming i was selecting the 3rd and 8rd entry into my list.

    that way, whenever i use the arrow keys, it will move my 'selectedObject'

    I'll google references and see what it comes up with. thankyou.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default

    I'm a bit lost now.
    You have a key listener (or listeners).
    When the key is pushed the selected object moves.

    Presumably your object has coordinates?
    This is quite difficult without seeing code...but does this pseudo-java look like the sort of thing?

    Java Code:
    // User clicks the screen.
    // selectedObject is of class MyObject, just for the sake  of giving it a name.
    selectedObject = findObjectUnderMouse(mouseEvent);
    // findObjectUnderMouse() sort of does what Addez shows, but returns the found object
    // rather than setting it.
    
    // then the user hits the up arrow.
    // this is in your listener.
    selectedObject.moveUp();

  7. #7
    Bongeh is offline Member
    Join Date
    Apr 2010
    Posts
    16
    Rep Power
    0

    Default

    yeah, thats pretty much what im trying to acheive, but rather than using the mouse to select the item, it'd be an actionPerformed on the jList but don't worry about that bit.

    What im trying to assertain is how i declare and set the 'pointer'

    selectedObject

  8. #8
    Bongeh is offline Member
    Join Date
    Apr 2010
    Posts
    16
    Rep Power
    0

    Default

    oh my god! lol it's as simple as saying

    JLabel selectedObject;

    then when i set it, i do..

    selectedObject = image1; or whatever.

    Sorry for trouble, i understand now lol.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default

    Oh, that's good.
    You got there...Eureka, as they say!

  10. #10
    Bongeh is offline Member
    Join Date
    Apr 2010
    Posts
    16
    Rep Power
    0

Similar Threads

  1. Null Pointer
    By theen3my in forum AWT / Swing
    Replies: 3
    Last Post: 10-03-2009, 03:10 PM
  2. Replies: 1
    Last Post: 08-11-2009, 03:00 PM
  3. null pointer help
    By mayhewj7 in forum New To Java
    Replies: 5
    Last Post: 02-18-2009, 12:51 AM
  4. Replies: 1
    Last Post: 01-22-2009, 05:25 PM
  5. A variable pointer
    By tapies in forum New To Java
    Replies: 13
    Last Post: 09-12-2008, 11:44 AM

Posting Permissions

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