Q:

An Applet Program That Print Random Dot in Java

0

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

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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:

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
<< Bouncing Ball Program in Java Using Applet...