Moving Balls mini project using Java Applet
Write a program to develop a application in java applet which will show balls that will move across the screen .
In Moving Balls using Java Applet project we have two balls moving perpendicular to each other . Also balls have some sort of animation which will change it’s color a regular intervals . The code for this applet is simple and this code can be used as applet assignment in your academics .
Sample code for Applet Animated Balls that will move regularly across the screen :
import java.util.*; import java.applet.*; import java.awt.*; import java.awt.event.*; //Package Declarations public class balls extends Applet implements Runnable { Random r = new Random(); // Random number generation for Different Colours int x = 10, y = 10,sang1 = 0, sang2 = 0, he = 500 , wi = 500; public void init() { Thread t = new Thread(this); // Thread creation for animation t.start(); } public void run() { while(true) { try { repaint(); Thread.sleep(100); // Thread time interval set to 100 mil seconds if( x < wi - 100) // Playing with position of balls on the applet screen x += 5; if( y < he - 100) y += 5; if( x > wi - 100) x = wi - 100; if( y > he - 100) y = he - 100; sang1 += 10; sang2 += 10; } catch(Exception e) { } } } public void paint(Graphics g) { Dimension d = getSize(); he = d.height; wi = d.width; g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255))); // Balls random colors sets g.fillArc(x,20,100,100,sang1,90); g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255))); g.fillArc(x,20,100,100,sang1 + 90,90); g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255))); g.fillArc(x,20,100,100,sang1 + 180,90); g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255))); g.fillArc(x,20,100,100,sang1 + 270,90); g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255))); g.fillArc(10, y, 100, 100, sang2 ,90); g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255))); g.fillArc(10,y,100,100, sang2 + 90,90); g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255))); g.fillArc(10,y,100,100,sang2 + 180,90); g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255))); g.fillArc(10,y,100,100,sang2 + 270,90); } }
Now if you want to Change the speed of the Balls moving speed just change this Line …
repaint(); Thread.sleep(100);
Increase limit of sleep parameter for say Thread.sleep(400) to decrease speed or for increasing speed decrease parameter value .
To set the size of screen of applet just change values of he and wi in Above Code .
Sharmely says
Really this such a nice website for all types of projects…
It helped me a lot…:-)
Thank you Projects geek..:)
thekeeper says
hey! whenever i run this code(thru appletviewer), it says incorrect value of width and height. Please help!
ProjectsGeek says
Why don’t you try in Eclipse or Netbeans. Just give a try in these IDE .
safeena says
i executed this code and there was no errors bt how to display the output ….? i’m using apletviewer bt i’m nt getting the applet window. how to run this program?….. suggest me
ProjectsGeek says
@SAFEENA..I checked code and it is working without a Flaw. You just need to create a balls.java file in Eclipse or netbeans and then simply paste this code into balls.java file. Compile and run the file.
bhanu says
dude u declared balls as public class but u never used that in the program….its not compiling…can u help me…
ProjectsGeek says
@Bhanu …can you give us more details like….error message you are getting, So that we can help you out.
Pranav says
Here is error
Balls.java:6: class balls is public, should be declared in a file named balls.java
public class balls extends Applet implements Runnable
^
1 error
plz fix it… Thank You
ProjectsGeek says
@PRANAV. You need to paste the code inside file with name balls.java only. As the class is public it must be inside same file name file.
Priya Barse says
these website is very good for searching any type of project
ProjectsGeek says
Thanks priya ..!!
Java Projects