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

Loops, Advanced Color Functions - Con't

This example demonstrates some of the more advanced graphics function of Java, showing how easy it is to use them. The five filled circles use user-defined colors, four of them are global variables (accessible for every function of the class), and the first circle uses a new color object that is defined locally right inside the function: g.setColor(new Color(255,255,0));. If you need a color only once, this is the shorter way, but if you need it more than once, you should define a variable for it. If it is used only in one function, you can define it locally in that function, otherwise globally. The pink blocks in the row at the bottom show the ease of making a darker or lighter variant of a given color with brighter() and darker(). The gradients are not just as easy to achieve, just copy and modify the code if you ever need something like it.

//Sourcecode

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

public class Project15 extends Applet
{
        Color myGreen = new Color(110,220,160);
        Color myRed = new Color(255,90,100);
        Color myYellow = new Color(240,240,180);
        Color myBlue = new Color(140,190,250);

        public void paint (Graphics g)
        {
                //paint background black
                g.setColor(Color.black);
                g.fillRect(0,0,size().width,size().height);

                g.setColor(new Color(255,255,0));
                g.fillOval(10,10,50,50);

                g.setColor(myGreen);
                g.fillOval(65,10,50,50);

                g.setColor(myRed);
                g.fillOval(120,10,50,50);

                g.setColor(myYellow);
                g.fillOval(175,10,50,50);

                g.setColor(myBlue);
                g.fillOval(230,10,50,50);

                float color;
                int i;
                int WIDTH=300;

                //the three color gradients: red, green, blue
                for(i=0; i<WIDTH; i++)
                {
                        color=i*(float)255/WIDTH;

                        g.setColor(new Color((int)color,0,0));
                        g.drawLine(i,70,i,90);

                        g.setColor(new Color(0,(int)color,0));
                        g.drawLine(i,95,i,115);

                        g.setColor(new Color(0,0,(int)color));
                        g.drawLine(i,120,i,140);
                }

                //the rainbow color spectrum
                for(i=0; i<WIDTH; i++)
                {
                        color=(float)i/WIDTH;
                        g.setColor(Color.getHSBColor(color,1.0f,1.0f));
                        g.drawLine(i,150,i,220);
                }

                Color myPink = new Color(240,100,150);

                g.setColor(myPink.darker().darker());
                g.fillRect(15,240,45,40);

                g.setColor(myPink.darker());
                g.fillRect(70,240,45,40);

                g.setColor(myPink);
                g.fillRect(125,240,45,40);

                g.setColor(myPink.brighter());
                g.fillRect(180,240,45,40);

                g.setColor(myPink.brighter().brighter());
                g.fillRect(235,240,45,40);
        }
}

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.