0

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;

            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);
            }   
        }   
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Toothl
  • 1
  • 1
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT components in favor of Swing. – Andrew Thompson Apr 15 '17 at 22:47
  • Ya its my highschool teacher has to be Applet – Toothl Apr 16 '17 at 01:51
  • *"its my highschool teacher"* What's their reaction to the linked article? What's the answer to my 2nd question? These are not rhetorical questions, I'll see answers to them before offering help. BTW a point I forgot to add, See [Java Plugin support deprecated](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) and [Moving to a Plugin-Free Web](https://blogs.oracle.com/java-platform-group/entry/moving_to_a_plugin_free). – Andrew Thompson Apr 16 '17 at 13:43

0 Answers0