Meaningless Drivel is fun!
The moose likes Applets and the fly likes Falling rocks in an applet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Falling rocks in an applet?" Watch "Falling rocks in an applet?" New topic

Falling rocks in an applet?

keegan fitzpatrick

Joined: Jun 03, 2014
Posts: 1
This is code for a game I am making where you are a character at the bottom of the screen dodging falling rocks. I was wondering how to add in the falling rocks. I think I need to use Timer and an ArrayList but I am not sure if that would work and not truly sure how to go about doing it. Any help or tips would be appreciated. Thanks!

Tony Docherty

Joined: Aug 07, 2007
Posts: 2839
Welcome to the Ranch.

I would suggest you create a 'Rock' class that extends Component. The Rock Class needs to override paint() to draw a rock and also needs a method to move it to the next screen location. It may also be required to know how fast it is moving and what direction it is moving in (depending how complex your game is). Also you probably need some hit logic to know if it has touched another item such as another rock or the player.

You are correct in your idea of using a Timer and some form of collection to holds all your rocks. When the game is running you create instances of your Rock class for each rock that is currently falling and each time the timer fires you move the rocks to their next location and call repaint() to redraw the screen. In the paint method you draw the background and the player and then iterate over the collection of rocks and pass the current graphics object to each rock's paint method.
I agree. Here's the link:
subject: Falling rocks in an applet?
jQuery in Action, 3rd edition