java applet example programs download

java applet example programs download

java applet example programs download

/**
* @(#)Prog_01.java
*
*
* @author Adarsh Patel
* @version 1.00 2008/11/6
*
*        Change background color frequently
*/

/*<APPLET CODE=”Prog_01″ WIDTH=700 HEIGHT=500>
*</APPLET>
*/

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.EventListener;

public class Prog_01 extends Applet implements Runnable
{
Thread t;
boolean flag=true;

public Prog_01()
{
t = new Thread(this);
t.setPriority(9);
t.start();
}

public void run()
{
while(true)
{
flag = !flag;
try
{
repaint();
//Thread.sleep(2000);
t.sleep(1000);
}
catch(InterruptedException ie)    {}

}
}

public void paint(Graphics g)
{
if(flag)
{

setBackground(Color.red);
}
else
{
setBackground(Color.blue);
}
repaint();

}

}

/**
* @(#)Prog_06.java
*
*
* @author Adarsh Patel
* @version 1.00 2008/11/6
*
*        Draw two lines and rotate in clock wise and anticlock wise
*/

/*<APPLET CODE=”Prog_06″ WIDTH=500 HEIGHT=500>
*</APPLET>
*/

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.EventListener;

public class Prog_06 extends Applet implements ActionListener
{
int curx=0,cury=0;
Button b1,b2,b3;
int x1,y1,x2,y2,x3,y3,x4,y4;
boolean flag_reverse,flag;
int cx,cy,r;
int r1,r2,r3,PI=3;

public void init()
{
//true mens clock wise and 0 means anticlock
flag =true;

flag_reverse=false;

b1 = new Button(“On”);
b2 = new Button(“Off”);
b3 = new Button(“Reverse”);

add(b1);
add(b2);
add(b3);

b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
}

public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand().equals(“On”))
{
flag=true;
flag_reverse=false;
}
else if(ae.getActionCommand().equals(“Off”))
flag=false;
else if(ae.getActionCommand().equals(“Reverse”))
flag_reverse=true;

repaint();
}

public void paint(Graphics g)
{

g.drawLine(150,150,150,250);

if(flag)
{
if(flag_reverse)
{
g.drawString(“revdfsdfsdf”,100,100);
}
else
{
g.drawString(“clock”,100,100);

cx = 200;
cy = 200;
r = 50;
for (int i=0; ; i++)
{

r1 = (0+i)*PI ;
r2 = (120+i) * PI;
r3 = (240+i) * PI;

g.drawLine( (int)(cx+r * Math.cos(r1)) , (int)(cy+r * Math.sin(r1)),(int) ( cx+r * Math.cos(r2)) , (int) (cy+r * Math.sin(r2)));
//g.drawLine( cx+r * Math.cos(r2) , cy+r * Math.sin(r2), cx+r * Math.cos(r3) , cy+r * Math.sin(r3));
//g.drawLine( cx+r * Math.cos(r3) , cy+r * Math.sin(r3), cx+r * Math.cos(r1) , cy+r * Math.sin(r1));

try
{
Thread.sleep(50);
}
catch(Exception e)
{
}

}
}

}
else
{
g.drawString(“stop”,100,100);
}

}

}

/**
* @(#)Prog_05.java
*
*
* @author Adarsh Patel
* @version 1.00 2008/11/6
*
*        left justified, right justified and center justified
*/

/*<APPLET CODE=”Prog_05″ WIDTH=500 HEIGHT=500>
*</APPLET>
*/

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.EventListener;

public class Prog_05 extends Applet
{
int curx=0,cury=0;
Button b1,b2,b3;
String s1,s2,s3;

public void init()
{
s1 = “M.C.A.”;
s2 = “M.B.A.”;
s3 = “”;

b1 = new Button(“Button_1”);
b2 = new Button(“Button_2”);
b3 = new Button(“Button_3”);

Font f = new Font(“MS Serif”,Font.BOLD,20);
setFont(f);

Dimension d = this.getSize();
System.out.println (“Default dimention :” + d);
Dimension setnew = new Dimension(500,500);
setSize(setnew);
System.out.println (“New Dimention : ” + setnew);

}

public void paint(Graphics g)
{
curx=0;
cury=0;

g.setColor(Color.blue);

Dimension d =this.getSize();
FontMetrics fm = g.getFontMetrics();

firstlineright(s1,d.width,d.height,g);
secondlineleft(s2,g);

//setSize(300,300);
centerline( s1.concat(s2) ,d.width,d.height,g);
}

void firstlineright(String s, int w, int h, Graphics g)
{
g.setColor(Color.red);
FontMetrics fm = g.getFontMetrics();
cury = fm.getHeight();
curx = w – fm.stringWidth(s);
g.drawString(s,curx,cury);
curx = fm.stringWidth(s);
}

void secondlineleft(String s,Graphics g)
{
FontMetrics fm = g.getFontMetrics();
cury += fm.getHeight();
curx = 0;
g.drawString(s,curx,cury);
curx = fm.stringWidth(s);
}

void centerline(String s,int w,int h, Graphics g)
{

FontMetrics fm = g.getFontMetrics();
//System.out.println (“FM : ” + fm);
//System.out.println (“width : ” + fm.stringWidth(s));
//System.out.println (“height : ” + fm.getHeight());
int x = (w – fm.stringWidth(s))/2;
int y = (h – fm.getHeight())/2;
g.drawString(s,x,y);

}

}

/**
* @(#)Assignment_09.java
*
*
* @author Adarsh Patel
* @version 1.00 2008/11/9
*/

/*<applet code=”Prog_09″ width=400 height=400>
*</applet>
*
*
*/

import java.awt.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.*;
import java.applet.*;

public class Prog_09 extends Applet implements MouseListener, MouseMotionListener
{
int clickx,clicky,r,flag;
int tx=0,ty=0;

public void init()
{
clickx =0;
clicky = 0;
r =0;
flag=0;
addMouseListener(this);
addMouseMotionListener(this);
}

public void mousePressed(java.awt.event.MouseEvent event)
{
clickx = event.getX();
clicky = event.getY();
repaint();
//System.out.println (event.getX() + ” , ” + event.getY() );
}

public void mouseReleased(java.awt.event.MouseEvent event)
{
int xx,yy;

xx = Math.abs(event.getX() – clickx);
yy = Math.abs(event.getY() – clicky);

if(xx>=yy)
r = xx;
else
r = yy;

flag=1;
//System.out.println (event.getX() + ” , ” + event.getY() );
tx = event.getX();
ty = event.getY();
repaint();
}

public void paint(Graphics g)
{
g.drawString(“hello at :” + clickx + ” , ” + clicky ,clickx,clicky);
g.drawString(“hello at :” + tx + ” , ” + ty ,tx,ty);
g.drawString(” ” + r,100,100);

if(flag==1)
{
g.drawArc(clickx-r/2,clicky-r/2,r,r,0,360);
flag=0;
}

}

public void mouseClicked(java.awt.event.MouseEvent event)
{
//System.out.println (event.getX() + ” , ” + event.getY() );
}

public void mouseDragged(java.awt.event.MouseEvent event)
{
//System.out.println (event.getX() + ” , ” + event.getY() );
}

public void mouseMoved(java.awt.event.MouseEvent event)
{
//System.out.println (event.getX() + ” , ” + event.getY() );
}
public void mouseEntered(java.awt.event.MouseEvent event)
{
//System.out.println (event.getX() + ” , ” + event.getY() );
}

public void mouseExited(MouseEvent event)
{
//System.out.println (“MouseExited”);
}

}

/**
* @(#)Prog_08.java
*
*
* @author Adarsh Patel
* @version 1.00 2008/11/6
*
*        Stop watch
*/

/*<APPLET CODE=”Prog_08″ WIDTH=700 HEIGHT=500>
*</APPLET>
*/

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.EventListener;

public class Prog_08 extends Applet implements Runnable, ActionListener
{
String str;
int hrs,mins,sec,milisec;
Button btnstart,btnstop,btnreset;

Thread t;

public void start()
{
Font f = new Font(“arial”,Font.BOLD,17);
setFont(f);

setBackground(Color.cyan);
setForeground(Color.blue);

t = new Thread(this);
hrs = 0;
mins = 0;
sec = 0;
milisec = 0;

btnstart = new Button(“Start”);
btnstop = new Button(“Stop”);
btnreset = new Button(“Reset”);

add(btnreset);
add(btnstart);
add(btnstop);

btnreset.addActionListener(this);
btnstart.addActionListener(this);
btnstop.addActionListener(this);

t.start();
}

public void run()
{
while(true)
{
if(milisec>99)
{
milisec=0;
sec += 1;
}

if(sec == 60)
{
sec = 0;
mins += 1;
}

if(mins == 60)
{
mins = 0;
hrs += 1;
}

str = String.valueOf(hrs) + ” : ” + String.valueOf(mins) + ” : ” +  String.valueOf(sec)  + ” : ” +  String.valueOf(milisec) ;
repaint();

try
{
Thread.sleep(9);
milisec += 1;
}
catch (Exception ex)         {    }
}
}

public void paint(Graphics g)
{
g.drawString(str,200,200);
}

public void actionPerformed(ActionEvent e)
{

if(e.getActionCommand().equals(“Reset”))
{
milisec = 0;
mins = 0;
hrs = 0;
sec =0;
str = String.valueOf(hrs) + ” : ” + String.valueOf(mins) + ” : ” +  String.valueOf(sec)  + ” : ” +  String.valueOf(milisec) ;
repaint();
}
else if(e.getActionCommand().equals(“Stop”))
{
t.suspend();
}
else if(e.getActionCommand().equals(“Start”))
{
t.resume();
}
repaint();
}
}

/**
* @(#)Prog_11.java
*
*
* @author Adarsh Patel
* @version 1.00 2008/11/6
*
*        appear dots in random place and draw line between them
*
*/

/*<APPLET CODE=”Prog_11″ WIDTH=700 HEIGHT=500>
*</APPLET>
*/

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.applet.*;
import java.util.EventListener;

public class Prog_11 extends Applet implements Runnable
{
int px,py;
Thread t;

public void init()
{
px = 0;
py =0;

t = new Thread(this);
t.start();
}

public void paint(Graphics g)
{
Dimension d = getSize();
int x = (int) (Math.random() * d.width);
int y = (int) (Math.random() * d.height);
g.setColor(Color.black);
//g.fillOval(x,y,5,5);
g.drawOval(x,y,5,5);

if(px!=0 && py!=0)
{
g.drawLine(px,py,x,y);
}
px = x;
py = y;

}

public void  run()
{
try
{
while(true)
{
repaint();
Thread.sleep(3000);
}
}
catch(InterruptedException ie)
{}
}

public void update(Graphics g)
{
paint(g);
}

}

/**
* @(#)Prog_13.java
*
*
* @author Adarsh Patel
* @version 1.00 2008/11/9
*
*        Write an applet that contains four buttons “Red”, ”Blue”,” Green” and “Clear”.
*        Your mouse pointer should works like a panicle of paintbrush.
*         When user presses the “Red”, “Blue” or “Green” button then change the pencil color according to that color.
*      When user press “C” or “Clear” button all the content of the display area is erased
*/
/*
*<applet code=”Prog_13″ height=400 width=400>
*</applet>
*
**/

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.EventListener;

public class Prog_13 extends Applet implements ActionListener, KeyListener, MouseListener, MouseMotionListener
{
Button bred,bgreen,bblue,bclear;
String pencolor;
int xx,yy;

public void init()
{
pencolor=””;
xx =0;
yy =0;

bred = new Button(“Red”);
bgreen = new Button(“Green”);
bblue = new Button(“Blue”);
bclear = new Button(“Clear”);

add(bred);
add(bgreen);
add(bblue);
add(bclear);

bred.addActionListener(this);
bgreen.addActionListener(this);
bblue.addActionListener(this);
bclear.addActionListener(this);

addMouseListener(this);
addMouseMotionListener(this);

}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals(“Red”))
{
pencolor = “R”;
}
else if(e.getActionCommand().equals(“Green”))
{
pencolor = “G”;
}
else if(e.getActionCommand().equals(“Blue”))
{
pencolor = “B”;
}
else //if(e.getActionCommand().equals(“Clear”))
{
pencolor = “C”;
}
//System.out.println (pencolor);
//repaint();
}

public void paint(Graphics g)
{
FontMetrics fm = g.getFontMetrics();

if(pencolor==”C”)
{
g.clearRect(0,0,400,fm.getHeight());
}
else if(pencolor==”R”)
{
setForeground(Color.red);
}
else if(pencolor==”G”)
{
setForeground(Color.green);
}
else //if(pencolor==”B”)
{
setForeground(Color.blue);
}

if(xx!=0 || yy!=0)
{
g.drawLine(xx,yy,xx+1,yy+1);
g.drawString(“.”,xx,yy);
}
}

public void keyTyped(KeyEvent e)
{
int t;
t = e.getKeyCode();

if (t == 67)
{
pencolor = “C”;
}
//System.out.println (“C”);
repaint();
}

public void mouseDragged(MouseEvent e)
{
xx = e.getX();
yy = e.getY();
//System.out.println (xx + ” ” + yy);
repaint();
}
public void mouseClicked(MouseEvent e)
{
//xx =    e.getX();
//yy =    e.getY();
//System.out.println (xx + ” ” + yy);
//repaint();
}

public void update(Graphics g)
{
paint(g);
}

public void mousePressed(MouseEvent e)
{
//
}

public void mouseReleased(MouseEvent e)
{
//
}

public void mouseEntered(MouseEvent e)
{
//
}

public void mouseExited(MouseEvent e)
{
//
}

public void mouseMoved(MouseEvent e)
{
//xx = e.getX();
//yy = e.getY();
//System.out.println (xx + ” ” + yy);
//repaint();
}

public void keyPressed(KeyEvent e)
{
//
}

public void keyReleased(KeyEvent e)
{
//
}

}

 

/**
* @(#)Prog_15.java
*
*
* @author Adarsh Patel
* @version 1.00 2008/11/9
*/
/*
*<applet code=”Prog_15″ height=500 width=750>
*</applet>
*
**/

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Prog_15 extends Applet implements ActionListener
{
TextField t1,t2;
Button b1,b2;
boolean  flag=false;

public void init()
{
Font f = new Font(“Arial”,Font.BOLD,17);
setFont(f);

setBackground(Color.cyan);
setForeground(Color.blue);

t1 = new TextField(“I am Lucky”);
t2 = new TextField(25);
add(t1);
add(t2);
t1.addActionListener(this);
t2.addActionListener(this);

b1 = new Button(“Transfer”);
b2 = new Button(“Undo”);

add(b1);
add(b2);

b1.addActionListener(this);
b2.addActionListener(this);

}

public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand().equals(“Transfer”))
{
flag=true;
System.out.println (t1.getSelectedText());
t2.setText(t1.getSelectedText() );
String a = t1.getText();

String ans = “”;
for (int i = 0; i<a.length(); i++)
{
if(i<t1.getSelectionStart() || i>=t1.getSelectionEnd())
{
ans += a.charAt(i);
}
}
t1.setText(ans);
}
else if(ae.getActionCommand().equals(“Undo”))
{
t1.setText(“I am Lucky”);
t2.setText(“”);
}
//repaint();
}

//     public void paint(Graphics g)
//     {
//         if(flag)
//         {
//             t2.setText(t1.getSelectedText() );
//             flag=false;
//         }
//
//     }

}

 

/**
* @(#)Prog_10.java
*
*
* @author Adarsh Patel
* @version 1.00 2008/11/6
*
*        Display Images
*/

/*<APPLET CODE=”Prog_10″ WIDTH=700 HEIGHT=500>
*</APPLET>
*/

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.EventListener;

public class Prog_10 extends Applet implements Runnable, ActionListener
{
Image imglist[] = new Image[4];
Image i;
int j=0;
Thread t;
boolean flag = true;
Button bstop,bstart;

public void init()
{
Font f = new Font(“Arial”, Font.BOLD,17);
setFont(f);
setBackground(Color.cyan);
setForeground(Color.blue);

imglist[0] = getImage(getCodeBase(),”1.jpg”);
imglist[1] = getImage(getCodeBase(),”2.jpg”);
imglist[2] = getImage(getCodeBase(),”3.jpg”);
imglist[3] = getImage(getCodeBase(),”4.jpg”);

bstart = new Button(“Start”);
bstop = new Button(“Stop”);

add(bstart);
add(bstop);

bstart.addActionListener(this);
bstop.addActionListener(this);

}

public void start()
{
t = new Thread(this);
t.start();
}
public void paint(Graphics g)
{
g.drawImage(i,10,50,this);
}

public void run()
{
while(flag)
{
i = imglist[j++];
if(j>3)
j=0;
repaint();
try
{
t.sleep(2000);
//Thread.sleep(5000);
}
catch(InterruptedException ie)
{
}

}
}

public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals(“Start”))
{
t.resume();
}
else if(e.getActionCommand().equals(“Stop”))
{
t.suspend();
}
repaint();
}

}

 

/**
* @(#)Prog_21.java
*
*
* @author Adarsh Patel
* @version 1.00 2008/11/9
*/
/*
*<applet code=”Prog_21″ height=400 width=400>
*</applet>
*
**/

import java.awt.event.*;
import java.awt.*;
import java.applet.*;
import java.util.EventListener;

public class Prog_21 extends Applet  implements ActionListener
{
String name,msg= “”;
CheckboxGroup cbg1,cbg2;
Checkbox year1,year2,year3;
Checkbox subc,subcpp,subjava;
Button submit;

public void init()
{
ComponentOrientation co = getComponentOrientation();
setComponentOrientation(co.LEFT_TO_RIGHT );

setBackground(Color.cyan);
setForeground(Color.blue);

Font f = new Font(“Arial”,Font.BOLD,15);
setFont(f);

setSize(400,400);
setLayout( new GridLayout(4,4));

name = “Adarsh”;

cbg1 = new CheckboxGroup();
cbg2 = new CheckboxGroup();

year1 = new Checkbox(“First”,cbg1,false);
year2 = new Checkbox(“Second”,cbg1,false);
year3 = new Checkbox(“Third”,cbg1,true);

subc = new Checkbox(“C”,cbg2,false);
subcpp = new Checkbox(“C++”,cbg2,true);
subjava = new Checkbox(“Java”,cbg2,false);

submit = new Button(“Submit”);

//        add(cbg1);
//setLayout(new FlowLayout(FlowLayout.LEFT));
add(year1);
add(year2);
add(year3);

//FlowLayout f2 = new FlowLayout(FlowLayout.RIGHT);
//subc.setLocation(50,50);

add(subc);
add(subcpp);
add(subjava);
add(submit);

submit.addActionListener(this);
}

public void actionPerformed(ActionEvent e)
{
msg = name + ” is studing in  ” + cbg1.getSelectedCheckbox().getLabel() + ” and Likes ” + cbg2.getSelectedCheckbox().getLabel() + ” progrmming ” ;

repaint();
}

public void paint(Graphics g)
{
g.drawString(msg,50,250);
}

}

 

/**
* @(#)Prog_12.java
*
*
* @author Adarsh Patel
* @version 1.00 2008/11/6
*
*        Stop watch
*/

/*<APPLET CODE=”Prog_12″ WIDTH=700 HEIGHT=500>
*</APPLET>
*/

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.EventListener;

public class Prog_12 extends Applet implements Runnable, ActionListener
{
String str;
int no;
Button btnstart,btnstop;//,btnreset;
Font f;

Thread t;

public void start()
{
t = new Thread(this);
no =0;

btnstart = new Button(“Start”);
btnstop = new Button(“Stop”);
//btnreset = new Button(“Reset”);

//add(btnreset);
add(btnstart);
add(btnstop);

//btnreset.addActionListener(this);
btnstart.addActionListener(this);
btnstop.addActionListener(this);

setBackground(Color.cyan);
setForeground(Color.blue);
f = new Font(“verdana”,Font.BOLD,25);
t.start();
}

public void run()
{
while(true)
{
str = String.valueOf(no);
repaint();

try
{
Thread.sleep(10);
no += 1;
}
catch (Exception ex)         {    }
}
}

public void paint(Graphics g)
{
g.setFont(f);
g.drawString(str,280,250);
}

public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals(“Stop”))
{
t.suspend();
}
else if(e.getActionCommand().equals(“Start”))
{
t.resume();
}
repaint();
}
}

 

/**
* @(#)Prog_26.java
*
*
* @author Adarsh Patel
* @version 1.00 2008/11/9
*/
/*
*<applet code=”Prog_26″ height=400 width=400>
*</applet>
*
**/

import java.awt.event.*;
import java.awt.*;
import java.applet.*;
import java.util.EventListener;

public class Prog_26 extends Applet  implements ActionListener
{
String msg= ” Press any One Button “;
Button b1,b2;
Label mylabel;

public void init()
{
setBackground(Color.cyan);
setForeground(Color.blue);
Font f = new Font(“Arial”,Font.BOLD,15);
setFont(f);
setSize(200,200);

mylabel = new Label();
mylabel.setText(msg);
add(mylabel);

b1 = new Button(“Zoom In”);
b2 = new Button(“Zoom Out”);

add(b1);
add(b2);

b1.addActionListener(this);
b2.addActionListener(this);

}

public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals(“Zoom In”))
{
msg = “Zoom-In Pressed”;
//System.out.println (“x : ” + getWidth() + ” y : ” + getHeight());
resize(getWidth()- 20,getHeight()-25);
//System.out.println (“x : ” + getWidth() + ” y : ” + getHeight());
}
else if(e.getActionCommand().equals(“Zoom Out”))
{
msg = “Zoom-Out Pressed”;
//System.out.println (“x : ” + getWidth() + ” y : ” + getHeight());
resize(getWidth() + 20,getHeight()+25);
//System.out.println (“x : ” + getWidth() + ” y : ” + getHeight());
}
repaint();
}

public void paint(Graphics g)
{
mylabel.setText(msg);
}
}

 

/**
* @(#)Prog_02.java
*
*
* @author Adarsh Patel
* @version 1.00 2008/11/6
*
*        left justified, right justified and center justified
*/

/*<APPLET CODE=”Prog_02″ WIDTH=500 HEIGHT=500>
*</APPLET>
*/

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.EventListener;

public class Prog_02 extends Applet implements ActionListener
{
int curx=0,cury=0;
Button b1,b2,b3;
String s1,s2,s3;
boolean  flag1,flag2,flag3;

public void init()
{

flag1 = false;
flag2 = false;
flag3 = false;

s1 = “M.C.A.”;
s2 = “M.B.A.”;
s3 = “”;

b1 = new Button(“Button_1”);
b2 = new Button(“Button_2”);
b3 = new Button(“Button_3”);

add(b1);
add(b2);
add(b3);

b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);

Font f = new Font(“MS Serif”,Font.BOLD,20);
setFont(f);

Dimension d = this.getSize();
System.out.println (“Default dimention :” + d);
Dimension setnew = new Dimension(500,500);
setSize(setnew);
System.out.println (“New Dimention : ” + setnew);

}

public void paint(Graphics g)
{
curx=0;
cury=0;

g.setColor(Color.blue);

Dimension d =this.getSize();
FontMetrics fm = g.getFontMetrics();

if(flag1)
{
firstlineright(s1,d.width,d.height,g);
}

if(flag2)
{
secondlineleft(s2,g);
}

if(flag3)
{
centerline( s1.concat(s2) ,d.width,d.height,g);
}

//setSize(300,300);

}

void firstlineright(String s, int w, int h, Graphics g)
{

FontMetrics fm = g.getFontMetrics();
cury = fm.getHeight();
curx = w – fm.stringWidth(s);
g.drawString(s,curx,cury);
curx = fm.stringWidth(s);
}

void secondlineleft(String s,Graphics g)
{
FontMetrics fm = g.getFontMetrics();
cury = fm.getHeight() * 2;
curx = 0;
g.drawString(s,curx,cury);
curx = fm.stringWidth(s);
}

void centerline(String s,int w,int h, Graphics g)
{

FontMetrics fm = g.getFontMetrics();
int x = (w – fm.stringWidth(s))/2;
int y = (h – fm.getHeight())/2;
g.drawString(s,x,y);
}

public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals(“Button_1”))
{
if(flag1)
{
flag1=false;
}
else
{
flag1=true;
}

}
else if(e.getActionCommand().equals(“Button_2”))
{
if(flag2)
{
flag2=false;
}
else
{
flag2=true;
}
}
else if(e.getActionCommand().equals(“Button_3″))
{
if(flag3)
{
flag3=false;
}
else
{
flag3=true;
}
}

repaint();
}

}

 

/**
* @(#)Prog_03.java
*
*
* @author Adarsh Patel
* @version 1.00 2008/11/6
*
*        Time showing in applet
*/

/*<APPLET CODE=”Prog_03” WIDTH=700 HEIGHT=500>
*</APPLET>
*/

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.applet.*;
import java.text.*;

public class Prog_03 extends Applet implements Runnable
{
Date d;
String str;
int mins,sec;
Thread t;
Font f = new Font(“Arial”,Font.BOLD,20);

public void start()
{
t = new Thread(this);
d = new Date();
//mins = d.getMinutes();
//sec = d.getSeconds();
mins = 0;
sec = 0;
t.start();
}

public void run()
{
while(true)
{
d = new Date();

if(sec == 60)
{
sec = 0;
mins += 1;
}

if(mins == 60)
mins = 0;

str = “”;
if(mins>9)
str += String.valueOf(mins);
else
str += “0” + String.valueOf(mins);

str += ” : “;

if(sec>9)
str += String.valueOf(sec);
else
str += “0” + String.valueOf(sec);

//str = String.valueOf(mins) + ” : ” +  String.valueOf(sec) ;
repaint();

try
{
Thread.sleep(1000);
sec += 1;
}
catch (Exception ex)
{
}
}
}

public void paint(Graphics g)
{
g.setFont(f);
g.drawString(d.toString(),100,100);
g.drawString(str,200,200);
}
}

 

/**
* @(#)Prog_06.java
*
*
* @author Adarsh Patel
* @version 1.00 2008/11/6
*
*        Draw two lines and rotate in clock wise and anticlock wise
*/

/*<APPLET CODE=”Prog_06″ WIDTH=500 HEIGHT=500>
*</APPLET>
*/

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.EventListener;

public class Prog_066 extends Applet implements ActionListener
{
int curx=0,cury=0;
Button b1,b2,b3;
//int x1,y1,x2,y2,x3,y3,x4,y4;
boolean flag_reverse,flag;
int cx,cy;
//int r1,r2,r3,PI=3;
int xc,yc,r,x1,x2,y1,y2,x3,y3,x4,y4,x5,y5,x6,y6;

public void init()
{
//true mens clock wise and 0 means anticlock
flag =true;

flag_reverse=false;

b1 = new Button(“On”);
b2 = new Button(“Off”);
b3 = new Button(“Reverse”);

add(b1);
add(b2);
add(b3);

b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);

xc = 200;
yc = 100;

x1=200;    y1=100;
x2=100;    y2=300;

}

public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand().equals(“On”))
{
flag=true;
flag_reverse=false;
}
else if(ae.getActionCommand().equals(“Off”))
flag=false;
else if(ae.getActionCommand().equals(“Reverse”))
flag_reverse=true;

repaint();
}

public void paint(Graphics g)
{

g.drawLine(150,150,150,250);

if(flag)
{
if(flag_reverse)
{
g.drawString(“revdfsdfsdf”,100,100);
}
else
{
g.drawString(“clock”,100,100);

cx = 200;
cy = 200;
r = 50;
for (int i=1; 😉
{
//static int i=1;
float a=(float)(i*3.1415/180.0);

x1= (int) (xc+(x1-xc)*Math.cos(a) – (y1-yc)*Math.sin(a));
y1= (int) (yc+(x1-xc)*Math.sin(a) + (y1-yc)*Math.cos(a));

x2= (int) (xc+(x2-xc)*Math.cos(a) – (y2-yc)*Math.sin(a));
y2= (int) (yc+(x2-xc)*Math.sin(a) + (y2-yc)*Math.cos(a));

//                    x3=xc+(x3-xc)*cos(a) – (y3-yc)*sin(a);
//                    y3=yc+(x3-xc)*sin(a) + (y3-yc)*cos(a);
//
//                    x4=xc+(x4-xc)*cos(a) – (y4-yc)*sin(a);
//                    y4=yc+(x4-xc)*sin(a) + (y4-yc)*cos(a);
//
//                    x5=xc+(x5-xc)*cos(a) – (y5-yc)*sin(a);
//                    y5=yc+(x5-xc)*sin(a) + (y5-yc)*cos(a);
//
//                    x6=xc+(x6-xc)*cos(a) – (y6-yc)*sin(a);
//                    y6=yc+(x6-xc)*sin(a) + (y6-yc)*cos(a);

g.drawLine(x1,y1,x2,y2);
//line(x3,y3,x4,y4);
//line(x5,y5,x6,y6);

try
{
Thread.sleep(50);
}
catch(Exception e)
{
}

}
}

}
else
{
g.drawString(“stop”,100,100);
}

}

}

 

for more java tutorial click here

 

 

Adarsh Patel

My Self Adarsh Suryakant Patel. Currently I am working as Assistant Professor at Parul Instiute of Engineering and Technology, MCA Department, Limda, Vadodara. Major Responsibility : * Lectures * Organizing Training and Expert Sessions for * Placement and Recruitment * Faculty Representative