Tutorials : Java by Example : Section 1 :
Section One Contents
Basic Graphics Functions
Simple Methods And Basic Data Types
If, Else And Switch: Basic Control Structures

Java Basics

If, Else And Switch: Basic Control Structures - Con't

The example below demonstrates this. If the number of clicks inside the applet matches zero, one, two or ten, you get an appropriate message, also for any other case:

//Sourcecode

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

public class Project5C extends Applet
{
        //we can initialize this variable with 0, which is not neccessary though,
        //booleans are initialized with false and numbers with 0 by default
        int clickNumber=0;

        public boolean mouseDown(Event evt,int x,int y)
        {
                //increase by 1, you could also write
                //clickNumber = clickNumber+1, but why should you :)
                clickNumber++;

                repaint();

                return true;
        }

        public void paint(Graphics g)
        {
                g.setFont(new Font("Helvetica",Font.PLAIN,15));

                g.drawString("Click inside the applet!", 80,30);

                g.drawString("Clicks: "+clickNumber, 120,50);

                //our switch statement, which tests the clickNumber variable
                switch(clickNumber)
                {
                        case 0:
                         g.drawString("You haven't clicked yet!",70,100);
                         break;

                        case 1:
                         g.drawString("You clicked once!",90,100);
                         break;

                        case 2:
                         g.drawString("You clicked 2 times!",80,100);
                         break;

                        case 10:
                         g.drawString("You clicked 10 times!",90,100);
                         break;

                        //the default statement is mandatory, it can be ommitted
                        default:
                        {
                                g.drawString("You clicked more than 3 times,",50,80);
                                g.drawString("but not 10 times!",90,100);
                        }
                }
        }
}

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.