Open In App

How to Fill Colors in Shapes using Applet?

In this article, we will fill the random attractive colors in shapes like rectangles, circles, and squares using Applet. Here, we will have three buttons rectangle, circle, and square. When the user clicks on the button the shape will get filled with random color on every click.

Approach to the Program

Creating and Running the Applet

The folder view of the project is shown in the below image.



Step 1: First, create the file as ShapeApplet.java and enter the code into it.



Step 2: Once the code is ready we need to create the ShapeApplet.html file, through which we will display the output.




<!DOCTYPE html>
<html>
<head>
    <title>Shape Applet</title>
</head>
<body>
    <applet code="ShapeApplet.class" width="300" height="300">
    </applet>
</body>
</html>

Step 3: Then we need to compile the Java code using the below command:

javac ShapeApplet.java

Step 4: Now finally we need to run the application using the below command:

appletviewer ShapeApplet.html

Program to fill different colors in shapes

Implementation of the ShapeApplet program is mentioned below:




// Java Program to fill colors in Shapes using Applet
import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
  
// Driver Class
public class ShapeApplet
    extends Applet implements ActionListener {
  
    private String shapeSelect = "";
    private Color shapeColor = Color.WHITE;
  
    public void init()
    {
  
        // Creating buttons for each shape
        Button rectBtn = new Button("Rectangle");
        Button circleBtn = new Button("Circle");
        Button squareBtn = new Button("Square");
  
        // Setting font and background color for buttons
        Font btnFont = new Font("Arial", Font.BOLD, 20);
  
        Color rectColor = new Color(50, 100, 150);
        Color circleColor = new Color(150, 50, 100);
        Color squareColor = new Color(100, 150, 50);
  
        rectBtn.setFont(btnFont);
        circleBtn.setFont(btnFont);
        squareBtn.setFont(btnFont);
  
        rectBtn.setBackground(rectColor);
        circleBtn.setBackground(circleColor);
        squareBtn.setBackground(squareColor);
  
        // Adding action listeners to the shape buttons
        rectBtn.addActionListener(this);
        circleBtn.addActionListener(this);
        squareBtn.addActionListener(this);
  
        // Adding buttons to the applet
        add(rectBtn);
        add(circleBtn);
        add(squareBtn);
    }
  
    public void actionPerformed(ActionEvent e)
    {
  
        String cmd = e.getActionCommand();
  
        // Setying the selected shape
        shapeSelect = cmd;
  
        // Generating a random color for the shape
        shapeColor = generateRandomColor();
  
        // Repainting the applet to display the shape with
        // the new color
        repaint();
    }
    public void paint(Graphics g)
    {
  
        int X = getWidth() / 2;
        int Y = getHeight() / 2;
  
        // Drawing the selected shape with the chosen color
        g.setColor(shapeColor);
  
        if (shapeSelect.equals("Rectangle")) {
            g.fillRoundRect(X - 75, Y - 40, 150, 80, 20,
                            20);
        }
        else if (shapeSelect.equals("Circle")) {
            g.fillOval(X - 75, Y - 75, 150, 150);
        }
        else if (shapeSelect.equals("Square")) {
            g.fillRect(X - 75, Y - 75, 150, 150);
        }
    }
    // Method to generate a random color
    private Color generateRandomColor()
    {
  
        int red = (int)(Math.random() * 256);
        int green = (int)(Math.random() * 256);
        int blue = (int)(Math.random() * 256);
  
        return new Color(red, green, blue);
    }
}

Output for the program

Explanation of the Program


Article Tags :