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.
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.
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.
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.
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!
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.