Applet Life Cycle
import
java.applet.Applet;
import
java.awt.Graphics;
public class
AppletLifeCycleExample extends Applet
{
public void init()
{
super.init();
}
public void start()
{
super.start();
}
public void stop()
{
super.stop();
}
public void
paint(Graphics g)
{
super.paint(g);
}
public void destroy()
{
super.destroy();
}
}
Basic Java Applet
import
java.applet.Applet;
import
java.awt.Graphics;
public class
BasicAppletExample extends Applet
{
public void
paint(Graphics g)
{
g.drawString("This
is my First Applet",20,100);
}
}
Create Number counter in an
Applet using Thread
import
java.applet.Applet;
import
java.awt.Dimension;
import java.awt.Font;
import
java.awt.FontMetrics;
import
java.awt.Graphics;
public class
UsingRepaintAndThreadExample extends Applet implements Runnable
{
int counter;
Thread t;
public void init()
{
counter = 0;
t = new Thread(this);
t.start();
}
public void run()
{
Try
{
while(true)
{
repaint();
Thread.sleep(1000);
++counter;
}
}
catch(Exception e)
{
}
}
public void
paint(Graphics g)
{
g.setFont(new
Font("Serif",Font.Bold,30));
FontMetrics fm =
g.getFontMetrics();
String s =
"" + counter;
Dimension d =
getSize();
int x = d.width/2 -
fm.stringWidth(s)/2;
int y = d.height/2;
g.drawString(s,x,y);
}
}
Display Image in an Applet
import
java.applet.Applet;
import
java.awt.Graphics;
import java.awt.Image;
public class
DisplayImageExample extends Applet
{
Image img1,
img2;
public void init()
{
img1 =
getImage(getDocumentBase(), getParameter("Image1"));
img2 =
getImage(getDocumentBase(), getParameter("Image2"));
}
public void
paint(Graphics g)
{
g.drawImage(img1,
0,0,this);
g.drawImage(img2,
100,100,this);
}
}
Draw 3D Rectangle & Square
in Applet Window
import java.applet.Applet;
import java.awt.Color;
import
java.awt.Graphics;
public class
Draw3DRectanglesExample extends Applet
{
public void
paint(Graphics g)
{
g.setColor(Color.green);
g.draw3DRect(10,10,50,100,true);
g.draw3DRect(100,100,50,50,true);
g.setColor(Color.orange);
g.fill3DRect(10,150,50,100,true);
g.fill3DRect(100,200,50,50,true);
}
}
Draw Arc in Applet Window
import java.applet.Applet;
import java.awt.Color;
import
java.awt.Graphics;
public class
DrawArcExample extends Applet
{
public void
paint(Graphics g)
{
setForeground(Color.red);
g.drawArc(10,10,50,100,10,45);
g.fillArc(100,10,100,100,0,90);
}
}
No comments:
Post a Comment