HOW TO CREATE AND RUN APPLET PROGRAMS
Step 1 - Create Source code ( . java ) file
Step 2: Write the following code..
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="testapp" width=450 height=350 > // Define class file to be load and
applet window size
<param name="p1" value="Hello! SABAB" > // Passing parameter to applet
</applet>
*/
public class testapp extends Applet implements MouseMotionListener
{
int x[]={213,280,148,300,180,213}; // Setting X coordinates of points to draw Polygon
int y[]={107,240,156,156,246,107}; // Setting Y coordinates of points to draw Polygon
Font f;
Color clr;
String str;
public void init()
{
str=getParameter("p1"); // Getting parameter
f=new Font("Mistral",Font.BOLD,48) ; // Define font to be used
clr=new Color(51,164,247); // Define custom color
setBackground(clr); // Setting applet background color
setForeground(new Color(64,0,128)); // Setting applet foreground color
addMouseMotionListener(this);
}
public void paint(Graphics g)
{
g.setFont(f);
g.drawString("Welcome to Microtek.",40,50);
g.drawString(str,40,300);
g.drawLine(40,65,400,65);
g.drawPolygon(x,y,6); // Draw polygon with 6 points
}
public void mouseMoved(MouseEvent me)
{
/*****Display Mouse Position on status bar **********/
showStatus("Mouse Pointer at : "+me.getX()+" , "+me.getY()); // Getting pointer positions
}
public void mouseDragged(MouseEvent me) { }
}
Step 2: Write the following code..
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="testapp" width=450 height=350 > // Define class file to be load and
applet window size
<param name="p1" value="Hello! SABAB" > // Passing parameter to applet
</applet>
*/
public class testapp extends Applet implements MouseMotionListener
{
int x[]={213,280,148,300,180,213}; // Setting X coordinates of points to draw Polygon
int y[]={107,240,156,156,246,107}; // Setting Y coordinates of points to draw Polygon
Font f;
Color clr;
String str;
public void init()
{
str=getParameter("p1"); // Getting parameter
f=new Font("Mistral",Font.BOLD,48) ; // Define font to be used
clr=new Color(51,164,247); // Define custom color
setBackground(clr); // Setting applet background color
setForeground(new Color(64,0,128)); // Setting applet foreground color
addMouseMotionListener(this);
}
public void paint(Graphics g)
{
g.setFont(f);
g.drawString("Welcome to Microtek.",40,50);
g.drawString(str,40,300);
g.drawLine(40,65,400,65);
g.drawPolygon(x,y,6); // Draw polygon with 6 points
}
public void mouseMoved(MouseEvent me)
{
/*****Display Mouse Position on status bar **********/
showStatus("Mouse Pointer at : "+me.getX()+" , "+me.getY()); // Getting pointer positions
}
public void mouseDragged(MouseEvent me) { }
}
Step 3: Save the file named "testapp.java" and Compile the file
c:\temp> javac testapp.java
Step 4: Test the applet using appletviewer tools.
c:\temp\> appletviewer testapp.java
Output:
Excellent Mr. Sabab !!
ReplyDeleteKeep it up...