5, ఏప్రిల్ 2012, గురువారం

THIS IS MY CAR ANIMATION IN JAVA WHICH HAS BEEN CONSTRUCTED BY USING APPLETS CONCEPTS TO RUN THIS PROGRAM LIKE A NORMAL APPLET PROGRAM


import java.awt.*;
import java.util.*;
import java.applet.*;
/* <APPLET CODE="ani.class" WIDTH=400 HEIGHT=300></APPLET>*/
//The basic applet class.The applet shows 4 cars crossing each other at a square.
public class ani extends Applet implements Runnable
{
 Thread t;
 //4 variables used to vary the car's positions.
 int x1=0,x2=380,y1=50,y2=250;
 public void start()
 {
  if(t==null)
  {
   t=new Thread(this,"New Thread");//New side Thread created on start of applet.
   t.start();
  }
 }
 public void stop()
 {
  if(t!=null)
  {
   t=null;//On stop of applet the created thread is destroyed.
  }
 }
 //Implementation of method run() of Runnable interface.
 public void run()
 {
  Thread t1=Thread.currentThread();
  while(t==t1)
  {
   repaint();
   try
   {
    Thread.sleep(100);
   }
   catch(Exception e)
   {   }
  }
 }
 public void paint(Graphics g)
 {
  setBackground(Color.cyan);
  g.setColor(Color.BLACK);
  x1=(x1+16)%400;
  x2=x2-16;
  y1=(y1+12)%300;
  y2=y2-12;
  if(y2<0)
   y2=288;
  if(x2<0)
   x2=384;
  //Draw the roads using 2 filled rectangles using black color.
  g.fillRect(0,130,400,40);
  g.fillRect(180,0,40,305);
  //Draw the white colored lines.
  g.setColor(Color.white);
  for(int i=0;i<20;i++)
  {
   if(i!=9 && i!=10)
    g.drawLine(i*20,150,i*20+10,150);
  }
  for(int j=0;j<15;j++)
  {
   if(j!=7 && j!=8)
    g.drawLine(200,j*20,200,j*20+10);
  }
  //Draw 4 colored cars using filled round rectangles.
  g.setColor(Color.red);
  g.fillRoundRect(x2,152,20,8,2,2);
  g.fillRoundRect(x1,140,20,8,2,2);
  g.fillRoundRect(190,y1,8,20,2,2);
  g.fillRoundRect(202,y2,8,20,2,2);
 }
}

Output:-
The output on applet fame it will display a four way junction one each road one car will be running


 program-2

THIS IS MY SIMPLE CAR WAS DESIGNED TO ILLUSTRATE THE CONCEPT OF THE SWING IMPLEMENTATION FOR APPLET
IT IS SO SIMPLE JUST FIRST YOU DRAW A RECTANGLE AND ATTACH 4 CORNERS S 4 CIRCLES FILL WITH ALL THESE 5 COMPONENTS WITH U R DESIRED COLORS IT IS NOT DESIGNED BY USING THREAD WHICH WILL GIVE U ANIMATION AFFECT
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
public class test extends JApplet {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new test());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1200,600);
frame.setVisible(true);
}
public void paint(Graphics g) {
try {
int j = 0;
while (j < 1500) {
g.setColor(Color.blue);
g.fillRect(0, 0, 5000, 5000);
g.setColor(Color.yellow);
g.fillRect(250 + j, 250, 100, 50);
g.setColor(Color.red);
g.fillOval(260 + j, 300, 20, 20);
g.fillOval(320 + j, 300, 20, 20);
j++;
Thread.sleep(2);
}
} catch (InterruptedException e) {
}}}
Running of the above code
Run this application like a simple core java program a car shape will move on the screen from left to right only once

కామెంట్‌లు లేవు:

కామెంట్‌ను పోస్ట్ చేయండి