Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Help with snow  RSS feed

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
I'm supposed to make a frame with a snowman and falling snow. I've finished the snowman, but I've suddenly run into a mind block.
I can't, for the life of me, figure out how to make falling snow. Algorithm suggestions would be greatly appreciated, as would actual code snippets.
Thanks.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just slapped something together and it seemed to work OK
add to a frame, a panel with a background image of a snowman.
set the frame's size(500,500) and setResizable(false) - not the best, but you can hard-code some of the values this way.
add to the panel another panel (transparent) - this handles the snow

classes for the snow
1)
SnowFlake
int x, y (for coords)
set y = 10, x = a random int(500)
2)
SnowLayer
has an array of SnowFlakes
array.length is a random number say, 5 to 10

in the transparent panel, have an arraylist for snowLayers
start a timer, to do:
1) add a snowLayer
2) repaint
3) check arraylist.size() == 10, remove(0), and add a new snowLayer

in the transparent panel's paintComponent()
1) super.paintComponent(g);
2) setColor to white
3) iterate the snowLayer arraylist, with inner iteration of each snowlayer's
snowFlakes array - to get to each snowFlake's x and y, then g.fillOval(x,y,10,10)
also inside the inner iteration add 50 to each snowFlake's y (this will have
the effect of the snowlayer 'falling'

put it all together, and, well, it's not brilliant, but it works.
(from there you can tweak a few things)
 
mark geroso
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have several snowflake images I load and use. The problem with my code is that the panel with the snowman gets covered. And the snowflake images leave a trail. I can't upload the snowflake images, but I'm sure that just by looking at the code you gurus can point out my mistake. *crosses fingers*


I did rewrite it without all the panels, just drawing on the frame itself, and it generally worked, but the snowman flickered a lot, because it kept getting repainted and that was what I was trying to avoid.
Sorry for the long post and I hope you can help. Thanks. If you need me to post the code without the panels, lemme know.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
don't have time to go through it all at present, but this is what I'd slapped
together - not brilliant, but the concept seems to work OK (might be something
in it for you)

needs Snowman.gif

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!