Making Snow

Ok, here's the code:

import java.awt.*;

import javax.swing.*;

publicclass Blarg

{

publicstaticvoid main(String[] args)throws Exception

{

JFrame frame =new JFrame("Blargity");

APanel panel =new APanel();

frame.add(panel);

frame.setSize(250,250);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setResizable(false);

frame.setVisible(true);

while(true)

{

panel.repaint();

Thread.sleep(100);

}

}

}

import java.awt.*;

import javax.swing.*;

import java.util.Random;

publicclass APanelextends JPanel

{

Random rand =new Random();

int num = 5;

int num2 = 0;

int num3 = 125;

int num4 = 125;

int x = 0;

int y = 0;

int sub = rand.nextInt(10);

int sub2 = 5;

publicvoid paint(Graphics g)

{

Font myfont =new Font("arial",Font.BOLD|Font.ITALIC,20);

Color black =new Color(0,0,0);

Color white =new Color(250,250,250);

Color blue =new Color(0,0,255);

g.setColor(black);

g.fillRect(0,0,250,250);

g.setColor(white);

do

{

g.drawOval(num,num2,1,1);

x++;

num = ((int)Math.sqrt(num)) * 15 - (y * 5);

if (num2 > 250)

num2 = 0;

}while(x < 40);

num2+=3;

}

}

It should make like 40 snowflakes, but it only makes one. What's wrong with this?

[3132 byte] By [TheGuy@YourWindowa] at [2007-10-2 6:25:29]
# 1
if im not mistaken youre drawing all 40 snowflakes in exactly the same place so it just looks like one.
Cowsrulea at 2007-7-16 13:27:18 > top of Java-index,Java Essentials,New To Java...
# 2

> if im not mistaken youre drawing all 40 snowflakes in

> exactly the same place so it just looks like one.

no, he actually is drawing them on a different spot.. but only on the x-axis.

the problem is num2 (the y-axis in this case) isn't being updated until the end of the loop.

instead of having this:

}while(x < 40);

num2+=3;

try this:

num2+=3;

}while(x < 40);

that way all the flakes won't be drawn at 0 on the y-axis.

Woogleya at 2007-7-16 13:27:18 > top of Java-index,Java Essentials,New To Java...
# 3
> that way all the flakes won't be drawn at 0 on the> y-axis.erm, not 0, whatever num2 is at the beginning of the loop. either way all 40 flakes are drawn on the same y-axis o_O;
Woogleya at 2007-7-16 13:27:18 > top of Java-index,Java Essentials,New To Java...
# 4
yeah it changes where all 40 are drawn over time... but they are all just being drawn on top of eachother as neither X or Y in his code changes between drawing each flake..
Cowsrulea at 2007-7-16 13:27:18 > top of Java-index,Java Essentials,New To Java...
# 5
err yeah ignore what i just wrote
Cowsrulea at 2007-7-16 13:27:18 > top of Java-index,Java Essentials,New To Java...
# 6

they are all just being drawn on top of eachother

> as neither X or Y in his code changes between drawing

> each flake..

ah, for some reason I was reading this code as if it said Math.random():

num = ((int)Math.sqrt(num)) * 15 - (y * 5);

Woogleya at 2007-7-16 13:27:18 > top of Java-index,Java Essentials,New To Java...
# 7

You must learn Java GUI basics at http://java.sun.com/docs/books/tutorial/uiswing/index.html

and http://java.sun.com/docs/books/tutorial/2d/TOC.html

import java.awt.*;

import javax.swing.*;

import java.util.Random;

public class Blarg{

static Random rand;

static int num1, num2, num3, num4, x, y, sub1, sub2;

static APanel panel;

public static void main(String[] args) throws Exception{

JFrame frame = new JFrame("Blargity");

panel = new APanel();

panel.setBackground(Color.black);

panel.setForeground(Color.white);

frame.getContentPane().add(panel, BorderLayout.CENTER);

frame.setSize(250,250);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setResizable(false);

frame.setVisible(true);

rand = new Random();

num1 = 5; num2 = 0; num3 = 125; num4 = 125; // num3 and num4 not used

x = 0; y = 0;

sub1 = rand.nextInt(10); // not used

sub2 = 5;// not used

Thread snow = new Thread(){

public void run(){

while (x < 40){

panel.setParams(num1, num2); //currently, used parameters only

panel.repaint();

try{

Thread.sleep(500);

}

catch (InterruptedException e){

e.printStackTrace();

}

++x;

num1 = ((int)Math.sqrt(num1)) * 15 - (y * 5); // y is zero ?!?!

num2 += 3;

if (num2 > 250){

num2 = 0;

}

}

}

};

snow.start();

}

}

class APanel extends JPanel{

int num1, num2;

public APanel(){

num1 = num2 = -1;

}

public void setParams(int a, int b){

num1 = a;

num2 = b;

}

public void paintComponent(Graphics g){

super.paintComponent(g);

if (num1 > 0){ //drawing thread started

g.drawOval(num1, num2, 1, 1); // only one snow-flake !?!?

}

}

}

hiwaa at 2007-7-16 13:27:18 > top of Java-index,Java Essentials,New To Java...
# 8

Ok, I made a new APanel. I've added in some arrays, and it should work(for the most part). It compiles, but when I run Blarg it just shows a bunch of scattered snowflakes and tells me I have errors over and over again on the command line. The main error is some sort of arrayIndexOutOfBoundsException error or something like that. It says it's on line 50, which happens to be the line that draws the oval. The Blarg class is the same. Here's the new APanel:

import java.awt.*;

import javax.swing.*;

import java.util.Random;

public class APanel extends JPanel

{

Random rand = new Random();

//int num = 5;

int num2 = 0;

//int num3 = 125;

//int num4 = 125;

int x = 0;

//int y = 0;

//int sub = rand.nextInt(250);

//int sub2 = 5;

//int sub3 = rand.nextInt(250);

int[] snow = new int[40];

int[] snow2 = new int[40];

public APanel()

{

for(int i = 0;i < snow.length;i++)

snow[i] = rand.nextInt(250);

for(int i = 0;i < snow2.length;i++)

snow2[i] = rand.nextInt(250);

}

public void paint(Graphics g)

{

//-- The settings --//

Font myfont = new Font("arial",Font.BOLD|Font.ITALIC,20);

Color black = new Color(0,0,0);

Color white = new Color(250,250,250);

Color blue = new Color(0,0,255);

//-- The sky --//

g.setColor(black);

g.fillRect(0,0,250,250);

//-- The snow --//

g.setColor(white);

do

{

if (num2 > 200) // <-- There's no real reason for 200. It's just an approximation --//

{

for(int i = 0;i < snow.length;i++)

snow[i] = rand.nextInt(250);

for(int i = 0;i < snow2.length;i++)

snow2[i] = rand.nextInt(250);

}

g.drawOval(snow[x] + num2,snow2[x] + num2,1,1);

x++;

}while(x < 40);

num2+=3;

}

}

What is wrong with this.

TheGuy@YourWindowa at 2007-7-16 13:27:18 > top of Java-index,Java Essentials,New To Java...
# 9
I think you need to reset 'x' to 0 at the beginning of paint (before the do/while loop). It is going 0..39, then stops the loop when it hits 40. Then, 40 is out of bounds for the snow and snow2 array indexes. 'x' should probably be local to 'paint', anyway--it isn't used elsewhere.
MLRona at 2007-7-16 13:27:18 > top of Java-index,Java Essentials,New To Java...
# 10
Oh, I see what you're saying. That might be it.
TheGuy@YourWindowa at 2007-7-16 13:27:18 > top of Java-index,Java Essentials,New To Java...
# 11
Aha, that was it, but now I have snow that drifts away and no more ever comes. I guess I'll start working on that problem next. But hey, at least I got moving snow.
TheGuy@YourWindowa at 2007-7-16 13:27:18 > top of Java-index,Java Essentials,New To Java...
# 12
After num2 hits 200 and you reinitialize the snow/snow2 arrays, set num2 back to 0.
MLRona at 2007-7-16 13:27:18 > top of Java-index,Java Essentials,New To Java...
# 13

Ok, I've been working on it some more. Here's what I have so far:

import java.awt.*;

import javax.swing.*;

import java.util.Random;

public class APanel extends JPanel

{

Random rand = new Random();

//int num = 5;

int num2 = 0;

int num3 = 254;

int ycoord = 125;

int num4 = 254;

int yco2 = 124;

//int sub3 = rand.nextInt(250);

int[] snow = new int[40];

int[] snow2 = new int[40];

int[] starsx = new int[10];

int[] starsy = new int[10];

public APanel()

{

for(int i = 0;i < snow.length;i++)

snow[i] = rand.nextInt(250);

for(int i = 0;i < snow2.length;i++)

snow2[i] = rand.nextInt(250);

for(int i = 0;i < starsx.length;i++)

starsx[i] = rand.nextInt(250);

for(int i = 0;i < starsy.length;i++)

starsy[i] = rand.nextInt(40);

}

public void paint(Graphics g)

{

//-- The settings --//

Font myfont = new Font("arial",Font.BOLD|Font.ITALIC,20);

Color black = new Color(0,0,0);

Color white = new Color(250,250,250);

Color blue = new Color(0,0,255);

Color star = new Color(239,252,1);

//-- The variables --//

int x = 0;

int y = 0;

//-- The sky --//

do

{

Color thesky = new Color(0,0,num3);

g.setColor(thesky);

g.drawLine(0,ycoord,250,ycoord);

ycoord--;

if(num3 > 0)

{

num3-=2;

}

}while(ycoord >= 0);

//-- The stars --//

g.setColor(star);

do

{

g.drawString("*",starsx[y],starsy[y]);

y++;

}while(y < 10);

//-- The ground --//

do

{

Color ground = new Color(num4,num4,num4);

g.setColor(ground);

g.drawLine(0,yco2,250,yco2);

yco2++;

if(num4 > 0)

num4-=2;

}while(yco2 <= 250);

//-- The snow --//

g.setColor(white);

do

{

if ((snow[x] + num2 > 280) || (snow2[x] + num2 > 280))

{

num2 = 0;

}

g.drawOval(snow[x] + num2,snow2[x] + num2,1,1);

x++;

}while(x < 40);

num2+=3;

}

}

It looks pretty cool, but it makes the snowflakes turn into streaks. Why is this so?

TheGuy@YourWindowa at 2007-7-16 13:27:18 > top of Java-index,Java Essentials,New To Java...