In the Following example PixelsApplet shows how to Fill Screen With Pixel in Applet windows Example and set color of an Applet window using setColor(),Random(), method of Graphics class for color the screen. Following example demonstrates how to Random setcolor with RGB values, for this you can use a Color object.
Here is the java code for the program PixelsApplet :
import java.awt.Graphics2D;
import java.util.Random;
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
public class PixelsApplet extends Applet
{
public static void main(String[] args)
{
PixelsApplet Pixels = new PixelsApplet();
Frame frame = new Frame("Fill Screen With Pixel in Applet windows Example");
frame.add(Pixels);
frame.setSize(350, 250);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e) {System.exit(0); });
}
public void paint(Graphics g)
{
g.setColor(Color.darkGray);
g.setFont(new Font("Arial",Font.BOLD,14));
g.drawString("Fill Screen With Pixel in Applet windows ", 30, 102);
g.setFont(new Font("Arial",Font.BOLD,10));
g.drawString("http://ecomputernotes.com", 200, 205);
Graphics2D Gr2D = (Graphics2D) g;
Gr2D.setColor(Color.green);
//set Random Pixel color.
Random Rand = new Random();
int redColor = Rand.nextInt(255);
int greenColor = Rand.nextInt(255);
int blueColor = Rand.nextInt(255);
Color clr = new Color(redColor, greenColor, blueColor);
g.setColor(clr);
//Loop for Display screen Pixel
for (int i = 0; i <= 99999; i++)
{
Dimension size = getSize();
int screen_width = size.width ; // Get Screen width Size
int screen_height = size.height; // Get Screen height Size
int x_postion = Math.abs(Rand.nextInt()) % screen_width;
int y_position = Math.abs(Rand.nextInt()) % screen_height;
Gr2D.drawLine(x_postion, y_position, x_postion, y_position);
repaint();
}
}
}