Tutorials : Java by Example : Section 5 :
Section Five Contents
Introducing Classes and Objects
Using the Vector Class

Objects and Classes

Introducing Classes and Objects

You might wonder why you should need a separate class at all for the last program we discussed, but the advantages might come clearer to you in a more complicated one. The next applet uses a thread to make the bubbles you produce by clicking grow. The applet calles the grow() method of the GrowingBubbles class from within the run() method, which does all there is to do to make the bubbles grow: increment its diameter. Making the objects of your programs also objects of a separate class, allows for more logical and simple programming, especially for big projects, as you will soon discover.

//Sourcecode

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

class GrowingBubble
{
   //instance variables
   int x, y, diameter, startDiameter, endDiameter;
   Color randomColor;
   Graphics g;

   //constructor
   public GrowingBubble()
   {
       int red=(int)(Math.random()*255);
       int green=(int)(Math.random()*255);
       int blue=(int)(Math.random()*255);

       //each bubble object has one random color
       randomColor=new Color(red, green, blue);

       //start diameter between 10 and 30
       startDiameter=(int)(Math.random()*20)+10;

       //end diameter between 50 and 150
       endDiameter=(int)(Math.random()*100)+50;

       diameter=startDiameter;
    }

    public void setPosition(int x, int y)
    {
       this.x=x;
       this.y=y;
    }

    public void drawBubble()
    {
       g.setColor(randomColor);
       g.fillOval(x-diameter/2,y-diameter/2,diameter,diameter);
    }

    public void grow()
    {
       //quickly grow, till end diameter is reached
        if(diameter<endDiameter)
             diameter+=4;
    }

    public void paint(Graphics gr)
    {
       g=gr;
       drawBubble();
    }
}

public class Project24 extends Applet implements Runnable
{
    Image Buffer;
    Graphics gBuffer;
    GrowingBubble myBubble;
    Thread runner;
    boolean clicked;

    public void init()
    {
       Buffer=createImage(size().width,size().height);
       gBuffer=Buffer.getGraphics();
    }

    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 15 ms here
            try {runner.sleep(15);}
            catch (Exception e) { }

            //bubble methods may only be accessed, if we have
            //created the object in the mouseDown method!
            if(clicked)
            {
                 myBubble.grow();
                 myBubble.paint(gBuffer);
            }

            repaint();
        }
   }

   public boolean mouseDown(Event evt,int x,int y)
   {
        myBubble = new GrowingBubble();

        myBubble.setPosition(x,y);

        clicked=true;

        return true;
   }

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

   public void paint(Graphics g)
   {
        gBuffer.setColor(Color.black);
        gBuffer.drawString("Click the applet to paint growing bubbles!", 30,20);

        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.