CrazyEngineers
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Ashutosh_shukla

    MemberNov 30, 2008

    The java file code is as follows:
    It shows 4 cars crossing a square.
    import java.awt.*;
    import java.util.*;
    import java.applet.*;
    /*
     <APPLET CODE="Animation.JAVA" WIDTH=400 HEIGHT=300>
     </APPLET>
    */
    //The basic applet class.The applet shows 4 cars crossing each other at a square.
    public class Animation 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);
     }
    }
    
    Are you sure? This action cannot be undone.
    Cancel
  • lano51

    MemberJan 8, 2009

    i take this code and try to run it on java but it doesnot run since it doesnot contain the main method that runs the program please can you send me the runnable code.thanks
    Are you sure? This action cannot be undone.
    Cancel
  • Ashutosh_shukla

    MemberJan 8, 2009

    Hey it is an applet you don't have to run it as Java application
    Instead you have to view it using the appletviewer
    Are you sure? This action cannot be undone.
    Cancel
  • rosario.n

    MemberJan 9, 2009

    😕 i also tried that program.there is error in the class declaration.


    Are you sure? This action cannot be undone.
    Cancel
  • Ashutosh_shukla

    MemberJan 9, 2009

    Hey you save it as Animation.java and try again if it does not work out still send me actual error i will see because it runs perfectly on my pc
    Are you sure? This action cannot be undone.
    Cancel
  • lano51

    MemberJan 9, 2009

    hi i try again to run this applet but their is an error 😔 please send me step by step what should i do inorder to run this applet please reply as soon as possible as you can.thanks
    Are you sure? This action cannot be undone.
    Cancel
  • Ashutosh_shukla

    MemberJan 10, 2009

    Hey listen you copy the code and store it in a file Animation.java
    Next thing is to compile it with the jdk and then kepp the .class file in folder of bin. Now on comand prompt type
    appletviewer Animation
    Are you sure? This action cannot be undone.
    Cancel
  • lano51

    MemberJan 12, 2009

    hi again i face another problem i try to apply what you wrote but i donot know what is meant by jdk and the .class i donot have it, more over where is the location of the bin in the computer .
    i create a file and name it animation.java but the rest of the steps i donot know how to apply it .so please send me the code in a zip file on my mail :
    #-Link-Snipped-#
    thank you very much for helping me 😁
    Are you sure? This action cannot be undone.
    Cancel
  • Ashutosh_shukla

    MemberJan 12, 2009

    Hey listen you don't know jdk i mean it is same as the java installed on your machine.It has a bin folder and on compilation of a .java file you get a .class file created.Now simply you have to put this file in your bin and then compile it then you will get a Animation.class file.Then simply you have to use appletviwer command to view the applet.The bin folder contains an application file appletviewer.You need to go to command prompt and then write this command appletviewer Animation
    Thats it it will run I guess you have never used java and compiled programs on command line interpreter. Try it once again i have already given you the code you just need to execute this steps properly
    Are you sure? This action cannot be undone.
    Cancel
  • rosario.n

    MemberJan 23, 2009

    hey..i tried again.there is no error..but,i didn't get the car image..😕
    Are you sure? This action cannot be undone.
    Cancel
  • shalini_goel14

    MemberFeb 1, 2009

    rosario.n
    hey..i tried again.there is no error..but,i didn't get the car image..😕
    Hey rosario,

    This code is working absolutely fine for me. Well done Mr. Ashutosh 😀

    @rosario: Did you get the applet window? If not even that, follow the steps told by Mr. Ashutosh to run the program.😀
    Are you sure? This action cannot be undone.
    Cancel
  • rama_krish627

    MemberFeb 6, 2009

    Ya it's working ya. I got a moving cars in red color.
    Are you sure? This action cannot be undone.
    Cancel
  • zia.sepsis

    MemberAug 1, 2009

    lano51
    hi again i face another problem i try to apply what you wrote but i donot know what is meant by jdk and the .class i donot have it, more over where is the location of the bin in the computer .
    i create a file and name it animation.java but the rest of the steps i donot know how to apply it .so please send me the code in a zip file on my mail :
    #-Link-Snipped-#
    thank you very much for helping me 😁
    Jdk stands for java developing kit... which is an open source software developed by sun microsystem. only with the help of jdk the java source code is compiled into java byte code with the extension of (.class). the command instructing JDK to compile is "javac".
    do the following:
    1.Copy the above source code and paste it in a notepad.
    2.save it with the "class name" i.e."Animation" with an extension (.java) (note: java is case sensitve)
    3.For instance your saving this in a folder named "david" in your "c:", now open your "cmd" and type as following.
    c:\documents and settings\user name> cd\
    c:\> cd david
    c:\david>
    " you have located the desired directory now"
    c:\david> javac Animation.java
    c:\david>
    (this denotes successfull execution of the program)
    (please be sure that you have already installed jdk)
    4. now "javac" instructs JDK to compile the given source code in java byte code with an extension of (.class). this compiled code will be saved in the same folder.
    5. applets cannot be run'd as application because it doesn't hold's any main method. so it has to done by using appletviewer.
    6. In order to run an applet you have to write an HTML document and include the applet into that.
    <html>
    <head>
    <title> Display name applet </title>
    </head>
    <body>
    <h1 align=center> Name web applet</h1>
    <center>
    <applet name="Animation" code="Animation.class" width=200 height=100></applet>
    </center>
    </body>
    </html>
    7. save it with an extension (.html)....in the folder david.
    8. now--->
    c:/david>appletviewer Animation.html
    Are you sure? This action cannot be undone.
    Cancel
  • zia.sepsis

    MemberAug 1, 2009

    Its working.....😎
    Are you sure? This action cannot be undone.
    Cancel
  • spp

    MemberAug 1, 2009

    i tried this code but its not working.
    it showing an error that applet not initialized.
    so can u please check this out
    Are you sure? This action cannot be undone.
    Cancel
  • zia.sepsis

    MemberAug 1, 2009

    spp
    i tried this code but its not working.
    it showing an error that applet not initialized.
    so can u please check this out
    Have you installed JDK?????
    Are you sure? This action cannot be undone.
    Cancel
  • spp

    MemberAug 1, 2009

    zia.sepsis
    Have you installed JDK?????
    yah i have installed jdk1.6
    Are you sure? This action cannot be undone.
    Cancel
  • zia.sepsis

    MemberAug 1, 2009

    spp
    yah i have installed jdk1.6
    Try this:
    c:\foldername>set path="directory of the jdk's bin";
    eg:
    "set path=c:\program files\java\jdk.1.5.0-06\bin;"
    Are you sure? This action cannot be undone.
    Cancel
  • spp

    MemberAug 1, 2009

    yah i have already set that path
    other applet applications are working but this one not
    Are you sure? This action cannot be undone.
    Cancel
  • Srikz

    MemberAug 20, 2009

    Actually, inside the program the <applet> code must contain the class file and not the java file.
    Once you change it, it'll work.
    i.e <APPLET CODE="Animation.class" WIDTH=400 HEIGHT=300> </APPLET>

    It Worked for me !!
    Are you sure? This action cannot be undone.
    Cancel
  • sookie

    MemberAug 21, 2009

    Hi Srikz,

    That program works if I use neither .java nor .class

    Step # 1: Create a folder named "I:\JavaSrcCode" in your local
    Step # 2: Open notepad, copy the program in this thread in it and save it as Animation.java in I:\JavaSrcCode folder. Also edit Animation.JAVA to simply Animation in Animation.java file.So final file would look like this
    import java.awt.*;
    import java.util.*;
    import java.applet.*;
    /*
     <APPLET CODE="Animation" WIDTH=400 HEIGHT=300>
     </APPLET>
    */
    //The basic applet class.The applet shows 4 cars crossing each other at a square.
    public class Animation 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);
     }
    }
    
    Step #3: Now open command prompt. Switch to your I:\JavaSrcCode directory and enter following command. It will create Animation.class file in the same folder "I:\JavaSrcCode" folder.
    javac I:\JavaSrcCode\Animation.java
    [​IMG]

    Step 4: Now enter following command
    appletviewer Animation.java
    [​IMG]

    You will get applet window finally.

    [​IMG]

    It has nothing to do with .java and .class until or unless you are keeping your .java and .class files at the same location. Better keep them at same place to avoid any compilications. For large projects only we should keep .class files separately.

    Thanks !
    Are you sure? This action cannot be undone.
    Cancel
  • Srikz

    MemberAug 22, 2009

    Hey sookie,
    it's just that i happened to compile few other java programs using the .java extenstion in the applet code, which just failed. But it'd work along fine with the class extension. Anyhow, i'll try your method and let you know. Thanks 😀
    Are you sure? This action cannot be undone.
    Cancel
  • HImanshu_Sharma

    MemberMar 23, 2010

    i haven't tried bt i'l actually i had an another programe of this moving car,,,i made myself..so think i can sort out your prob...
    Are you sure? This action cannot be undone.
    Cancel
  • Kavithak

    MemberAug 12, 2014

    Ya ..it works...Thanks a lot..
    Are you sure? This action cannot be undone.
    Cancel
  • v.gowtham

    MemberOct 9, 2014

    in my pc,at the applet page,on the bottom (start:applet not initialised)is displaying whats wrong...........?pls help me......!
    Are you sure? This action cannot be undone.
    Cancel
  • Bharath0bk

    MemberAug 14, 2015

    I/O exception while reading: D:\Mycode\Animation (The system cannot find the fil e specified)
    Are you sure? This action cannot be undone.
    Cancel
  • Bharath0bk

    MemberAug 14, 2015

    I/O exception while reading: D:\Mycode\Animation (The system cannot find the fil e specified)

    i am getting this error?
    Are you sure? This action cannot be undone.
    Cancel
  • Aaditya Karankot

    MemberFeb 12, 2016

    D:\>AppletViewer a.html
    load: class Animation.JAVA not found.
    java.lang.ClassNotFoundException: Animation.JAVA
    at sun.applet.AppletClassLoader.findClass(AppletClassLoader.java:211)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.applet.AppletClassLoader.loadClass(AppletClassLoader.java:144)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
    at sun.applet.AppletClassLoader.loadCode(AppletClassLoader.java:662)
    at sun.applet.AppletPanel.createApplet(AppletPanel.java:785)
    at sun.applet.AppletPanel.runLoader(AppletPanel.java:714)
    at sun.applet.AppletPanel.run(AppletPanel.java:368)
    at java.lang.Thread.run(Thread.java:662)
    Are you sure? This action cannot be undone.
    Cancel
  • Aashish Chugh

    MemberMay 30, 2016

    Yeah nice Coding ..
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register