This is a Java program to display several dots on the frame continuously.
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.
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.
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.
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.
import javax.swing.*;
import java.awt.*;
import java.lang.Math;
import java.awt.Graphics.*;
class Infinite_Dots
{
//Driver function
public static void main(String args[])
{
//Create a frame
JFrame frame=new JFrame("Infinite Dots");
frame.setSize(500,500);
frame.getContentPane().setBackground(Color.black);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
/*Draw the dots on frame continuously using method paint
until the user closes the frame*/
while(frame.isVisible())
{
paint(frame.getGraphics());
try
{
//Delay by 1ms
Thread.sleep(1);
}
catch(InterruptedException ie){}
}
}
//function to draw a dot on the frame
public static void paint(Graphics g)
{
g.setColor(Color.green);
int x=(int)(Math.random()*1000)%500;
int y=(int)(Math.random()*1000)%500;
g.drawLine(x,y,x,y);
}
}
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.
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.
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.
Sanfoundry Global Education & Learning Series – Java Programs.
- Apply for Computer Science Internship
- Practice BCA MCQs
- Practice Information Technology MCQs
- Check Java Books
- Apply for Java Internship