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 following applet does just that:

//Sourcecode

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

public class Project5B extends Applet
{
        int mouseX, mouseY;
        boolean clicked=false;

        public boolean mouseDown(Event evt,int x,int y)
        {
                clicked=true;

                mouseX=x;
                mouseY=y;

                repaint();

                return true;
        }

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

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

                //we only draw this if the mouse has been clicked once
                if(clicked)
                {
                        if(mouseX < size().width/3)
                                g.drawString("You clicked in the left 1/3!",60,100);

                        else if(mouseX > size().width-size().width/3)
                                g.drawString("You clicked in the right 1/3!",60,100);

                        else
                                g.drawString("You clicked in the middle 1/3!",60,100);
                }
        }
}

In some cases you will want to check for a number of conditions, where a series of if/else statements would be suitable. A more elegant and shorter way is the switch-statement. It tests for a variable (which must be an integer type) to match one of several values. If any of them matches, the body of that statement is executed:

switch(test-expression)
{
   case 0:   body1
      break;
   case 1:   body2
      break;
   case 2:   body3
      break;
   case 3:   body4
      break;
   default:  body5
}
So, if the test-expression equals 0, body1 is executed, if it equals 3, body 4 is executed and if neither of the first four match, the default statement is executed, if any.

The break after each statement takes care of the whole switch-statement being left, then. If none of the single statements matches the value of the test variable, the body of the default-statement is executed. This is mandatory though, you don't have to provide a default branch.

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.