|
|
Java Applets #3
You are here: irt.org | Articles | Java | Java Applets #3 Published on: Saturday 4th April 1998 By: Tarique Sani
Introductionblackbug@bom4.vsnl.net.in wrote "I went through your article -The lifecycle of an applet- with a great deal of interest in the hope of finding the promised free applet" <snip>. Though I don't recall promising any such thing I would hate to disappoint an old faithful like Black Bug! So download your free tsMenu applet now or first see what you are going to get. In this article we will look into the details of the KTN applet, of all the applets which I have written so far the KTN applet - one of my earliest - remains my favourite despite the fact that it is not the most sophisticated looking of the lot. Here are some of the features and intricacies which make it programmatically so appealing:
The Kalptaru Net Applet-Setting the variablesHaving sung praises for my own creation lets dive into the code to find out if all that is really true, if you are a newbie to Java, I suggest that you first read the previous two articles of the series Java Applets #1 and Java Applets #2, even if you are not a complete newbie it is strongly recommended that you take out your Java API reference. The Applet was so named because it was first put up at the Kalptaru Net, a website managed by me for a friend's ISP outfit, The first chunk of code reads as follows:
The applet begins with importing all the classes that will be needed, and then the variables which will be needed later in the program are declared! This business of declaring and knowing what variables you are going to use will seldom be known to you before hand, the most common method is that you keep adding the variables here as and when you feel the need, rather than scatter you variables around the program! The start of the init() MethodThe init() method is is the longest piece of code in the whole applet lets take it bit by bit:
This part of the code is very simple - it just sets the color of the background, and draws the welcome string and a line in the applet window. The next bit is tricky! Getting the Applet Parameters
What this seemingly complicated code is doing is nothing but getting all the parameters defined in the applet tag into the applet environment! This is achieved by creating a for loop and feeding each of the item <param> to a StringTokenizer. What the StringTokenizer does is breaks up the item parameter string in to different parts depending on the presence of '+' symbol in the string. Lets look at a sample parameter for better understanding <param name=item0 value="Welcome+main.html+Welcome from Dr. Tarique Sani+splash"> Welcome goes into the items array main.html goes into the page array (after due conversion) Welcome from Dr. Tarique Sani goes into the tags array and the last token splash goes into the frm array - if this last thing is missing then the frm (frame) is set to _top. This code is finally wrapped inside a try - catch clause so that any error occuring would not bring the applet down! The last line here gets the delay parameter converts the string into an integer and then multiplies it by 1000 to get the delay in milliseconds - Java understands only milliseconds! Visual Effects Part I
This is the part where the visual effects begin - the first part creates an image object which contains a gray rectangle and a green triangle over it! In the next part the Strings in the items() array are converted to images with progressively darker shade of gray in each character - this gives a beautiful graduated look to the string (call it a light effect if you want to!) Please note BLUE and blue here are not actual colors but names of the variables which hold color - A hang over from the old program on the basis of which this applet was built - not a good practice but it happens when you have used the variable often enough changing names midway gives rise to wierd results and you give up trying to rename them! Visual Effects Part II
This is the last piece of code in the init() method (Whew!) The image of string which was created in the previous part of the code is now taken and its pixels put into an array using the PixelGrabber. These pixels are then manipulated using a small routine - processIt()-to make them a bit darker than the original. After they have been darkened they are placed in another array but now at the mirror image position! Lastly the pixels in the array are used to create a new Image using the createImage() along with MemoryImageSource(). This brings us to the end of this episode of the Kalptaru Net applet in the next and concluding part we will add interactivity to the Applet! Which Java IDE'sI have been often asked what IDE or development tool is best for developing Java Applets for the Web. For most my answer is Notepad! This is specially true if you are learning Java or intend to write only Applets. If you don't believe me try and replicate ktn applet in any IDE. However if you are developing Java applications or you are programming for an Intra Net it makes a great deal of sense to use an IDE tool - simply because the supplied components (Called Beans) do most of the work for you and these days almost every Java IDE comes with a whole gamut right from picture buttons to database connectivity tools! The Leaders in the pack include - Visual Cafe from Symantec, Visual J++ form Microsoft and the late comer but the best liked by me JBuilder from Borland. View the profile on Tarique Sani and the list of other Articles by Tarique Sani. |
-- div -->
|