Sunday 10 February 2013

Animated car in Java

//save the following source code as  car.java 


import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.geom.*;
import java.applet.*;


public class car extends JFrame
{
 
  Point2D.Float p1;
  GeneralPath start;
  public car()
    {
      super("Drawing 2D shapes");

      setBackground(Color.green);
      setSize(600,500);
     
      show();
     
    }
 

  public void paint(Graphics g)
    {
      

       Graphics2D g2d=(Graphics2D)g;
      
      
       p1=new Point2D.Float(100.0f,100.0f);
       start=new GeneralPath();

      

      

       start.moveTo(p1.x,p1.y);
       start.lineTo(p1.x+50.0f,p1.y-5);

       p1=(Point2D.Float)start.getCurrentPoint();
       start.lineTo(p1.x+10,p1.y-40.0f);

       p1=(Point2D.Float)start.getCurrentPoint();
       start.lineTo(p1.x+50.0f,p1.y-5);

       p1=(Point2D.Float)start.getCurrentPoint();
       start.lineTo(p1.x+5.0f,p1.y+20.0f);

       p1=(Point2D.Float)start.getCurrentPoint();
       start.lineTo(p1.x+5.0f,p1.y+20.0f);

       p1=(Point2D.Float)start.getCurrentPoint();
       start.lineTo(p1.x+30,p1.y+5.0f);

       p1=(Point2D.Float)start.getCurrentPoint();
       start.lineTo(p1.x,p1.y+40.0f);

       p1=(Point2D.Float)start.getCurrentPoint();
       start.lineTo(p1.x-150,p1.y);

       start.closePath();
      
       g2d.setPaint(Color.red);

       g2d.fill(start);

       float w1=50.0f,h1=40.0f;
      
       Point2D.Float p1=new Point2D.Float(5.0f,30.0f);
       Ellipse2D.Float r1=new Ellipse2D.Float(p1.x,p1.y,w1,h1);

       g2d.setPaint(Color.red);
       g2d.draw(r1);
       g2d.translate(110,80);
      
       g2d.setPaint(Color.blue);
       g2d.fill(r1);
     
       g2d.translate(80,0);
       
       g2d.setPaint(Color.blue);
       g2d.fill(r1);

       for(int i=1;i<=4;i++)
         { 
            
             g2d.translate(-10,-80);
             //g2d.setPaint(Color.red);
             g2d.setColor(new Color((int) (Math.random()*256),(int) (Math.random()*256),(int) (Math.random()*256)));
             g2d.fill(start);
            
             g2d.setColor(new Color((int) (Math.random()*256),(int) (Math.random()*256),(int) (Math.random()*256)));
             g2d.translate(100,80);
             //g2d.setPaint(Color.blue);
             g2d.fill(r1);
            
             g2d.translate(80,2);
             g2d.fill(r1);
            
        }          
   }

public static void main(String args[])
   {
     car st=new car();
    
     st.addWindowListener(new WindowAdapter()
         {
           public void windowClosing(WindowEvent e)
             {
                System.exit(0);
             }
          }
         );
    }



 }


0 comments:

Post a Comment