Event Handling using Anonymous Class in Applet

Author posted by Jitendra on Posted on under category Categories JAVA and tagged as Tags , , with Leave a comment on Event Handling using Anonymous Class in Applet

Source code to demonstrate the usage of anonymous class in Applet

We can handle the Event in Applet, AWT or Swing by following three different methods:

  • Inner Class
  • Anonymous class
  • this Pointer

In this example, i will show you that how to use Anonymous classes to handle the events in Applet.

package com.kunal.applet;

import java.applet.Applet;
import java.awt.Button;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AnonymouseButton extends Applet {

  TextField txtSource;
  Button btnToLOwer;

  public void init()
  {
     txtSource=new TextField(20);
     btnToLOwer=new Button("Lower Case");
     btnToLOwer.addActionListener(new ActionListener(){

       @Override
       public void actionPerformed(ActionEvent e) {

       String tmp=txtSource.getText();
       tmp=tmp.toLowerCase();
       txtSource.setText(tmp);

     }});

    add(txtSource);
    add(btnToLOwer);
  }
 }

Event Handling using Anonymous Class in Applet

Event Handling using Inner Class in Applet

Author posted by Jitendra on Posted on under category Categories JAVA and tagged as Tags , , with Leave a comment on Event Handling using Inner Class in Applet

Demonstrates the example for handling event using Inner Class in Java Applet

We can handle the Event in Applet, AWT or Swing by following three different methods:

  • Inner Class
  • Anonymous class
  • this Pointer

In this example, i will show you that how to use inner classes to handle the events in Applet.

package com.kunal.applet;

import java.applet.Applet;
import java.awt.Button;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class InerClassButton extends Applet {

 TextField txtSource;
 Button btnLower,btnUpper;
 String lowerText="lower case";

  public void init()
  {
     txtSource=new TextField(20);
     btnLower=new Button(lowerText);
     btnUpper=new Button("upper case");

     btnLower.addActionListener(new MyActionListener());
     btnUpper.addActionListener(new MyActionListener());

     add(txtSource);
     add(btnLower);
     add(btnUpper);
   }
   class MyActionListener implements ActionListener
   {
    @Override
     public void actionPerformed(ActionEvent evt) {
     String tmp=txtSource.getText();
     txtSource.setText(evt.getActionCommand()==lowerText?tmp.toLowerCase():tmp.toUpperCase());
     }
   }
}

Event Handling using Inner Class in Applet

Bouncing ball animation in Applet

Author posted by Jitendra on Posted on under category Categories JAVA and tagged as Tags , , with 6 Comments on Bouncing ball animation in Applet

Example to show Bouncing ball animation in Applet

In Previous article, we have seen the life cycle of Applet.

In this example, i will dem0nstrate that how we can animate ball and bounce it around the walls.

Please give your feedback on this article, so that i can improve it in future.

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;

class Ball
{
  int x,y,radius,dx,dy;
  Color BallColor;

 public Ball(int x,int y,int radius,int dx,int dy,Color bColor)
 {
   this.x=x;
   this.y=y;
   this.radius=radius;
   this.dx=dx;
   this.dy=dy;
   BallColor=bColor;
 }
}
public class BouncingbALL extends Applet implements Runnable{
Ball redBall,blackBall;
  public void init()
  {
    redBall=new Ball(80,80,20,2,4,Color.red);
    blackBall=new Ball(40,70,20,4,2,Color.black);
    Thread t=new Thread(this);
    t.start();
  }

 public void paint(Graphics g)
 {
    g.setColor(redBall.BallColor);
    g.fillOval(redBall.x, redBall.y, redBall.radius, redBall.radius);

    g.setColor(blackBall.BallColor);
    g.fillOval(blackBall.x, blackBall.y, blackBall.radius, blackBall.radius);
 }

  public void run()
  {
    while(true)
    {
     try
        {
           displacementOperation(redBall);
           displacementOperation(blackBall);
           Thread.sleep(20);
           repaint();
        }
    catch(Exception e){}
    }
  }
//This method checks the boundary condition of ball movement
public void displacementOperation(Ball ball)
 {
  if(ball.y >= 200 || ball.y <= 0)
  {
    ball.dy=-ball.dy;
  }

  if(ball.x >= 200 || ball.x <= 0)
  {
    ball.dx=-ball.dx;
  }

   ball.y=ball.y-ball.dy;
   ball.x=ball.x-ball.dx;
  }
}

Mouse handling in applets using this pointer method, Bouncing ball

Lifecycle of Applet

Author posted by Jitendra on Posted on under category Categories JAVA and tagged as Tags with Leave a comment on Lifecycle of Applet

Example which shows the lifecycle of applet

Applet is a piece of program in which runs on JAVA enabled browsers.

LifeCycle of Applet:

  • init()  is called only once  in lifecycle of applet during creation.
  • destroy() is called only once in applet lifecycle during disposal of applet.
  • whenever applet gots focus(active) paint() is called.
  • whenever applet is minimized stop() is called.
  • whenever applet is maximized start() & paint() is called.
  • start(), stop() & paint(Graphics g) can be called many times in applet lifecycle.

Example :


package com.shivasoft.applet;

import java.applet.Applet;
import java.awt.Graphics;

public class AppletLifecycle extends Applet
{
  public void init()
  {
      System.out.println("Init State");
  }

  public void start()
  {
      System.out.println("Start State");
  }

  public void paint(Graphics g)
  {
      System.out.println("Paint State");
   }

   public void stop()
   {
        System.out.println("Stop State");
   }

   public void destroy()
   {
        System.out.println("Destroy State");
    }
}