samedi 15 avril 2017

Java: making random ovals with random x intverals and then proceed to fall down the screen

the code i have written is for a school project, i am still very new to java so sorry for the ugly coding. im trying to make 8-9 random ovals appear at y = 0 and at random x intervals (fixed values) But my problem is either it will make the random x values and place the ovals there but will not add to the y value, or they will fall down the screen but it will keep changing the x value when i want it to stay at a fixed number.

import java.applet.Applet;

import java.awt.*; import java.util.Random;

public class animation extends Applet implements Runnable { Thread runner;

int xPos;
int y = 0;
int stop = 0;
Random rng = new Random();

public void start()
{
    if(runner == null)
    {
        runner = new Thread(this);
        runner.start();
    }
}

public void run()
{
    while(stop != 8)
    {
        for(int i = 0; i < 9; i++)
        {
            xPos = rng.nextInt(300);
            stop++;
        }
    }

    while(true)
    {
        y += 10;`enter code here`

        repaint();
        try{runner.sleep(250);}
        catch (InterruptedException e) { } 
    }
}   

public void paint(Graphics gr)
{   
    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 3; j++)
        {   
            gr.fillOval((xPos), y, 8, 8);
        }   
    }   
}

}




Aucun commentaire:

Enregistrer un commentaire