Wednesday, November 5, 2014

Java basic applet pro



Get Applet parameter

import java.applet.Applet;
import java.awt.Graphics;
public class GetAppletParameterExample extends Applet
{
public void paint(Graphics g)
{
int x = 0;
int y = 0;
String msg = "";             
try
{
x = Integer.parseInt(getParameter("xPosition"));
}
catch(NumberFormatException ne)
{
msg = msg + "Invalid x Value";
}             
try
{
y = Integer.parseInt(getParameter("yPosition"));
}
catch(NumberFormatException ne)
{
msg = msg + "Invalid y Value";
}               
msg = getParameter("msg");               
g.drawString(msg, x, y);
  }
}

Get Applet's Document URL or Directory Base

import java.applet.Applet;
import java.awt.Graphics;
import java.net.URL;
public class GetDocumentBaseExample extends Applet
{
public void paint(Graphics g)
{
URL appletDocDir = getDocumentBase();
g.drawString(appletDocDir.toString(), 50, 50);               
   }
}

Get Background Color of an Applet Window

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
public class GetBackGroundColorExample extends Applet
{     
public void paint(Graphics g)
{
Color c = getBackground();
g.drawString(c.toString(),50,50);
  }
}

Get Foreground Color of an Applet Window

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
public class GetForgroundColorExample extends Applet
{
public void paint(Graphics g)
{
Color c = getForeground();
g.drawString(c.toString(),50,50);
    }
}

Hello World Applet

import java.applet.Applet;
import java.awt.Graphics; 
public class HelloWorldApplet extends Applet
{
public void paint(Graphics g)
{
g.drawString("Hello World", 50, 50);
  }
}

Load New HTML File Using Applet Context

import java.applet.Applet;
import java.applet.AppletContext;
import java.net.MalformedURLException;
import java.net.URL;
public class LoadNewHTMLFileExample extends Applet
{
public void start()
{
URL codeBase = getCodeBase();AppletContext context = getAppletContext();
try
{                   
URL url = new URL(codeBase + "AppletContextExample.html");
context.showDocument(url);
}
catch(MalformedURLException me)
{
   }
  }
}

Resize Applet Window

import java.applet.Applet;
import java.awt.Graphics;
public class ResizeAppletWindowExample extends Applet
{
public void paint(Graphics g)
{
resize(300,300);
g.drawString("Window has been resized to 300,300", 50, 50);
  }       
}

No comments:

Post a Comment