aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Collision algorithm? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Collision algorithm?" Watch "Collision algorithm?" New topic
Author

Collision algorithm?

Be Khuong
Greenhorn

Joined: Mar 31, 2012
Posts: 9

Hello,

Can someone give me a simple way to detect collision between components in jframe?

Thank,


Java SE, Java EE, NetBeans IDE, Eclipse IDE
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
Turn up the sound.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18909
    
    8

And if you don't think that Michael's suggestion applies to your situation, then it would help if you explained what you mean by "collision between components". Perhaps a small code example would help.
Jay Orsaw
Ranch Hand

Joined: Jun 14, 2011
Posts: 356

No offense to the previous posters, but I really dislike when stuff that is unneeded is said, especially if someone doesn't understand what they are doing and really cannot put out any code. Really turn up the sound?

That being said the most obvious thing would be to check the intersection of the 2 points, depending on what you are trying to do.

For example

http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/Rectangle.html

Rectangle intersection(Rectangle r)
Computes the intersection of this Rectangle with the specified Rectangle.
boolean intersects(Rectangle r)
Determines whether or not this Rectangle and the specified Rectangle intersect.


Rectangle2D createIntersection(Rectangle2D r)
Returns a new Rectangle2D object representing the intersection of this Rectangle with the specified Rectangle2D.
Rectangle2D createUnion(Rectangle2D r)
Returns a new Rectangle2D object representing the union of this Rectangle with the specified Rectangle2D.

depending on what you want to do after the intersection is up to you, good luck hope this helps.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Really turn up the sound?

a lame question will always deserve a lame answer
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18909
    
    8

Michael, come on, be nice. (It's the motto of the forum, remember.)

But really I don't think of things that you draw in a Swing application as "components". To me "components" are things like JButtons, which normally don't go about colliding. So your guess might be right, Jay, but I think the original poster owes us a bit more of an explanation.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
my post wasn't directed at Jay, just an explanation of my first post.
apologies if there's any offence taken (certainly unintended).

anyway, back on topic, I too would like to find out more about how/why the components are bumping into each other.
Jay Orsaw
Ranch Hand

Joined: Jun 14, 2011
Posts: 356

Paul Clapham wrote:Michael, come on, be nice. (It's the motto of the forum, remember.)

But really I don't think of things that you draw in a Swing application as "components". To me "components" are things like JButtons, which normally don't go about colliding. So your guess might be right, Jay, but I think the original poster owes us a bit more of an explanation.


I don't doubt his wording was incorrect, but some of us don't always know the exact terminology; however it isn't hard to guess what exactly he is talking about. (To my knowledge there isn't a ton of collision detection methods just the intersect).


I see this all the time on this forum that people really don't like to help others unless they

A. Try and put some code up(which sometimes cannot be done if you are extremely confused[Like in my TransferHandler thread])
B. They try and help themselves in some fashion.
C. Know how to 100% articulate their words and terminology.

The ranch to me should be about helping each other out even the noobies who are struggling, we were all new once, and sometimes we need help even if we cannot help ourselves at first.


I dislike the extreme egos I see on this forum that don't want to help anyone out in respect to these 3 points. Besides not answering the questions, and putting up a pointless post, the people, to me, look very childish. I really do not want to deal with such people, nor do the thread starters. We should be the role models and if there is an issue try and answer the question the best you can, and explain it to them.

I do, however, agree the OP should have explained more at first, but since I answered his question to the best of my ability he can either
A. Learn from it and it hopefully will answer his question
B. He will say it doesn't help much and will hopefully explain more now that I have given him a step in the right direction.

I also know that this is a site that we all are spending our free time to help others and that we should be as clear as possible. I REALLY appreciate EVERYTHING I have learned from this site, it is a great place, and there are tons of helpful/warm people, but really, pointless posts aren't needed and not only are you wasting the thread starter's time, but your own as well. why post if it's not going to solve anything? That being said Thank you Paul for the help you have given me I appreciate it greatly.

No offense to anyone in particular, just my feelings about it all,

I hope this question is resolved,

~JO

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4658
    
    5

It's not about egos at all. This forum, like most programming forums with the notable exception of one major forum that actively encourages spoonfeeding via reward points, aims to teach. Some posters need to learn not just Java, but how to ask a better question. A little sarcastic humor early on goes a long way toward making them realize that.

For those who are genuinely incapable of learning to ask an answerable question, or throw a hissy fit at a misperceived insult, a forum isn't the best learning resource. They too, discover that early and don't clutter the forum with vague, unanswerable questions.

Just my 2p on the matter.


luck, db
There are no new questions, but there may be new answers.
 
Don't get me started about those stupid light bulbs.
 
subject: Collision algorithm?