• Post Reply Bookmark Topic Watch Topic
  • New Topic

Bouncing balls with trails - for fun and play  RSS feed

 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some time ago there was a thread on a bouncing balls program. The other day I added to that code so that the balls would have trails. I do this by each iteration blurring and darkening the previous image buffer before painting the new balls over it. HAPPY THANKSGIVING (USA)
Two lines of code were Java 8.
blur2() and darken2() were attempts at performance improvement and are not currently being called.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's quite mesmerizing. Now I have to figure out how it works and see if I can refactor it a little. Too bad we aren't able to give out turkeys today so have a cow instead.
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some refactoring. Moved image processing into its own utility class.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not quite convinced that ImageProcessing is a good class to extract that stuff out to. Here's what I've done so far to clean up the code, mostly around the Ball class for now. Still reading up on Kernel and the darkening and blurring graphics stuff. I haven't done a lot of graphics programming so it may take me another day or two to figure out the rest of the code.

Summary of changes I made:

1. Gave the Ball class the responsibility of creating a random list of balls: static newList()

2. Changed newRandomLocation() to adjustBounds() so that the balls would stay on their current trajectories when window is resized. They now just "be like water" and adjust to their container size. I like watching what the balls do now when you resize the window.

3. Refactored for clarity: did a lot of extracting to methods.  I had created a bunch of constants for MIN/MAX_COUNT, MIN/MAX_RATE, MIN/MAX_SIZE but since they were only used in the one place, I just inlined the values back in their respective extracted methods.

 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I understand most of what the program is doing now. I tried using RescaleOp to darken the trails but it produced really long trails, no matter how much I tried increasing the scale factor. I guess it's the same deal with the problem of never ever getting there if you only step 1/2 of the way closer each time.

Below is the refactoring that I have done this morning. Have to go out for some late Thanksgiving day shopping with the wife but thanks, Carey, for the exercise. It was fun.

 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Couldn't resist one more refactoring, although this one violates my "don't do evil premature optimization" rule and actually in that sense, it's not truly a "refactoring":

I don't know if this will even result in any significant performance gains or not; I'd be curious to find out but my wife is calling out to me to knock it off and get ready... C'ya later!
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm glad you enjoyed this Junilu. I enjoyed seeing your refactoring, especially the RGB enum. Thanks for the cow.
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How did you get this to compile?

I had to do this

 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See the static import, line 3 in the code that I posted.

In fact, if you also static imported RGB. isBlack, you'll also just write
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I add
import static RGB.*;

I get
The import RGB cannot be resolved

I have a playpen project in Eclipse with no packages. RGB resides in the same .java file as the rest of the code.
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never mind. Seems like import static and default packages are incompatible.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got the RescaleOp working for darkening. It seems to work if you only have one RescaleOp that you reuse over and over instead of creating one every time you call darken().

A somewhat challenging enhancement to this program might be to simulate the balls bouncing off each other in addition to bouncing off the boundaries. Then you can add momentum/inertia based on ball size. I'm going to try it. Will have to research or dig up some of my old physics and mechanics/dynamics knowledge to implement it. Probably call the program Entropy or Chaos.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!