Posts

Showing posts with the label applet

Using Applet dimension to print center aligned text Example

/* Using Applet dimension to print center aligned text Example This Java example shows how to print text in center of an applet window using Dimension class. */   import java.applet.Applet ; import java.awt.Dimension ; import java.awt.Font ; import java.awt.FontMetrics ; import java.awt.Graphics ;   /* <applet code = "AppletDimensionExample" width = 500 height = 300> </applet> */   public class AppletDimensionExample extends Applet {   public void paint ( Graphics g ) { int x,y ; String s = "Hello World" ;   //get applet size using getSize method Dimension d = getSize ( ) ; Font f = new Font ( "Arial" , Font . BOLD , 24 ) ; g. setFont ( f ) ;   //determine x and y coordinates FontMetrics fm = g. getFontMetrics ( ) ; x = d. width / 2 - fm. stringWidth ( s ) / 2 ; y = d. height / 2 - fm. getHeight ( ) ;   //print string at specified location using drawStr...

Get Applet parameter Example

/* Get Applet parameter Example This java example shows how get the parameter passed to an applet using getParameter() method of Java Applet class. */   import java.applet.Applet ; import java.awt.Graphics ;   /*  * To pass a parameter to an Applet use <param> HTML tag.  * Specify name and value attribute to pass a parameter to an applet like  * given below.  *  * <param name="paramName" value="paramValue"/>  *  * You can pass multiple parameters for an Applet using multiple  * <param> HTML tag.  */   /* <applet code="GetAppletParameterExample" width=200 height=200> <param name="msg" value="This is a parameter example program"/> <param name="xPosition" value="50"/> <param name="yPosition" value="50"/> </applet> */   public class GetAppletParameterExample extends Applet {   public void paint ( Graphics g )...

Basic Java Applet Example

/* Basic Java Applet Example  This Java example shows how to create a basic applet using Java Applet class.  */  import java.applet.Applet;  import java.awt.Graphics;  /*  <applet code = "BasicAppletExample" width = 200 height = 200>  </applet>  */  public class BasicAppletExample extends Applet{  public void paint(Graphics g){  //write text using drawString method of Graphics class  g.drawString("This is my First Applet",20,100);  }  }

Java Applets Life cycle

/* Applet Life Cycle Example  This java example explains the life cycle of Java applet.  */   import java.applet.Applet;  import java.awt.Graphics; /*  *  * Applet can either run by browser or appletviewer application.  * Define <applet> tag within comments as given below to speed up  * the testing.  */  /*  <applet code="AppletLifeCycleExample" width=100 height=100>  </applet>  */  public class AppletLifeCycleExample extends Applet{ /*  * init method is called first.  * It is used to initialize variables and called only once.  */  public void init() {  super.init();  } /*  * start method is the second method to be called. start method is  * called every time the applet has been stopped.  */  public void start() {  super.start();  }  /*  * stop method is called when the the user navigates away from ...