Saturday, 4 November 2017

An Applet Program That Print Random Dot in Java

Problem:- Write an applet program in Java that display Random dot in screen or An applet program that displays random dot or Draw Dots at Random Locations in an Applet Example or java - Drawing random points in JApplet or java - 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.

Explanation:- This is a very interesting problem, your task is to print random dots on the screen. while you can also control the speed of dots and color of dots. Now come to point first you to copy this program and save with .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 CD command ie.- "cd Desktop". If you are getting problem how to run applet program in java via command link check this How to Run Java Applets and Swing Using Command Line. Now just open and run the program and see the magic.

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

1. public void init()
2. public void run()
3. Thread.sleep(1)
4. public void paint(Graphics g)
5. g.fillOval(x,y,10,10)
6. g.setColor(Color.red)


1. public void init()

public void init is used for 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 condition theirs. how many time 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 color and shape decide.

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

This is a property of filling a color in any shape here is an X-Axis and y is a Y-Axis and 10 and 5 is the size 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 coloring a shape.


Solution:- Draw Dots at Random Locations in an 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);          
    }

}

Output:-

Extreme Recommended:- Like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date. If you have any Query or Question you can ask in the group, I will Try To Solve your Query and try to answers of your Questions withing 24 Hours, You can also Email me or comment below Please suggest to your Friends to join and like our page and don't forget to Subscribe. Enter your Email and click to subscribe.

Ghanendra Yadav

Ghanendra Yadav

Hello, I Am Ghanendra Yadav Owner of This Blog, I am professional Blogger and Programmer. I Love Programming, Logo Making, And Banner Designing. My Highest Qualification is MCA From NIT Warangal. You Can Find Me On Social Media Through Below Link And If You Have Any Query Related To Programming And Other Subject Comment Below or You Can Mail Me I Will Try To Answer Within 24 Hours Email:- yghanendra@student.nitw.ac.in

Find me on Social Media

Facebook | Twitter | Google+ | RSS Feed

No comments:

Post a Comment