File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes C++ish Address & Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "C++ish Address &" Watch "C++ish Address &" New topic
Author

C++ish Address &

Rick Gentry
Ranch Hand

Joined: Dec 14, 2002
Posts: 63
How do I pass the address of a variable so that a class will modify the variable and not create a new one? (C++ uses "& variable")
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

Java does not allow you to pass by reference. However, there are ways to get around this:

This wraps the variable in an object (in this case, an array) and passes the object. Since Objects are never passed (only references to objects), you can modify the object. Note that this is the same as passing pointers in C++ (int myClass::aMethod(MyObject *anObject)), not the same as "pass by reference" (int myClass::aMethod(MyObject &anObject)). This is passing the reference (pointer) by value.
[ May 14, 2003: Message edited by: Joel McNary ]

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Rick Gentry
Ranch Hand

Joined: Dec 14, 2002
Posts: 63
The problem is that I'm trying to share a graphics object between several classes. Any ideas on how to do that? I wasn't really clear on that.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
The problem is that I'm trying to share a graphics object between several classes. Any ideas on how to do that? I wasn't really clear on that.
What kind of graphics object? Why can't you just pass a reference to the object? Maybe you need to list some code so we can see exactly where your problem is.


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

Indeed. Java is just like programming with pointers in C++. So when you say:

The AnotherClass object created on line 14 is shared between the two methods. There are two references to the object ("anObject" in the first method and "bob" in the second) that are not shared. That is, if I assigned bob to be a reference to a different object, my reference in the first method is not affected. It will still point to the original object.
The only differences between Java and using C++ this way are:
1). You can't do pointer arithmetic. bob+3 makes no sense in Java, but it does in C/C++.
2). You don't need to use * (or &)
3). You dont need to use '->' '.' replaces that, and takes one fewer keystrokes to type.

Hope that this helps.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Java is just like programming with pointers in C++.
I would say semantically similar. You can't index thru an array with a Java reference like you can with a C++ pointer, that is no pointer math. Just like a reference in C++ (for example int& myIntRef), a Java reference is bound to a single object.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
The only differences between Java and using C++ this way are:
Guess I should have read all of your post huh Joel?
Rick Gentry
Ranch Hand

Joined: Dec 14, 2002
Posts: 63
My code has changed so much that it would be pointless to post it. In short, I have two JComponents (each of them looping as Runnables and calling repaint() inside the loop) that are painting over each other so that only one of them appears. I thought that I could have them share one Graphics object and that might solve the problem.
Rick Gentry
Ranch Hand

Joined: Dec 14, 2002
Posts: 63
refer to http://www.coderanch.com/t/371127/java/java/multiple-painting-components for the code
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451

My code has changed so much that it would be pointless to post it. In short, I have two JComponents (each of them looping as Runnables and calling repaint() inside the loop) that are painting over each other so that only one of them appears. I thought that I could have them share one Graphics object and that might solve the problem.

That's a big architechture problem. Swing components are not thread safe. You should never alter the state of a swing component except on the swing dispatcher thread. I'm still a bit confused on your intent. Are you trying to create some sort of composite effect? Without posting your code try to explain what you're doing.
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

I'm not sure what you problem is. Lacking the "goat.png" file, I downloaded the Moose and used that (moosefly.gif). It appeared breifly on the screen and then vanished. I edited the picture (using mspaint, of all things) to remove the fly and then got the Moose bouncing around in an otherwise empty frame. (Poor Moose! )
I then used the "Post Reply" image and that worked with no modification (It is not a "moving" gif, as the moose is).

What problem are you getting, and can you post a link to your Goat.png file for testing?
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

Ah...never mind... I see that there's supposed to be two balls (or Moose, or whatever....)
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

Try this. What I did was to stop having the balls as components and instead created a BallRegistry. The Ball registry keeps track of how many balls it is displaying and, when its paint method is called, draws each ball that has been registered with it.
This way, all simultaneous calls to draw are being made to the same Graphics object. Note that subsequent requests may not be to the same graphics object, but that doesn't matter, since you are drawing the whole picture at the same time.
You would have to expand this to include paddles; this example just creates balls.
(I got two balls bouncing around the screen. However, while one of them nicely bounced within the confines of the frame, the other just zoomed around randomly, leaving the frame and sometimes changing direction right in the middle of the frame. Not sure if that's what it's supposed to do, but that's left as an exercise for you ... )
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

Originally posted by Rick Gentry:
refer to http://www.coderanch.com/t/371127/java/java/multiple-painting-components for the code

Come to think of it, this thread should probably be continued back on the original topic...
Rick Gentry
Ranch Hand

Joined: Dec 14, 2002
Posts: 63
Thanks for your help. I guess I need to read the whole book before I embark on a large project like this. Just remember though, in a few years there will be a pong game, and in the code, your name will be mentioned in a comment. And 20 or so nerds in a computer science class will leave their snes emulators and play a crappier pong game that you made possible...
Thanks for the help.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Sorry for not getting back with you for so long. For future reference, please keep this discussion here from now on and don't start a new thread. This will help avoid much, much confusion, especially if someone else answers a question first. It also allows us to go back to see how things have progressed.
Also, I don't believe that it is totally pointless to re-post your code. In fact, the opposite is true. If we can see the changes you have made it is more helpful to give suggestions about how to fix it.
I should caution that you don't need to post all the code for the whole program. Specific snippets work best. Choose the parts of the program that illustrate the current problem. Also give a good explanation of what you have tried that doesn't quite work. We'll be glad to help you from there.
Okay, enough of my soap box. You might be interested in a similar program that I have been working on. I was going to post the code on my web site, but the FTP program seems to be broken in this computer lab. Anyways, here's the latest version of my bouncing balls program:

Hopefully this will give you some ideas about drawing multiple objects in a window.


Java API Documentation
The Java Tutorial
 
Consider Paul's rocket mass heater.
 
subject: C++ish Address &
 
Similar Threads
java has the address of a variable´╝č and what is the meaning of the instance count?
Pass by value or reference?
Assesment test question
Pointers in Java?
Array - Security hole