Tuesday, 29 November 2011

Growing text

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

//<applet code="Growing" height=700 width=800> </applet>

public class Growing extends Applet
{
    int size=10;
    Thrd tt=new Thrd();
    Image img;
    public void init()
    {
        tt.start();
        img=getImage(getDocumentBase(),"Water lilies.jpg");
    }
   
   
    public void paint(Graphics g) 
    {
        g.setColor(Color.yellow);
        g.setFont(new Font("Arial",Font.BOLD,size));
        g.drawImage( img, size,size ,this);
        g.drawString("Welcome to All",200,350);
    }
    public void destroy()
    {
        tt.stop();
        System.out.print("finished");
       
    }


    class Thrd extends Thread
    {
        public void run()
        {
            do
            {
                try
                {
                    Thread.sleep(200);
                    size++;
                    if(size==70) size=10;
                    repaint();
                }
                catch(InterruptedException ie)
                {
                    System.out.println ("Thread Exception");
                }
            }while(true);
        }
    }
}

Tuesday, 15 November 2011

Face

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

/*
<html>
<body>
<h1>Java Applet Demo</h1>
<applet code=prg9.class width=500 height=350>
</applet>
</body>
</html>
*/

public class prg9 extends Applet
{
  Label l1;
  private int mouseX, mouseY;
  private boolean mouseclicked = false;
  public void init()
  {
   l1=new Label("Click Any Where On The Window.");
   add(l1);
  }
 public void paint(Graphics g)
 {
  g.drawOval(40, 40, 120, 150);
  g.drawOval(57, 75, 30, 30);
  g.drawOval(110, 75, 30, 30);
  g.fillOval(68, 81, 10, 10);
  g.fillOval(121, 81, 10, 10);
  g.drawLine(100, 100, 100, 117);
  g.fillRect(60, 139, 80, 10);
  if (mouseclicked)
  {
   g.clearRect(60, 139, 80, 10);
   g.drawArc(70, 100, 60, 60, 0, -180);
   mouseclicked = false;
  }
 }
 public boolean mouseDown(Event e, int x, int y)
 {
  mouseX = x;
  mouseY = y;
  mouseclicked = true;
  repaint();
  return true;
 }
}

shapes

import java.awt.*;
import java.applet.*;
public class shapes extends Applet
{
public void paint(Graphics g)
{
try
{
// cone

g.drawOval(100,100,100,50); //cone base
g.drawLine(100,125,150,15); //cone left side
g.drawLine(200,125,150,15); //cone right side

//cylinder

g.drawOval(100,200,100,50);//top circle
g.drawOval(100,400,100,50);//bottom circle
g.drawLine(100,225,100,425);//left side line
g.drawLine(200,225,200,425);//right side line

//square inside circle
g.drawOval(300,400,200,200);//circle
g.drawRect(350,450,100,100);//square

//circle inside square
g.drawRect(300,100,200,200);//square
g.drawOval(350,150,100,100);//circle

//CUBE
g.drawRect(600,200,150,150);
g.drawRect(650,250,150,150);
//LINES TO FORM TOP OF CUBE
g.drawLine(600,200,650,250);
g.drawLine(750,200,800,250);
//LINES TO FORM BOTTOM OF CUBE
g.drawLine(600,350,650,400);
g.drawLine(750,350,800,400);
}
catch(Exception e)
{ }
}
}

Linked List

import java.util.*;
import java.util.Scanner;
class linklist
{
public static void main(String[] args)
{
LinkedList<Integer> ll=new LinkedList<Integer>();
int ch;
for(;;)
{
System.out.println("1:Append \n 2:Add At First \n 3:Add At Last \n 4:Add At Location \n5:Remove Location \n 6:Remove First \n 7:Remove Last \n 8: Display \n 9:Exit\n");
System.out.println("Enter the choice:");
Scanner in=new Scanner(System.in);
ch=in.nextInt();
switch(ch)
{
case 1:     System.out.println("Enter The Item:");
            int a=in.nextInt();
            ll.add(a);
            break;
case 2:     System.out.println("Enter The Item:");
            a=in.nextInt();
            ll.addFirst(a);
            break;
case 3:     System.out.println("Enter  The Item:");
            a=in.nextInt();
            ll.addLast(a);
            break;
case 4:     System.out.println("Enter the location:");
            int b=in.nextInt();
            System.out.println("Enter  The Item:");
            a=in.nextInt();
            ll.add(b,a);
            break;
case 5:     System.out.println("Enter The location to delete:");
            a=in.nextInt();
            ll.remove(a);
            break;
case 6:     ll.removeFirst();
            break;
case 7:     ll.removeLast();
            break;
case 8:    System.out.println("ELEMENTS ARE:"+ll);
           System.out.println("size of linklist="+ll.size());
           System.out.print("Reverse Data :[ ");
           for(int i=ll.size(); i>0;i--)
             {
             int x=ll.get(i-1);
             System.out.print(+x+" ");
            }
            System.out.print("]\n");
             break;
case 9:   System.exit(0);
default:
              System.out.println("invalid choice:");
             
         }
     }
   }
}
            

Area - Interface

import java.io.*;
import java.util.Scanner;
interface area
{
float pi=3.14F;
void circle();
void square();
}
class imp implements area
{
public void circle()
{
System.out.print("enter the value of Radius:");
Scanner in=new Scanner(System.in);
float r=in.nextFloat();
float carea=pi*r*r;
System.out.println("\nArea of circle ="+carea);
}
public void square()
{
System.out.println("enter the value of side:");
Scanner in=new Scanner(System.in);
float x=in.nextFloat();
float sarea=x*x;
System.out.println("\n Area of circle:"+sarea);
}
}
class Interface
{
public static void main(String[] args)
{
imp obj=new imp();
obj.circle();
obj.square();
}
}

hash table

import java.util.*;
import java.io.*;

public class hash1{
  public static void main(String[] args) throws IOException{
  int key;
  try{
  BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  System.out.print("How many elements you want to enter to the hash table : ");
  int n = Integer.parseInt(in.readLine());
  Hashtable<Integer, String> hashTable = new Hashtable<Integer, String>();
  for(int i = 0; i < n; i++){
  System.out.print("Enter key for the hash table : ");
  key = Integer.parseInt(in.readLine());
  System.out.print("Enter value for the key : ");
  hashTable.put(key, in.readLine());
  }
  Map<Integer, String> map = new TreeMap<Integer, String>(hashTable);
  System.out.println(map);
  }
  catch(NumberFormatException ne){
  System.out.println(ne.getMessage() + " is not a legal value.");
  System.out.println("Please enter a numeric value.");
  System.exit(1);
  }
  }
}

flag

import java.io.*;
import java.applet.*;
import java.awt.*;
public class flag extends Applet
{
public void paint(Graphics g)
{
g.fillOval(60,450,120,50);
g.fillRect(110,60,10,400);
g.setColor(Color.red);
g.fillRect(120,80,150,30);
g.setColor(Color.white);
g.fillRect(120,110,150,30);
g.setColor(Color.green);
g.fillRect(120,140,150,30);
g.setColor(Color.black);
g.drawRect(120,80,150,90);
g.setColor(Color.blue);
g.drawOval(180,110,30,30);
int t=0;
int x=195,y=125;
double x1,y1;
double r=15;
double d;
for(int i=1;i<=24;i++)
{
d=(double)t*3.14/180.0;
x1=x+(double)r*Math.cos(d);
y1=y+(double)r*Math.sin(d);
g.drawLine(x,y,(int)x1,(int)y1);
t+=360/24;
}
}
}

File attributes

import java.io.File;
public class fileats
 {
  public static void main(String[] argv) throws Exception
{
    File f = new File("c:/INSTALL.LOG");

    if (!f.exists()) {
      System.out.println("File not found.");
      return;
    }
    if (f.canRead())
      System.out.println("  Readable");
    else
      System.out.println("  Not Readable");

    if (f.canWrite())
      System.out.println("  Writable");
    else
      System.out.println("  Not Writable");
  
      if(f.isHidden())
      
            System.out.println( " is Hidden.");
else
System.out.println(" Not a Hidden file");
     
  }
}

datediff

import java.io.*;
import java.util.*;
import java.text.*;

public class DiffDate
{
    public static void main(String[] args) throws ParseException, IOException
    {
        DataInputStream in=new DataInputStream(System.in);
       
        DateFormat df = new SimpleDateFormat ("dd-MM-yyyy");
        // Get Date 1
        System.out.print("\nEnter the First Date : ");
        Date d1 = df.parse(in.readLine().trim());
        // Get Date 2
        System.out.print("\nEnter the Second Date : ");
        Date d2 = df.parse(in.readLine().trim());
       
        String relation;
        if (d1.equals(d2))
            relation = "the same date as";
        else if (d1.before(d2))
                relation = "before";
             else
                relation = "after";
        System.out.println(d1 + " is " + relation +" "+ d2);
        long diff = d2.getTime() - d1.getTime( );
        System.out.print("\n\nThe Difference between d1 and d2 is : "+(diff / (1000*60*60*24)));
    }
}

stack

import java.util.*;
import java.io.*;
class StackD {
static void showpush(Stack st, int a) {
st.push(new Integer(a));
System.out.println("push(" + a + ")");
System.out.println("stack: " + st);
}
static void showpop(Stack st) {
System.out.print("pop -> ");
Integer a = (Integer) st.pop();
System.out.println(a);
System.out.println("stack: " + st);
}
public static void main(String args[])throws IOException
 {
Stack st = new Stack();
DataInputStream d=new DataInputStream(System.in);
int will=1;
System.out.println("stack: " + st);
while(will==1)
{
System.out.println("Enter your choice:");
System.out.println("*****1.Push      2.Pop   3.Exit*****");

int c=Integer.parseInt(d.readLine());
switch(c)
{
case 1:
{
System.out.println("Enter a number to stack:");
int n=Integer.parseInt(d.readLine());
showpush(st, n);
}
break;
case 2:
{
try {
showpop(st);
} catch (EmptyStackException e) {
System.out.println("empty stack");
}
}
break;
default:
System.out.println("Invalid Choice");
}
System.out.println("Do U Want To Continue? 1.Yes 2.No");
will=Integer.parseInt(d.readLine());
}
}
}

Current date and time using applet

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

/*
<APPLET CODE = "curtime.class" WIDTH = "500" HEIGHT = "400">
Download Java Plug In Please
</APPLET>
*/
public class curtime extends Applet implements Runnable
{
  Thread t,t1;
  Label l1,l2;
  TextField t2,t3;
  int day, month, year;
  public void init()
  {
   t2=new TextField(20);
   l1=new Label("Time");
   t3=new TextField(20);
   l2=new Label("Date");
   add(l1);
   add(t2);
   add(l2);
   add(t3); 
  }
  public void start()
  {
   t = new Thread(this);
   t.start();
  }
  public void run()
  {
   t1 = Thread.currentThread();
   while(t1 == t){
   repaint();
   try
   {
    t1.sleep(1000); 
   }
   catch(InterruptedException e)
   {
   }
   }
 }

  public void paint(Graphics g)
  {
    Calendar cal = new GregorianCalendar();
    GregorianCalendar date = new GregorianCalendar();
    String hour = String.valueOf(cal.get(Calendar.HOUR));
    String minute = String.valueOf(cal.get(Calendar.MINUTE));
    String second = String.valueOf(cal.get(Calendar.SECOND));
    String msg=""+hour + ":" + minute + ":" + second;
    t2.setText(msg);
    day = date.get(Calendar.DAY_OF_MONTH);
    month = date.get(Calendar.MONTH);
    year = date.get(Calendar.YEAR);
    String msg1=""+day + ":" + month + ":" + year;
    t3.setText(msg1);
  }
}


Biggest of 3 nos. Using Applet

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
  public class big extends Applet implements ActionListener
  {
        TextField t1,t2,t3,t4;
        Label Result;
        Button b1;
        public void init()
       {
           t1=new TextField(10);
          add(t1);
           
          t2=new TextField(10);
          add(t2);
       
        t3=new TextField(10);
        add(t3);

          b1=new Button("Result");
          add(b1);
             
          b1.addActionListener(this);

          t4=new TextField(10);
          add(t4);
     }
      public void actionPerformed(ActionEvent e)
         {
         if(e.getSource()==b1)
          {
           if((Integer.parseInt(t1.getText())>Integer.parseInt(t2.getText())) && (Integer.parseInt(t1.getText())>Integer.parseInt(t3.getText())))
         
        {
         t4.setText(String.valueOf(t1.getText()));
        }
   
        else if(Integer.parseInt(t2.getText())>Integer.parseInt(t3.getText()))
   
        {
        t4.setText(String.valueOf(t2.getText()));
        }

        else

        {
        t4.setText(String.valueOf(t3.getText()));
        }
              }
         }
  }
/*
<applet code=big.class
width=300
height=400>
</applet>
*/