Hello. I am just starting java and I was woundering if anyone could help me with 2 things. I was woundering how to put my program into a loop so it just keeps going and how to make the circles transparent. Here is the program. Thank you very much if you help me.

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


public class BackGround extends Applet {

    public void init() {
    }

    public void paint(Graphics g) 
    {
        Random rnd = new Random();

        int randomR1 = rnd.nextInt(220) + 15;
        int randomR2 = rnd.nextInt(220) + 15;
        int randomR3 = rnd.nextInt(220) + 15;

        int randomG1 = rnd.nextInt(220) + 15;
        int randomG2 = rnd.nextInt(220) + 15;
        int randomG3 = rnd.nextInt(220) + 15;   

        int randomB1 = rnd.nextInt(220) + 15;
        int randomB2 = rnd.nextInt(220) + 15;
        int randomB3 = rnd.nextInt(220) + 15;

        int randomX1 = rnd.nextInt(450) + 25;
        int randomX2 = rnd.nextInt(450) + 25;
        int randomX3 = rnd.nextInt(450) + 25;

        int randomY1 = rnd.nextInt(450) + 25;
        int randomY2 = rnd.nextInt(450) + 25;
        int randomY3 = rnd.nextInt(450) + 25;


        Color randomColor1 = new Color( randomR1, randomG1, randomB1);
        Color randomColor2 = new Color( randomR2, randomG2, randomB2);
        Color randomColor3 = new Color( randomR3, randomG3, randomB3);

        g.setColor(randomColor1);
        g.fillOval(randomX1, randomY1, 80, 80);

        g.setColor(randomColor2);
        g.fillOval(randomX2, randomY1, 80, 80);

        g.setColor(randomColor3);
        g.fillOval(randomX3, randomY3, 80, 80);
    }
}

Edited 3 Years Ago by Nick Evan: Fixed formatting

write public class BackGround extends Applet implements Runnable { You must overwrite function run()
Make never ending loop inside run()
Introduce varible Thread thread; In function init() started thread
Declare Random rnd = new Random(); and randomZZ varibles without paint(Graphics g) body
Assignment of new values of randomZZ You can made in run() body.

public void run() {
        while (true) {

           // randomR1 = 
          ///   all, what You need

            try {
                Thread.sleep(sleepTime);
            } catch (InterruptedException ex) {
                //
            }
            repaint();
        }
    }
public void init() {
       if (thread == null) {
           thread = new Thread(this);
           thread.start();
        }
    }
public void paint(Graphics g) {
        g.setColor(randomColor1);
        g.fillOval(randomX1, randomY1, 80, 80);

        g.setColor(randomColor2);
        g.fillOval(randomX2, randomY2, 80, 80);

        g.setColor(randomColor3);
        g.fillOval(randomX3, randomY3, 80, 80);
    }

search sources...
quuba

I am sorry I do not understand =,(

is there any chance you could explain it in a diffrent way

I don't think anything in his code does the transparent part he was just trying to explain how to do the redrawing (like frames but using a timer to delay in between frames or in this case calls to repaint). Being a beginner doing the transparency might not be worth your time, you probably want to focus on figuring out how to implement the threading described above if you need to get the balls moving. If you decide to do the transparency still here's a site that'll probably help out, just barrel through any parts you don't understand some of the terminology is pretty technical. http://www.informit.com/articles/article.aspx?p=26349&seqNum=5

As far as what quuba was trying to say, you need to add the first part he talks about so that your class implements Runnable. The exact details of what this means isn't too important, but basically you need to then create a method called run that will be called whenever something calls start on an instance of your class, this thread will remain running in an infinite loop, so it'll never close. Inside the loop you want it to keep redrawing all your graphics to the screen.

What he wrote should work for you, try putting the methods he gave you into your code and then repost what your results are, if you get an error just copy that and put it in here along with your source code. Just remember to keep a backup of your semi-working version so you don't lose any work.

This article has been dead for over six months. Start a new discussion instead.