Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org

Related items

Steps to Java Internationalization (i18n)

Java Applets in Education

Java #6 I Wanna hold your hand - longer

Mouse Event Handling in Java 1.0

Java #5 I Wanna hold your hand

Java Applets #3

Java Applets #2

Java Applets #1

Java Applets #4

You are here: irt.org | Articles | Java | Java Applets #4 [ previous next ]

Published on: Saturday 25th April 1998 By: Tarique Sani

Introduction

This is the concluding article of this series, the much advertised Java FAQ to be maintained by myself and my mentor friend Rajesh Dhawan is already on line! There is not much code left to be explained in the KTN applet but it is the part which gives the applet its interactivity - so without further ado...

The Kalptaru Net Applet- the paint( ) method

As show in the previous article the paint() method is the place where the actual drawing on the applet canvas is done, here is how (hope you have your API reference at hand):

   public void paint(Graphics g){
      
      g.drawImage(img[index],0,0,w,h,this);
      
      if (selected) {
         g.setColor(new Color(0,0xff,0));
         g.drawLine(0,h-1,w,h-1);
                      
         for (int i=0;i<5;i++){
            int hh=5*(i+1);
            g.drawImage(arrow,10,h-20-hh/2,25,hh,this);
            try {Thread.sleep(50);
                }catch (InterruptedException e){
            }
         }
         g.setColor(BLUE);
         g.fillRect(0,h,w,h);
         g.setFont(new Font("TimesRoman",Font.BOLD,14));
         g.setColor(Color.white);
         g.drawString(tags[index],10,h+18);
      }else{
         g.drawImage(mirror[index],0,h,w,h,this);
      }
   }
  

The paint() method when called draws an Image referenced by the index plus if selected is true then shows the animated green arrow and the tag line associated with the Image, if selected is false then the mirror image is drawn. As you will see later selected is toggled by mouseEnter() and mouseExit() event handlers.

The start() stop() and destroy() Methods

These three small bits of code smoothen the running of the applet:

   Thread t=null;
   public void start(){
      if (t==null){
         t=new Thread(this);
         t.start();
      }else{
         t.resume();
      }
   }
   
   public void stop(){
      if (t!=null){
         t.suspend();
      }
   }

   public void destroy(){
      t.stop();
      t=null;
   }

The start() method starts the thread t which is created if found to be null else it resumes it, the stop() method suspends the thread and the destroy() method stops the threads and makes it null (thus doing the clean up job).

The run() Method

This is where the thread we created is controlled from:

   int index=0;
   int old=0;
   public void run(){
      while (true){
         try {
         Thread.sleep(delay);
         }catch (InterruptedException e){
          }
         index++;
         if (index>=nItems) index=0;
         repaint();
      }
   }

The run method first sleeps for the period specified in the delay parameter, then increments the index variable if the index is greater than the number of items on our list it is reset to zero, then the run method calls the repaint() thus in effect it redraws the applet after every n second gap, n being the delay we specified.

The mouse event handlers

There are three mouse events which are handled by the applet mouseEnter(), mouseExit() and mouseDown():

   public boolean mouseEnter(Event evt,int x,int y){
      selected=true;
      t.suspend();
      repaint();
      return true;
   }
   

   public boolean mouseExit(Event evt,int x,int y){
      selected=false;
      t.resume();
      repaint();
      return true;
   }

   public boolean mouseDown(Event evt,int x,int y){
      ac.showDocument(page[index],frm[index]);
      return true;
   }

The mouseEnter() toggles selected to true, suspends the thread (we don't want the display to change while the mouse is over the applet) and calls repaint(). mouseExit() is exactly opposite of mouseEnter(). Finally the mouseDown() shows the appropriate document in the proper frame that we supplied in the parameter list.

The last word

That is all that is needed to make the applet work, BUT there is a lot of flickering or flashing between repaints to avoid this we just over ride the update() method and make it call paint() directly without clearing the applet canvas first (clearing the applet canvas is the default behaviour of the update() method).

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

} // Note this extra curly matches the one which we put when we started the applet!

Complete Source Code

The complete source code for the complete KTN applet is available.

Related items

Steps to Java Internationalization (i18n)

Java Applets in Education

Java #6 I Wanna hold your hand - longer

Mouse Event Handling in Java 1.0

Java #5 I Wanna hold your hand

Java Applets #3

Java Applets #2

Java Applets #1

Feedback on 'Java Applets #4'

©2018 Martin Webb