Tuesday, 22 October 2013

JAVA APPLET PROGRAMMING - -

JAVA APPLET PROGRAMMING

Program Cycle vs Car racing via Java Applets

Program Code:-

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

/*<applet code="applet" height=500 width=1000>
</applet> */

public class applet extends Applet
 {

   int x,y,k,h,w,n;
   public void init()
     {
            h=getHeight();
            w=getWidth();
            n=0;k=0;
           setBackground(Color.cyan);
      }  
   public void paint(Graphics g)
    {
           int i,m;
           g.setColor(Color.red);
           g.drawString("Cycle vs Car Racing Championship 2013",w/2-70,50);
           g.setColor(Color.gray);
           g.fillRect(0,h/2-60,w,120);
           m=0;
           for(i=0;m<=w;i++)
           {   
                 g.setColor(Color.white);
                 g.fillRect(m,h/2-7,80,14);
                 m+=120;  
           }
           g.setColor(Color.red);
           g.fillRect(100,h/2-60,7,120);
           g.fillRect(w-50,h/2-60,7,120);

           g.setColor(Color.black);
           g.drawOval(n,h/2+25,30,30);
           g.drawOval(n+13,h/2+38,4,4);
           g.drawOval(n+46,h/2+36,8,8);
           g.drawOval(n+48,h/2+38,4,4);
           g.drawLine(n+15,h/2+40,n+50,h/2+40);
           g.drawLine(n+15,h/2+42,n+50,h/2+44);
           g.drawLine(n+15,h/2+38,n+50,h/2+36);
           g.drawOval(n+70,h/2+25,30,30);
           g.drawOval(n+84,h/2+39,2,2);
           g.drawLine(n+15,h/2+40,n+30,h/2+20);
           g.drawLine(n+30,h/2+20,n+50,h/2+40);
           g.drawLine(n+50,h/2+40,n+70,h/2+20);
           g.drawLine(n+30,h/2+20,n+70,h/2+20);
           g.drawLine(n+70,h/2+20,n+85,h/2+40);
           g.drawLine(n+70,h/2+20,n+70,h/2+15);
           g.drawLine(n+65,h/2+13,n+75,h/2+17);
           g.drawLine(n+65,h/2+13,n+62,h/2+13);
           g.drawLine(n+72,h/2+17,n+75,h/2+17);
           g.drawLine(n+30,h/2+20,n+30,h/2+15);
           g.drawLine(n+26,h/2+15,n+34,h/2+15);
           g.fillRect(n+26,h/2+10,8,6);
           g.fillRect(n+27,h/2+6,6,4);
           g.drawLine(n+26,h/2+10,n+75,h/2+17);
                     /*Car Making*/
           Color c=new Color(200,60,200);
           g.setColor(c);
           g.fillRoundRect(k,h/2-30,100,20,5,5);
           Color c1=new Color(20,160,200);
           g.setColor(c1);
           g.fillOval(k+10,h/2-20,20,20);
           g.fillOval(k+70,h/2-20,20,20);
           g.drawRect(k+20,h/2-45,60,15);
           g.drawLine(k+50,h/2-45,k+50,h/2-30);
           g.drawLine(k+20,h/2-45,k+10,h/2-30);
           g.drawLine(k+80,h/2-45,k+90,h/2-30);
           g.setColor(Color.red);
           g.drawLine(100,h/2-70,100,h/2-120);
           g.drawString("Start",100,h/2-150);
                     /* Creating Flags*/
           int q[]={100,130,100};
           int e[]={h/2-120,h/2-110,h/2-100};
           g.fillPolygon(q,e,3);
           g.drawLine(w-50,h/2-60,w-50,h/2-120);
           g.drawString("End",w-50,h/2-150);
           int t[]={w-50,w-20,w-50};
           int y[]={h/2-120,h/2-110,h/2-100};
           g.fillPolygon(t,y,3);
           n+=2;
           k+=3;

           try
           { Thread.sleep(100);
            }
            catch(Exception ex)
            {
             }
           if(k+100<w-50)
           {repaint();}
         
       }
}

Output:-
   



Download the complete code:- Applet1.rar 

No comments:

Post a Comment