• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Bouncing balls with trails - for fun and play

 
Bartender
Posts: 11151
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 17735
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 11151
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some refactoring. Moved image processing into its own utility class.
 
Junilu Lacar
Sheriff
Posts: 17735
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17735
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17735
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 11151
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 11151
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How did you get this to compile?

I had to do this

 
Junilu Lacar
Sheriff
Posts: 17735
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 11151
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 11151
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Never mind. Seems like import static and default packages are incompatible.
 
Junilu Lacar
Sheriff
Posts: 17735
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 5128
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why is the timer.start() statement inside of the paintComponent method?
What if it were in the initTimer method?

Would it hurt the code any if there were comments in the code saying why it was done that way?
 
Carey Brown
Bartender
Posts: 11151
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Point taken on the initTimer() method. Don't know why I did it that way. I might have been thinking the Timer was a one-shot.

Comments would be lovely. I wrote this code for myself in a hurry. I did manage to clean up the code at least before posting it.

The basic loop is:
  • Blur what is showing on the screen (blur accumulates with each iteration of the loop except for newly painted balls)
  • Darken what is showing on the screen (ditto accumulates)
  • Paint the balls at their new locations.
  •  
    Norm Radder
    Rancher
    Posts: 5128
    38
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    From src.zip:
    TimerQueue.addTimer() -  If the Timer is already in the queue, then ignore the add.

    So multiple calls to start are ignored.
     
    There will be plenty of time to discuss your objections when and if you return. The cargo is this tiny ad:
    Clean our rivers and oceans from home
    https://www.kickstarter.com/projects/paulwheaton/willow-feeders
    reply
      Bookmark Topic Watch Topic
    • New Topic