How to Draw Dots in Java


In this example we will show how to use Thread and Applet classes to draw dots at random locations at specified interval in Java.

Source Code

package com.beginner.examples;

import java.applet.Applet;
import java.awt.Dimension;
import java.awt.Graphics;
 
public class DrawExample3 extends Applet{
 
	public void paint(Graphics g){
		
		Dimension d = getSize();
		//random locations
		int x = (int)(Math.random() * d.width);
		int y = (int)(Math.random() * d.height);
		g.fillRect(x, y, 10, 10);
        System.out.println("ok");
	}
}

Output:

ok

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments