Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

How to make these 2 rectangles collide?

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the code below i managed to make these rectangles collide and bound at the Jpanel border, but why is that these rectangle do not bound each other when they collide, instead they overlap each other ?
I think there is a problem within my actionPerformed method. Please tell me why they overlap each other and how to overcome it.
Below is the code.Thank you.








 
Marshal
Posts: 70711
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

It would be better not to extend JFrame, nor make display classes implement action listener. Give the paintComponent() method protected access, not public. Also flag it with the @Override annotation to verify you haven't made a tiny spelling error. I am afraid I am finding your variable names difficult to read, but I presume some mean the velocity you are moving your rectangles at.
Use the following idiom to add an action listener, which takes advantage of ActionListener being a functional interface, even though it isn't marked with that annotation:-If the syntax is unfamiliar, look in the Java™ Tutorials.
Repeatedly calculate the distance between the two rectangles and use that to change the direction; you won't get anything from Rectangle2D#intersects() until after they start to overlap. Beware of hard‑coding the width (450) (line 81/84): use some getWidth() method instead. Hard‑coding numbers is error‑prone: besides, the user may resize the display and make 450 incorrect.
Did you write your own Rectangle class or did you use java.awt.Rectangle? I would probably have used Rectangle2D, but that is a supertype of the java.awt class.
 
Bartender
Posts: 4109
156
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that you have two fields rectanlgle, but in your paintComponent you use two different (local) rectangles. The field rectangles do not get changed, so that is why the rectangles that you see overlap and contimue their way.

So, in your paintComponent, just do:
 
don krieg
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Piet Souris. Help me a lot.
 
WARNING! Do not activate jet boots indoors or you will see a tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic