08/02/2023

Write a Program Draw Random DOT in Java Using Applet

Write a Program Draw Random DOT in Java Using Applet. Drawing random points in JApplet or java - The simplest way to display a window with dots on it or arrays - dot random movement in java or Random Dots or Java Random Number Examples or A program of the applet that draws a dot at a random location in its or An Applet Program That Print Random Dot in Java or Draw Dots at Random Locations in an Applet Example.

Program Draw Random DOT in Java Using Applet

Java Program Draw Random DOT Using Applet


import java.applet.Applet;
import java.awt.*;

/* Program By Ghanendra Yadav
    Visit http://www.programmingwithbasics.com/
*/
/*
        <applet code = "DRAWDOTSRANDOM" width = 600 height = 400>
        </applet>
*/
public class DRAWDOTSRANDOM extends Applet implements Runnable {
    Thread t;
    public void init() {
        t = new Thread(this);
        t.start();
    }

    public void run() {
        try {
            while (true) {
                repaint();
                Thread.sleep(1);
            }
        } catch (Exception e) {}
    }
    public void update(Graphics g) {
        paint(g);
    }

    public void paint(Graphics g) {
        Dimension d = getSize();
        int x = (int)(Math.random() * d.width);
        int y = (int)(Math.random() * d.height);
        g.setColor(Color.red);
        g.fillOval(x, y, 10, 10);
    }

}

Explanation of Draw Random DOT in Java Using Applet


This is a very interesting problem, while you can also control the speed of dots and the colour of dots. Now come to point first you to copy this program and save it with the .java extension, now press the Win+R key and enter CMD and press Enter. Now locate the program where you save it with the help of the CD command ie.- "cd Desktop". 

If you are getting problems with how to run the applet program in java via the command link check this How to Run Java Applet Program in CMD Using Appletviewer. Now just open and run the program and see the magic.

Now Come to the technical terms of this program ie. How drawing random points in JApplet is working. So basically there are 6 steps.

  • public void init()
  • public void run()
  • Thread.sleep(1)
  • public void paint(Graphics g)
  • g.fillOval(x,y,10,10)
  • g.setColor(Color.red)

1. public void init()


public void init is used for the initialization of the Thread, this is the first process.

2. public void run()


the public void run is responsible for the run the thread and catching if any exception occurs, you can put conditions theirs. how many times you want to run the Loop etc.

3. Thread.sleep(Integer Value)


Thread.sleep(1000) is used for sleep The Thread means no operation will be performed for a given time, here 1000 means 1 Second and 1 means 1/1000 Second. So for a given time, no operation will be performed and the program will be Idle for a given time.

4. public void paint(Graphics g)


Performing a painting and drawing task, here colour and shape decide.

5. g.fillOval(x, y, 10, 5)


This is a property of filling a colour in any shape here is an X-Axis and y is a Y-Axis and 10 and 5 are the sizes of an Oval(Height and Width) but in Applet X-Axis is Left to Right and Y-Axis is Top to Bottom.

6. g.setColor(Color.red)


This is a property of the colouring of a shape.

Program Draw Random DOT in Java Using Applet


Draw Dots at Random Locations in an Applet

Similar to Java Applet Projects


Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: