Showing posts with label applet. Show all posts
Showing posts with label applet. Show all posts

Sunday, November 27, 2011

Using Applet dimension to print center aligned text Example


  1. /*
  2. Using Applet dimension to print center aligned text Example
  3. This Java example shows how to print text in center of an applet window using
  4. Dimension class.
  5. */
  6.  
  7. import java.applet.Applet;
  8. import java.awt.Dimension;
  9. import java.awt.Font;
  10. import java.awt.FontMetrics;
  11. import java.awt.Graphics;
  12.  
  13. /*
  14. <applet code = "AppletDimensionExample" width = 500 height = 300>
  15. </applet>
  16. */
  17.  
  18. public class AppletDimensionExample extends Applet{
  19.  
  20. public void paint(Graphics g){
  21. int x,y;
  22. String s = "Hello World";
  23.  
  24. //get applet size using getSize method
  25. Dimension d = getSize();
  26. Font f = new Font("Arial",Font.BOLD,24);
  27. g.setFont(f);
  28.  
  29. //determine x and y coordinates
  30. FontMetrics fm = g.getFontMetrics();
  31. x = d.width/2 - fm.stringWidth(s)/2;
  32. y = d.height/2 - fm.getHeight();
  33.  
  34. //print string at specified location using drawString method
  35. g.drawString(s,x,y);
  36. }
  37. }

Get Applet parameter Example


  1. /*
  2. Get Applet parameter Example
  3. This java example shows how get the parameter passed to an applet using
  4. getParameter() method of Java Applet class.
  5. */
  6.  
  7. import java.applet.Applet;
  8. import java.awt.Graphics;
  9.  
  10. /*
  11.  * To pass a parameter to an Applet use <param> HTML tag.
  12.  * Specify name and value attribute to pass a parameter to an applet like
  13.  * given below.
  14.  *
  15.  * <param name="paramName" value="paramValue"/>
  16.  *
  17.  * You can pass multiple parameters for an Applet using multiple
  18.  * <param> HTML tag.
  19.  */
  20.  
  21. /*
  22. <applet code="GetAppletParameterExample" width=200 height=200>
  23. <param name="msg" value="This is a parameter example program"/>
  24. <param name="xPosition" value="50"/>
  25. <param name="yPosition" value="50"/>
  26. </applet>
  27. */
  28.  
  29. public class GetAppletParameterExample extends Applet{
  30.  
  31. public void paint(Graphics g){
  32.  
  33. int x = 0;
  34. int y = 0;
  35. String msg = "";
  36.  
  37. try{
  38. x = Integer.parseInt(getParameter("xPosition"));
  39. }
  40. catch(NumberFormatException ne){
  41. msg = msg + "Invalid x Value";
  42. }
  43.  
  44. try{
  45. y = Integer.parseInt(getParameter("yPosition"));
  46. }
  47. catch(NumberFormatException ne){
  48. msg = msg + "Invalid y Value";
  49. }
  50.  
  51. msg = getParameter("msg");
  52.  
  53. g.drawString(msg, x, y);
  54. }
  55. }

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
 * html page containing the applet.
 */
 public void stop() {
 super.stop();
 }
 /* paint method is called every time applet has to redraw its
 * output.
 */
 public void paint(Graphics g) {
 super.paint(g);
 }
 /*
 * destroy method is called when browser completely removes
 * the applet from memeory. It should free any resources initialized
 * during the init method.
 */
 public void destroy() {
 super.destroy();
 }
 }



How to find the most appropriate Keywords?

  🔍 Step 1: Understand Your Business and Audience Define your products, services, or content . Identify your target audien...