Java Program to Display Several Dots on the Screen Continuously

This is a Java program to display several dots on the frame continuously.

Problem Description

We have to write a program in Java such that it creates a frame and displays dots at random positions continuously using paint methods of Graphics class. Dots on frame continuously increases until the user closes the frame.

Expected Input and Output

For drawing dots on the frame at random positions, we can have the following sets of input and output.

1. To test the drawing of dots at random positions: We expect a frame having a black background with the green dots at random positions.

2. To test if number of dots are increasing: It is expected that after some time, the number of dots on the frame continuously increases until the user closes the frame.

Problem Solution

1. Create a frame.
2. To draw dots continuously, repeatedly call function paint, until the frame has been closed, that is till the frame is visible.
3. Delay the function call of method paint using Thread.sleep() with duration as needed.
4. In method paint, generate two random points that lie within the limits of the frame using Math.random() method of java.lang.Math package.
5. Draw a point at the random point using method drawLine() of Graphics class.

advertisement
advertisement
Program/Source Code

Here is source code of the Java Program to draw dots at random positions continuously using Graphics class. The program is successfully compiled and tested using javac compiler on Fedora 30.

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.lang.Math;
  4. import java.awt.Graphics.*;
  5. class Infinite_Dots
  6. {
  7. 	//Driver function
  8. 	public static void main(String args[])
  9. 	{
  10. 		//Create a frame
  11. 		JFrame frame=new JFrame("Infinite Dots");
  12. 		frame.setSize(500,500);
  13. 		frame.getContentPane().setBackground(Color.black);
  14. 		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  15. 		frame.setVisible(true);
  16. 		/*Draw the dots on frame continuously using method paint 
  17.                   until the user closes the frame*/
  18. 		while(frame.isVisible())
  19. 		{
  20. 			paint(frame.getGraphics());
  21. 			try
  22. 			{
  23. 				//Delay by 1ms
  24. 				Thread.sleep(1);
  25. 			}
  26. 			catch(InterruptedException ie){}
  27. 		}
  28. 	}
  29. 	//function to draw a dot on the frame
  30. 	public static void paint(Graphics g)
  31. 	{
  32. 		g.setColor(Color.green);
  33. 		int x=(int)(Math.random()*1000)%500;
  34. 		int y=(int)(Math.random()*1000)%500;
  35. 		g.drawLine(x,y,x,y);
  36. 	}
  37. }
Program Explanation

1. A frame is created with background color as black.
2. The method paint is repeatedly called to draw a dot until the frame is closed.
3. We delay the call of method paint by 1ms using Thread.sleep() to visualize the addition of new dots on the frame.
4. In method paint we change the color of graphics to green by using method setColor(Color.color name) defined in the Graphics class.
5. Generate a random co-ordinate (x,y) such that it lies within the bounds of the frame.
6. Method drawLine(x1,y1,x2,y2) draws a line from point (x1,y1) to point (x2,y2). To draw a dot we replace the points (x1,y1) & (x2,y2) by (x,y), that is the start and end positions of line is same as the randomly generated (x,y) co-ordinate.

Note: Join free Sanfoundry classes at Telegram or Youtube
Runtime Test Cases

Here’s the run time test cases to draw dots at several positions continuously.

Test case 1 – Here is the runtime output of the Java program to test the drawing dots at random positions. The program displays a frame having a black background with the green dots at random positions.
java-program-display-several-dots-1

Test case 2 – Here is the runtime output of the Java program to test if number of dots have increased. The program displays a frame having a black background with the green dots at random positions. After some time, the number of dots on the frame continuously increases until the user closes the frame.
java-program-display-several-dots-2

advertisement

Sanfoundry Global Education & Learning Series – Java Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.