• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

C++ish Address &

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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")
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Rick Gentry
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Joel McNary
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
refer to https://coderanch.com/t/371127/java/java/multiple-painting-components for the code
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah...never mind... I see that there's supposed to be two balls (or Moose, or whatever....)
 
Joel McNary
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rick Gentry:
refer to https://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
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic