Tutorials : Java by Example : Section 4 :
Section Four Contents
Loops, Advanced Color Functions
Random Colors and Arrays
Digital Clocks, HTML Page Parameters

Functions and Arrays

Random Colors and Arrays - Con't

This applet adds automatic refresh of our color applet once a second, so we add a getNewColors() method, which is called once in a second, to get 100 new colors for our array.

//Sourcecode

import java.awt.*;
import java.applet.*;

public class Project17 extends Applet implements Runnable
{
        Image Buffer;
        Graphics gBuffer;
        //declare an array variable to hold our Colors
        Color myColors[];
        Thread runner;

        public void init()
        {
               //create off-screen image we can draw to
               Buffer=createImage(size().width,size().height);
               gBuffer=Buffer.getGraphics();

               //arrays are real objects, we have to declare them with "new"
               myColors = new Color[100];
        }

        public void getNewColors()
        {
               for(int i=0;i<100;i++)
                {
                       int red=(int)(Math.random()*255);
                       int green=(int)(Math.random()*255);
                       int blue=(int)(Math.random()*255);

                       //fill our array with random colors
                       myColors[i]=new Color(red, green, blue);
                }
        }

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

        public void stop()
        {
             if (runner != null)
             {
                runner.stop();
                runner = null;
             }
        }

        public void run()
        {
                while(true)
                {
                           //halt the thread for 1000 ms here
                           try {runner.sleep(1000);}
                           catch (Exception e) { }

                           //get new colors every second
                           getNewColors();

                           repaint();
                }
        }

        public void update(Graphics g)
        {
                paint(g);
        }

        public void drawColors()
        {
                //draw tiles with random colors
                //we use two nested loops to do this:

                int i=0;

                for(int x=0;x<300;x+=30)
                        for(int y=0;y<300;y+=30)
                        {
                                int red=(int)(Math.random()*255);
                                int green=(int)(Math.random()*255);
                                int blue=(int)(Math.random()*255);

                                gBuffer.setColor(myColors[i]);
                                gBuffer.fillRect(x,y,60,60);

                                //increment the index of our array here
                                i++;
                        }
        }

        public void paint (Graphics g)
        {
                drawColors();

                //copy the buffer to the screen
                g.drawImage (Buffer,0,0, this);
        }
}

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.