A short introduction to Java programming

by Sebastian Bologescu

Hi to all of you!

Whatever you do ... intranet applications, commercial presentation or only your home page, it is very important to have an impeccable presentation, that means some special buttons, help, dynamic pictures, some animations and sounds. You can do all of this and much more with Java applets and/or JavaScripts

My name is Sebastian Bologescu and I think it is a good idea to give you some useful hints which can make you become interested in Java programming.

First of all I'll introduce myself:
I am 24 years old, and I just finished Computer Science Dept. of University "Poitehnica" here in Bucharest. Between 15 nov 1997 and 1 June 1999, I worked for Media Pro International, the largest television company from Romania. Currenly I am working with Techdomain, as a member of their IT consulting group. Using my knowledge I implemented a system for news acquisition from four news agencies via sattelite. But this will be another subject about ASP programming.
If you want to know more about me, please look here for my CV.
If you are a company and you want to develop some in-house project don't hesitate to contact Techdomain team!!!

A short introduction to Java programming

Whatever you do ... intranet applications, commercial presentation or only your home page, it is very important to have an impeccable presentation, that means some special buttons, help, dynamic pictures, some animations and sounds. You can do all of this and much more with Java applets and/or JavaScripts.


Java applets: a small piece of executable code (compiled)which is downloaded from HTTP server to your web client and is executed through your Virtual Java Machine.

Virtual Java Machine: a software emulated processor which knows to execute your applet in context with your browser.

Java Scripts: interpretable code (script) which doesn't need to be compiled. This script is interpreted by the browser during run-time.

Java applications: something like applets, but is browser independent (different window, different menus etc.).

Methods: this is the name for functions from a class - it is like behaviour of an object

Members: the variables from a class or method - is like a properties of an object

What do I need for Java programming?

First off all you need a computer! After that you must buy a Java Compiler. For this I recommend you Visual J++ from Microsoft or Symantec Cafe from Symantec (very optimized code).

Java programming

The code is very similar to C++. The operators and the statements are all the same. The difference is that, in Java you don't declare prototypes. The source file has *.java extension and executable file has *.class extension.
From my view, the best way to learn Java is to take a concrete problem to resolve.

Let's go to do this applet:   

As you see, you can draw two types of elements (strings) selecting it with a button. It is a simple and useless applet but it is important in educational sense.

This is the sample code:

For Java compiler I used Symantec Cafe 1.5.

Java is an object oriented language, that means the secret in doing good code is to understand, and to know the structure of classes. For that I suggest to look in Symantec Cafe at browse tag to see this structure, or consult java documentation from any java compiler. I can't describe all this structure from Symantec Cafe here becouse I'll need all the harddisk space from swynk.com!!! :)

First thing we must do is to declare the libraries we use in our applet.

In this case we have to include java.awt.*; and java.applet.Applet. That means we will make accessible for us all classes from java.awt (used for layout manager to handle events and so on ...) and Applet class. Applet is the class from where we derive our own class (in this case, CFirst).
After that we will define our new class (CFirst). That class is derived from Applet, that means all methods and members of Applet are available to us. The new class declaration is:

public class CFirst extends Applet


// class personalize (override methods, add new members etc...)


All we need to do right now, is to personalize some of that methods overriding them.
I will declare some members (variables):

int xb=0, yb=0; - integer variables needed to store the last mouse cursor position in rectangular coordinates

String txt=""; - string variable used to set the text we display (in this case 'Computer' or 'Server')

Button b1, b2; - two objects from Button class.

We need to put two buttons so we'll override the init() method. Why init() method? Because that method is the entry point in our programm when applet is executed (initialized).
The declaration for init() member is:

public void init() - is a public method (can be accesed from outside) and don't have return or parameters (very similar to C++).

Here we will initialize the b1 and b2 object. We do this using Button(String label )  - the constructor for class Button. Label parameter define the label for the button and is of string type.
After that we display our new defined buttons (b1, b2) with ...

add(b1); - add first button, something like this .... and ...

add(b2); - add first button, something like this .... but as you see when we click on it nothing happens!!!

So we are finished with initialization part.

Now we need to see when a button is clicked. For this we have a lot of methods in class Applet which can handle this kind of events:

public boolean action(Event e, Object arg) - this method return a boolean value and have two parameters.

Event e - the event which happens

Object arg - the arguments associated with event.

When an event appears, the Windows send a message to our applet. In this case the method action is called. Depending on the button pressed, we will set the txt variable with the coresponding string.
In the same way we'll use "mouse drag" and "mouse up" events which are other methods treating mouse events.
At mouse drag, we need to move the text on the screen. We override the last position with background color (lightGray) and draw it on new position with blue.
At "mouse up" event, I reset the xb and yb coordinates, so that the text will not be erased on next mouse drag.
To put the applet in your .html or .asp page you need to write the following tag:

<applet code="CFirst" name="MyApplet">


That's all folks! ...

... But OOOPS, we have some problems!!! Try to move or minimize-maximize the window with your applet. As you can see all you draw in applet's window dissapear. This is normal, because when Windows refresh our window reset it, so we loose all. To preserve the text we need to redraw it after refresh.

This article was originally published on Nov 9, 1998
Page 1 of 2

Thanks for your registration, follow us on our social networks to keep up-to-date