This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
The title ahead explains exactly what I am trying to do. I want to add a shape to another shape. When I move the first one, I want them both to move (which would happen if I added the 2nd shape to the first).
For example, here is some code. It is not correct and giving me errors. How would I do this?
Hopefully, you can see what I am trying to do. Is there a way to do this?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” (Mosher's Law of Software Engineering)
“If debugging is the process of removing bugs, then programming must be the process of putting them in.” (Edsger Dijkstra)
What do you mean, you want to add one shape to another? You have to be more specific, I can't tell what it is you're trying to do.
Here are some tips I strongly urge you to consider:
- Don't let DBall implement ActionListener and KeyListener. Make separate classes that implement these interfaces (hint: anonymous classes) and add them to your panel instead.
- Don't add a JFrame field to your class. There is nothing your panel does with the frame, so leave the field out. It's not an integral part of your panel. Make it a local variable in your main method instead. Same goes for the content pane.
- Make your fields private. Really. Do this. I mean it.
- Use meaningful identifiers. a, b, c, d are not meaningful. Use x, y, width and height instead.
- Use KeyAdapter instead of KeyListener.
Explain what you're trying to do in line 52.
The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.