Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Saturday, 10 December 2011

Human face or Smiley program in java using Applet


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


public class HumanFace extends Applet{
private int mouseX, mouseY;
private boolean mouseclicked = false;
public void init(){
setBackground(Color.GREEN);
}


public void paint(Graphics g)
{
g.setColor(Color.YELLOW);
g.fillOval(40,40,120,150);
g.setColor(Color.BLACK);
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.setColor(Color.RED);
g.drawLine(100,100,100,130);
g.fillRect(75,140,50,7);
if (mouseclicked) {
g.clearRect(75,140,50,7);
g.setColor(Color.BLUE);
g.fillRect(70,139,60,9);
//g.drawArc(40,100,100,50,0,-120);
mouseclicked = true;
}


}


public boolean mouseDown(Event e, int x, int y )
{
mouseX=x; mouseY=y;
mouseclicked = true;
repaint();
return true;
}
}
/* <applet code="HumanFace.class" height= "300" width="200" >
</applet> */


//Output look like this...



Sunday, 13 November 2011

Clock applet program in java


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


/**
 * programmingwithdev.blogspot.com
 * clock applet program
 **/
public class Clock extends Applet implements Runnable {
    private volatile Thread timer;       // The thread that displays clock
    private int lastxs, lastys, lastxm,
                lastym, lastxh, lastyh;  // Dimensions used to draw hands 
    private SimpleDateFormat formatter;  // Formats the date displayed
    private String lastdate;             // String to hold date displayed
    private Font clockFaceFont;          // Font for number display on clock
    private Date currentDate;            // Used to get date to display
    private Color handColor;             // Color of main hands and dial
    private Color numberColor;           // Color of second hand and numbers


    public void init() {
        int x,y;
        lastxs = lastys = lastxm = lastym = lastxh = lastyh = 0;
        formatter = new SimpleDateFormat ("EEE MMM dd hh:mm:ss yyyy", 
                                          Locale.getDefault());
        currentDate = new Date();
        lastdate = formatter.format(currentDate);
        clockFaceFont = new Font("Serif", Font.PLAIN, 14);
        handColor = Color.blue;
        numberColor = Color.red;


        try {
            setBackground(new Color(Integer.parseInt(getParameter("bgcolor"),
                                                     16)));
        } catch (NullPointerException e) {
        } catch (NumberFormatException e) {
        }
        try {
            handColor = new Color(Integer.parseInt(getParameter("fgcolor1"),
                                                   16));
        } catch (NullPointerException e) {
        } catch (NumberFormatException e) {
        }
        try {
            numberColor = new Color(Integer.parseInt(getParameter("fgcolor2"),
                                                     16));
        } catch (NullPointerException e) {
        } catch (NumberFormatException e) {
        }
        resize(300,300);              // Set clock window size
    }


    // Paint is the main part of the program
    public void paint(Graphics g) {
        int xh, yh, xm, ym, xs, ys;
        int s = 0, m = 10, h = 10;
        int xcenter = 80, ycenter = 55;
        String today;


        currentDate = new Date();
        
        formatter.applyPattern("s");
        try {
            s = Integer.parseInt(formatter.format(currentDate));
        } catch (NumberFormatException n) {
            s = 0;
        }
        formatter.applyPattern("m");
        try {
            m = Integer.parseInt(formatter.format(currentDate));
        } catch (NumberFormatException n) {
            m = 10;
        }    
        formatter.applyPattern("h");
        try {
            h = Integer.parseInt(formatter.format(currentDate));
        } catch (NumberFormatException n) {
            h = 10;
        }
    
        // Set position of the ends of the hands
        xs = (int) (Math.cos(s * Math.PI / 30 - Math.PI / 2) * 45 + xcenter);
        ys = (int) (Math.sin(s * Math.PI / 30 - Math.PI / 2) * 45 + ycenter);
        xm = (int) (Math.cos(m * Math.PI / 30 - Math.PI / 2) * 40 + xcenter);
        ym = (int) (Math.sin(m * Math.PI / 30 - Math.PI / 2) * 40 + ycenter);
        xh = (int) (Math.cos((h*30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 30
                   + xcenter);
        yh = (int) (Math.sin((h*30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 30
                   + ycenter);
    
        // Draw the circle and numbers
        g.setFont(clockFaceFont);
        g.setColor(Color.blue);
        g.drawArc(xcenter-50, ycenter-50, 100, 100, 10, 360);
        g.setColor(numberColor);
        g.drawString("9", xcenter-45, ycenter+3); 
        g.drawString("3", xcenter+40, ycenter+3);
        g.drawString("12", xcenter-5, ycenter-37);
        g.drawString("6", xcenter-3, ycenter+45);




        // Get the date to print at the bottom
        formatter.applyPattern("EEE MMM dd HH:mm:ss yyyy");
        today = formatter.format(currentDate);


        // Erase if necessary
        g.setColor(getBackground());
        if (xs != lastxs || ys != lastys) {
            g.drawLine(xcenter, ycenter, lastxs, lastys);
            g.drawString(lastdate, 5, 125);
        }
        if (xm != lastxm || ym != lastym) {
            g.drawLine(xcenter, ycenter-1, lastxm, lastym);
            g.drawLine(xcenter-1, ycenter, lastxm, lastym); 
        }
        if (xh != lastxh || yh != lastyh) {
            g.drawLine(xcenter, ycenter-1, lastxh, lastyh);
            g.drawLine(xcenter-1, ycenter, lastxh, lastyh); 
        }


        // Draw date and hands
        g.setColor(Color.black);
        g.drawString(today, 5, 125);    
        g.drawLine(xcenter, ycenter, xs, ys);
        g.setColor(handColor);
        g.drawLine(xcenter, ycenter-1, xm, ym);
        g.drawLine(xcenter-1, ycenter, xm, ym);
        g.drawLine(xcenter, ycenter-1, xh, yh);
        g.drawLine(xcenter-1, ycenter, xh, yh);
        lastxs = xs; lastys = ys;
        lastxm = xm; lastym = ym;
        lastxh = xh; lastyh = yh;
        lastdate = today;
        currentDate = null;
    }


    public void start() {
        timer = new Thread(this);
        timer.start();
    }


    public void stop() {
        timer = null;
    }


    public void run() {
        Thread me = Thread.currentThread();
        while (timer == me) {
            try {
                Thread.currentThread().sleep(100);
            } catch (InterruptedException e) {
            }
            repaint();
        }
    }


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


    public String getAppletInfo() {
        return "Title: A Clock \n"
            + "Author: Rachel Gollub, 1995 \n"
            + "An analog clock.";
    }
  
    public String[][] getParameterInfo() {
        String[][] info = {
            {"bgcolor", "hexadecimal RGB number", 
             "The background color. Default is the color of your browser."},
            {"fgcolor1", "hexadecimal RGB number", 
             "The color of the hands and dial. Default is blue."},
            {"fgcolor2", "hexadecimal RGB number", 
             "The color of the second hand and numbers. Default is dark gray."}
        };
        return info;
    }
}


/* <applet code="Clock.class" height = "300" width = "200" >
</applet>
*/



My Clock      

My Applet Clock

Saturday, 15 October 2011

java progarm for moving cat.

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Color;
import java.applet.*;
public class cat extends Applet implements Runnable
 {
  Image catpics[] = new Image[9];
  Image currentimg;
  Thread runner;
  int xpos;
  int ypos = 50;
public void init()
{
    String catsrc[] = {
 "right1.gif", "right2.gif", "stop.gif", "yawn.gif", "scratch1.gif",
 "scratch2.gif","sleep1.gif", "sleep2.gif",  "awake.gif"     
 };
 for (int i=0; i < catpics.length; i++)
 {
        catpics[i] = getImage(getDocumentBase(),catsrc[i]);
    }
}
         public void start()
   {
     if (runner == null)
     {
      runner = new Thread(this);
      runner.start();
     }
          }


  public void stop()
   {
         if (runner != null)
     {
                  runner.stop();
                  runner = null;
             }
          }
         public void run()
   {
         setBackground(Color.white);
         catrun(0, size().width / 2);
         currentimg = catpics[2];
                   repaint();
         pause(1000);
         currentimg = catpics[3];
                repaint();
                pause(1000);
                catscratch(4);
                catsleep(5);
                currentimg = catpics[8];
                repaint();
                pause(500);
                catrun(xpos, size().width + 10);
          }
       void catrun(int start, int end)
   {
         for (int i = start; i < end; i += 10)
    {
                 xpos = i;
                 if (currentimg == catpics[0])
                    currentimg = catpics[1];
                 else currentimg = catpics[0];
                 repaint();
                 pause(150);
             }
          }
      void catscratch(int numtimes)
   {
            for (int i = numtimes; i > 0; i--)
     {
                  currentimg = catpics[4];
                  repaint();
                  pause(150);
                  currentimg = catpics[5];
                  repaint();
           pause(150);
              }
         }


       void catsleep(int numtimes)
   {
            for (int i = numtimes; i > 0; i--)
     {
                  currentimg = catpics[6];
                  repaint();
                  pause(250);
                  currentimg = catpics[7];
                  repaint();
                  pause(250);
              }
   }
     
  void pause(int time)
   {
            try
     {
      Thread.sleep(time);
     }
        
     catch (InterruptedException e)
     {
     }
   }
      public void paint(Graphics g)
   {
           if (currentimg != null)
              g.drawImage(currentimg, xpos, ypos, this);
   }
 }
/*
<APPLET
CODE=cat.java
WIDTH=600
HEIGHT=100 >
</APPLET>
*/












Fig.
Output:


           

Friday, 14 October 2011

Program to count the Vowels in a given string


//this program counts the vowels in a string


import java.util.*;
class CountVowels
{
  public static void main(String args[])
{
Scanner input=new Scanner(System.in);
System.out.print("Enter String: ");
String st = input.nextLine();
int count = 0; 
for (int i = 0; i < st.length(); i++) 
{
char c = st.charAt(i);
if (c=='a' || c=='e' || c=='i' || c=='o' || c=='u')
{
count++;
}
}
System.out.println("There are" + " " + count + " " + "vowels in this string.");
}
}

Program to Reverse a String


import java.util.*;

class ReverseString
{
   public static void main(String args[])
   {
      String original, reverse = "";
      Scanner in = new Scanner(System.in);

      System.out.println("Enter a string to reverse - ");
      original = in.nextLine();
      int length = original.length();


      for ( int i = length - 1 ; i >= 0 ; i-- )
         reverse = reverse + original.charAt(i);

      System.out.println("Reverse of entered string is : "+ reverse);
   }
}

Monday, 3 October 2011

JavaProgram for ImageFilter


/*<applet code=TileImage.java width=288 height=399>
<param name=img value=33685.jpg>
</applet>
*/
import java.applet.*;
import java.awt.*;
import java.awt.image.*;
public class TileImage extends Applet
{
Image img;
Image cell[]=new Image[4*4];
int iw,ih;
int tw,th;
public void init()
{
try
{
img=getImage(getDocumentBase(),getParameter("img"));
MediaTracker t=new MediaTracker(this);
t.addImage(img,0);
t.waitForID(0);
iw=img.getWidth(null);
ih=img.getHeight(null);
tw=iw/4;
th=ih/4;
CropImageFilter f;
FilteredImageSource fis;
t=new MediaTracker(this);
for(int y=0;y<4;y++)
{
for(int x=0;x<4;x++)
{
f=new CropImageFilter(tw*x,th*y,tw,th);
fis=new FilteredImageSource(img.getSource(),f);
int i=y*4+x;
cell[i]=createImage(fis);
t.addImage(cell[i],i);
}
}
t.waitForAll();
for(int i=0;i<32;i++)
{
int si=(int)(Math.random()*16);
int di=(int)(Math.random()*16);
Image tmp=cell[si];
cell[si]=cell[di];
cell[di]=tmp;
}
}
catch(InterruptedException e)
{
System.out.println("Interrupted");
}
}
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{
for(int y=0;y<4;y++)
{
for(int x=0;x<4;x++)
{
g.drawImage(cell[y*4+x],x*tw,y*th,null);
}
}
}
}
Image:


OutPut:



Sunday, 2 October 2011

Java Source codes for ImageProducer


/*
<applet code="MemoryImageGenerator.java" width=256 height=256>
</applet>
*/
import java.applet.*;
import java.awt.*;
import java.awt.image.*;
public class MemoryImageGenerator extends Applet
{
Image img;
public void init()
{
Dimension d=getSize();
int w=d.width;
int h=d.height;
int pixels[]=new int[w*h];
int i=0;
for(int y=0;y<h;y++)
{
for(int x=0;x<w;x++)
{
int r=(x^y)&0xff;
int g=(x*2^y*2)&0xff;
int b=(x*4^y*4)&0xff;
pixels[i++]=(255<<24)|(r<<16)|(g<<8)|b;
}
}
img=createImage(new MemoryImageSource(w,h,pixels,0,w));
}
public void paint(Graphics g)
{
g.drawImage(img,0,0,this);
}
}
OutPut:





Java Program for Double Buffering


/*<applet code=DoubleBuffer width=250 height=250>
</applet>*/
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class DoubleBuffer extends Applet
{
int gap=3;
int mx,my;
boolean flicker=true;
Image buffer=null;
int w,h;
public void init()
{
Dimension d=getSize();
w=d.width;
h=d.height;
buffer=createImage(w,h);
addMouseMotionListener(new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent me)
{
mx=me.getX();
my=me.getY();
flicker=false;
repaint();
}
public void mouseMoved(MouseEvent me)
{
mx=me.getX();
my=me.getY();
flicker=true;
repaint();
}
});
}
public void paint(Graphics g)
{
Graphics screengc=null;
if(!flicker)
{
screengc=g;
g=buffer.getGraphics();
}
g.setColor(Color.blue);
g.fillRect(0,0,w,h);
g.setColor(Color.red);
for(int i=0;i<w;i+=gap)
g.drawLine(i,0,w-i,h);
for(int i=0;i<h;i+=gap)
g.drawLine(i,0,w,h-i);
g.setColor(Color.black);
g.drawString("Press mouse button to double buffer",10,h/2);
g.setColor(Color.yellow);
g.fillOval(mx-gap,my-gap,gap*2+1,gap*2+1);
if(!flicker)
{
screengc.drawImage(buffer,0,0,null);
}
}
public void update(Graphics g)
{
paint(g);
}
}
OutPut:

Java program for Graphics using Applet


import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
 public class MyGraphic extends java.applet.Applet implements ActionListener
     {
      boolean go =false;
      int x, oldx, y, oldy;
      double angle;
      Button drawButton =new Button("Draw");
      public void init()
      {
    add(drawButton);
    drawButton.addActionListener(this);
       }
            public void paint(Graphics screen)
            {
             resize(500,500);
             int x,y, oldx, oldy;
             double i=0;
             double a=0;
             double b=0;
             oldx=250;
             oldy=250;
             double ang=0;
             double angle;
             x=1;
           if (go==true)
           {
             for (i=0; i<800; i=i+1)
             {
       a=a+.5;
       b=b+.5;
       ang=ang+.8;
       x=(int)(a*Math.cos(ang)+250);
       y=(int)(b*Math.sin(ang)+250);
       screen.drawLine(x,y, oldx, oldy);
       oldx=x;
       oldy=y;
       pause(1);
    }


  }
       }
       public void actionPerformed(ActionEvent event)
  {
                Object source = event.getSource();
    if (source.equals(drawButton))
      {
System.out.println("Started...");
           go=true;
    repaint();
           }
    }
       void pause(int time)
  {
          try {
              Thread.sleep(time);
          }
  catch (InterruptedException e) { }
      }


     }




/*<applet code="MyGraphic.java" height="500" width="300">
</applet>*/
OutPut: