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.

advertisement

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.

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.

advertisement
  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.

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

advertisement

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

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn