Results 1 to 10 of 10
Thread: Making a pointer to objects
- 04-01-2010, 11:52 AM #1
Member
- Join Date
- Apr 2010
- Posts
- 16
- Rep Power
- 0
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! :)
- 04-01-2010, 01:27 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
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
- 04-01-2010, 01:53 PM #3
Member
- Join Date
- Apr 2010
- Posts
- 16
- Rep Power
- 0
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...
- 04-01-2010, 01:56 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
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.
- 04-01-2010, 02:03 PM #5
Member
- Join Date
- Apr 2010
- Posts
- 16
- Rep Power
- 0
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.
- 04-01-2010, 02:14 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
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();
- 04-01-2010, 02:22 PM #7
Member
- Join Date
- Apr 2010
- Posts
- 16
- Rep Power
- 0
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
- 04-01-2010, 02:26 PM #8
Member
- Join Date
- Apr 2010
- Posts
- 16
- Rep Power
- 0
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.
- 04-01-2010, 02:46 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Oh, that's good.
You got there...Eureka, as they say!
- 04-01-2010, 04:18 PM #10
Member
- Join Date
- Apr 2010
- Posts
- 16
- Rep Power
- 0
Similar Threads
-
Null Pointer
By theen3my in forum AWT / SwingReplies: 3Last Post: 10-03-2009, 02:10 PM -
an API can return objects without making their classes public
By parthu_lkp in forum Advanced JavaReplies: 1Last Post: 08-11-2009, 02:00 PM -
null pointer help
By mayhewj7 in forum New To JavaReplies: 5Last Post: 02-17-2009, 11:51 PM -
read txt file,with some records, create objects and store objects in tables of a db.
By stamv in forum JDBCReplies: 1Last Post: 01-22-2009, 04:25 PM -
A variable pointer
By tapies in forum New To JavaReplies: 13Last Post: 09-12-2008, 10:44 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks