• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Add 2D shape to 2D shape and move the first one. Both should move.

 
john price
Ranch Hand
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
Thanks,
cc11rocks
 
Stephan van Hulst
Bartender
Pie
Posts: 5608
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
john price
Ranch Hand
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below...
 
john price
Ranch Hand
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just figured it out. You can use points relative to that rectangle.
Eg:

Thank you!
John Price aka cc11rocks

EDIT: Found I didn't need two lines. They are now removed...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic