How to create an image instance & check if it intersects with anotherimage

J

Jimmy

I'm new to animation in Java. Can someone help me out? Thanks.

I want to create a moving cat & a moving mouse (chasing from left to
right) inside a JPanel. When this cat's mouth touches the mouse's
tail, I need to trigger an event to do something.

- what instance (from standard Java SE 6) can I used to create these
moving animals?
- in 2D only, is there just 1 point (x,y) of the animal (e.g. center
of animal's chest) to control it to move from left to right (e.g. a
loop to increment x-coordinate)
- for the 2nd/previous point above, how I can ensure the animal's head
did went past the end (e.g. right) of the window, as the cat is
chasing the mouse from left to right.
- assuming I can use an animated GIF inside java.awt.Rectangle, then
use intersects(Rectangle r) method to check if the cat's rectangle
intersects the mouse's rectangle ... but what if I need more details
as some point, the cat's front legs may be at mouse's position the
cat's mouth is not 'touching' mouse's tail yet?

Thank you,
Jimmy
 
J

John B. Matthews

Jimmy said:
I'm new to animation in Java. Can someone help me out? Thanks.

I want to create a moving cat & a moving mouse (chasing from left to
right) inside a JPanel. When this cat's mouth touches the mouse's
tail, I need to trigger an event to do something.

- what instance (from standard Java SE 6) can I used to create these
moving animals? - in 2D only, is there just 1 point (x,y) of the
animal (e.g. center of animal's chest) to control it to move from
left to right (e.g. a loop to increment x-coordinate) - for the
2nd/previous point above, how I can ensure the animal's head did went
past the end (e.g. right) of the window, as the cat is chasing the
mouse from left to right. - assuming I can use an animated GIF inside
java.awt.Rectangle, then use intersects(Rectangle r) method to check
if the cat's rectangle intersects the mouse's rectangle ... but what
if I need more details as some point, the cat's front legs may be at
mouse's position the cat's mouth is not 'touching' mouse's tail yet?
[...]

Here's a simple example of timer driven animation along the x-axis:

<http://sites.google.com/site/drjohnbmatthews/subway>

You can use any class that implements the Shape interface to test for
intersection with a rectangular area:

<http://java.sun.com/javase/6/docs/api/java/awt/Shape.html>

Myriad examples:

<http://java.sun.com/products/java-media/2D/samples/suite/>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,685
Members
48,796
Latest member
Greg L.

Latest Threads

Top