8 Replies - 15103 Views - Last Post: 25 February 2011 - 10:19 AM

#1 jumptrooper  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 68
  • View blog
  • Posts: 234
  • Joined: 19-August 10

Java Graphics Challenge [ 1 ]

Post icon  Posted 16 February 2011 - 10:24 AM

Nothing sharpens those fine motor skills like some good-old arts and crafts action and, similarly, having fun messing around with Java is a great way to learn new things.

Here's the "challenge":

Below are two complete Java classes that, when run, paint a never-ending cascade of boxes down the screen ( Yay! :clap: ).
Your mission is to come up with some interesting modifications to it - or simply to use it as a model to come up with something completely different. Indulge your ego! Impress us!

...and, most importantly: Have Fun!


FallingBox class
Spoiler


BoxDropper class
Spoiler


Is This A Good Question/Topic? 4
  • +

Replies To: Java Graphics Challenge [ 1 ]

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10365
  • View blog
  • Posts: 38,399
  • Joined: 27-December 08

Re: Java Graphics Challenge [ 1 ]

Posted 16 February 2011 - 10:26 AM

I'll move this to Java Programmers so it doesn't fall off the page by the end of the night. :)
Was This Post Helpful? 0
  • +
  • -

#3 jumptrooper  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 68
  • View blog
  • Posts: 234
  • Joined: 19-August 10

Re: Java Graphics Challenge [ 1 ]

Posted 16 February 2011 - 10:43 AM

View Postmacosxnerd101, on 16 February 2011 - 09:26 AM, said:

I'll move this to Java Programmers so it doesn't fall off the page by the end of the night. :)


Thanks :)
Was This Post Helpful? 0
  • +
  • -

#4 shintetsu_80  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 105
  • Joined: 01-July 08

Re: Java Graphics Challenge [ 1 ]

Posted 22 February 2011 - 04:03 PM

Since I'm fairly new to Java I thought I'd have a play with this. I added a little bit of linear velocity and scaling to the boxes. I also renamed my classes to be more descriptive of what it's doing.

Basically my additions cause the boxes to spawn from the center of the JFrame and scale in size as they move outwards toward the boundaries of the JFrame. My initiale thinking was to make it look as if you were traveling through a field of boxes in space. There's more configuration needed to fully achieve that effect but I started loosing intrest. Interesting enough for a short play though.

Spoiler


Spoiler

Was This Post Helpful? 2
  • +
  • -

#5 jumptrooper  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 68
  • View blog
  • Posts: 234
  • Joined: 19-August 10

Re: Java Graphics Challenge [ 1 ]

Posted 22 February 2011 - 05:09 PM

View Postshintetsu_80, on 22 February 2011 - 03:03 PM, said:

Since I'm fairly new to Java I thought I'd have a play with this. I added a little bit of linear velocity and scaling to the boxes. I also renamed my classes to be more descriptive of what it's doing.

Basically my additions cause the boxes to spawn from the center of the JFrame and scale in size as they move outwards toward the boundaries of the JFrame. My initiale thinking was to make it look as if you were traveling through a field of boxes in space. There's more configuration needed to fully achieve that effect but I started loosing intrest. Interesting enough for a short play though.

Spoiler


Spoiler

This is cool! Nice Job! I know what you mean about only being able to work on something like this for so long, though.
I have a thought: In order to get a better distribution of random directions the boxes move in, how about just maintaining two variables (xIncrement and yIncrement) and randomly assign each box with a value for each in the range of something like -5 to +5 (or whatever). So box "a" moves -5 in the x-axis and +3 in the y-axis every cycle, while box "b" moves +1 in the x-axis and +2 in the y-axis every cycle, and so on and so forth. Then, of course, as they keep moving, update their size according to their distance from their origin (I believe there is a method that calculates the distance between two Points in the Point class or somewhere...)
Was This Post Helpful? 1
  • +
  • -

#6 shintetsu_80  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 105
  • Joined: 01-July 08

Re: Java Graphics Challenge [ 1 ]

Posted 22 February 2011 - 05:38 PM

View Postjumptrooper, on 22 February 2011 - 05:09 PM, said:

View Postshintetsu_80, on 22 February 2011 - 03:03 PM, said:

Since I'm fairly new to Java I thought I'd have a play with this. I added a little bit of linear velocity and scaling to the boxes. I also renamed my classes to be more descriptive of what it's doing.

Basically my additions cause the boxes to spawn from the center of the JFrame and scale in size as they move outwards toward the boundaries of the JFrame. My initiale thinking was to make it look as if you were traveling through a field of boxes in space. There's more configuration needed to fully achieve that effect but I started loosing intrest. Interesting enough for a short play though.

Spoiler


Spoiler

This is cool! Nice Job! I know what you mean about only being able to work on something like this for so long, though.
I have a thought: In order to get a better distribution of random directions the boxes move in, how about just maintaining two variables (xIncrement and yIncrement) and randomly assign each box with a value for each in the range of something like -5 to +5 (or whatever). So box "a" moves -5 in the x-axis and +3 in the y-axis every cycle, while box "b" moves +1 in the x-axis and +2 in the y-axis every cycle, and so on and so forth. Then, of course, as they keep moving, update their size according to their distance from their origin (I believe there is a method that calculates the distance between two Points in the Point class or somewhere...)



I noticed the distribution was lacking. I know in C++ you can get not so random numbers from your rand function and I figured I was seeing the same thing here. After looking at some code I have for particle emitters I might have clipped it myself. I'll have a look at this a little later.
Was This Post Helpful? 0
  • +
  • -

#7 shintetsu_80  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 105
  • Joined: 01-July 08

Re: Java Graphics Challenge [ 1 ]

Posted 23 February 2011 - 02:07 PM

Well it turns out that I was messing up my linear algebra a bit. Here's my updated code which produces a much better effect. You can get drastic changes in the effect by changing the configuration parameters in the StarFieldBox code. There's still some artifacts with sizing but it looks much better than before.

@jumptropper thanks for the idea on sizing in relation to source position I added that into this code.

StarFieldBoxGenerator:
Spoiler


StarFieldBox:
Spoiler

Was This Post Helpful? 0
  • +
  • -

#8 shintetsu_80  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 105
  • Joined: 01-July 08

Re: Java Graphics Challenge [ 1 ]

Posted 24 February 2011 - 02:10 PM

I came up with another box system that looks pretty cool. Also since I was doing this stuff as a learning exercise I decided to apply some of the design pattern ideas to the code that I've been studying lately.

There's two basic classes a BoxObject class that is an abstract class that any new BoxObject class should inherit from. The next is the BoxObjectGenerator class that knows how to draw BoxObjects.

Now anyone in the future that want's to play with this code only has to create a new BoxObject subclass and tell the BoxObjectGenerator in it's main method to draw that BoxObject subclass. Like so:

        // this will cause BoxObjectGenerator to draw StarFieldBox objects
        BoxObjectGenerator bd = new BoxObjectGenerator(frame, StarFieldBox.class);
        
        // this will cause BoxObjectGenerator to draw RotatingBox objects
        BoxObjectGenerator bd = new BoxObjectGenerator(frame, RotatingBox.class);




So here's my files BoxObject, BoxObjectGenerator, StarFieldBox (that draws a star field of boxes based on the configuration parameters), and RotatingBox (which should be renamed but still produces a cool effect). If someone noticies problems in my design then let me know so I don't keep doing something that's wrong.

Spoiler
[/hide]

Spoiler
[/hide]

Spoiler
[/hide]

Spoiler
[/hide]

This post has been edited by shintetsu_80: 24 February 2011 - 02:19 PM

Was This Post Helpful? 1
  • +
  • -

#9 jumptrooper  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 68
  • View blog
  • Posts: 234
  • Joined: 19-August 10

Re: Java Graphics Challenge [ 1 ]

Posted 25 February 2011 - 10:19 AM

View Postshintetsu_80, on 23 February 2011 - 01:07 PM, said:

Well it turns out that I was messing up my linear algebra a bit. Here's my updated code which produces a much better effect. You can get drastic changes in the effect by changing the configuration parameters in the StarFieldBox code. There's still some artifacts with sizing but it looks much better than before.

@jumptropper thanks for the idea on sizing in relation to source position I added that into this code.

StarFieldBoxGenerator:
Spoiler


StarFieldBox:
Spoiler

This is awesome! I can see what you mean about there being a few artifacts (lots of larger boxes in the up/down/left/right directions)

This post has been edited by jumptrooper: 25 February 2011 - 10:20 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1