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 .
Dhaval says
When i’m executing the code . It is not showing the Output .
ProjectsGeek says
Please provide the error you are getting while running this project. So that we can help you.
bhavna says
itz running but ball is not displaying in the applet
ProjectsGeek says
@Bhavna. Please post error message that you are getting, So that we can help you out in running this code.
Shobana says
hello bhavna,how r u. i’m fine.. 🙂
niji rajan says
very useful.
Pavan says
One problem with ,,,,it display the following errors,,,exception in thread “main” java.lang.no suchmethod error :main ,,…plz help mi,..
ProjectsGeek says
You need to create one main method in some other class say demo to run this thread.
Sahiti says
After compiling when i run the code, the status bar is showing that applet is not initialized.please help.
ProjectsGeek says
I think problem is due to browser java security. Please check your browser java settings and get back to us if that doesn’t works.
Midhun says
Hi Sahitri,
I also exp the same prblm when i tried to run this project. Copy and paste the code to any IDE you use and it will show the errors. in line 6, it will ask you to rename the file to Balls.java. The issue will be fixed after you rename the file.
ProjectsGeek says
Yes if java class is public class you need to name the file to same name. eg if class name is Balls ans its public class you file name should Balls.java