Thursday, December 18, 2014

Getting Applet Parameters and Event Handling and Using the super keyword in java pro



Getting Applet Parameters

import java.applet.*;
import java.awt.*;
public class CheckerApplet extends Applet
{
   int squareSize = 50;
   public void init () {}
   private void parseSquareSize (String param) {}
   private Color parseColor (String param) {}
   public void paint (Graphics g) {}
}

Event Handling

import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.applet.Applet;
import java.awt.Graphics;
public class ExampleEventHandling extends Applet implements MouseListener             
{
    StringBuffer strBuffer;
    public void init() 
   {
   addMouseListener(this);
   strBuffer = new StringBuffer();
        addItem("initializing the apple ");
    }
              public void start() 
             {
        addItem("starting the applet ");
    }
    public void stop() 
   {
        addItem("stopping the applet ");
    }
              public void destroy() 
              {
        addItem("unloading the applet");
    }
    void addItem(String word) 
   {
        System.out.println(word);
        strBuffer.append(word);
        repaint();
    }
    public void paint(Graphics g) 
  {
           g.drawRect(0, 0, 
           getWidth() - 1,
           getHeight() - 1);
 
        g.drawString(strBuffer.toString(), 10, 20);
    } 
    public void mouseEntered(MouseEvent event) 
   {
    }
    public void mouseExited(MouseEvent event) 
   {
    }
    public void mousePressed(MouseEvent event) 
    {
    }
    public void mouseReleased(MouseEvent event) 
   {
    }
    public void mouseClicked(MouseEvent event) 
   {
   addItem("mouse clicked! ");
    }
}

Displaying Images

import java.applet.*;
import java.awt.*;
import java.net.*;
public class ImageDemo extends Applet
{
  private Image image;
  private AppletContext context;
  public void init()
  {
      context = this.getAppletContext();
      String imageURL = this.getParameter("image");
      if(imageURL == null)
      {
         imageURL = "java.jpg";
      }
      try
      {
         URL url = new URL(this.getDocumentBase(), imageURL);
         image = context.getImage(url);
      }
       catch(MalformedURLException e)
      {
         e.printStackTrace();
         context.showStatus("Could not load image!");
      }
   }
   public void paint(Graphics g)
   {
      context.showStatus("Displaying image");
      g.drawImage(image, 0, 0, 200, 84, null);
      g.drawString("www.javalicense.com", 35, 100);
   }  
}
Create a package
package animals;
public class MammalInt implements Animal
{
   public void eat()
{
      System.out.println("Mammal eats");
   }
   public void travel()
  {
      System.out.println("Mammal travels");
   } 
   public int noOfLegs()
  {
      return 0;
   }
   public static void main(String args[])
   {
      MammalInt m = new MammalInt();
      m.eat();
      m.travel();
   }
} 

Using the super keyword

class Animal
{
   public void move()
  {
      System.out.println("Animals can move");
   }
}
class Dog extends Animal
{
   public void move()
  {
      super.move();
      System.out.println("Dogs can walk and run");
   }
}
public class TestDog
{
 
   public static void main(String args[])
  {
      Animal b = new Dog();
      b.move(); 
   }
}

No comments:

Post a Comment