Open In App

How to Fill Colors in Shapes using Applet?

Last Updated : 23 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

  • Firstly, we need to import all the essential packages like AWT and Applet.
  • Once the packages have been imported, we need to define the class that implements the ActionListener interface.
  • Then we need to initialize all the UI components like buttons, Font, and Color.
  • We will customize these components with different functions like setFont(), setBackground(), etc. We will apply different colors to the buttons.
  • Then, we will define the actionPerformed() method, which will be responsible for repainting the applet to display the shape.
  • In the paint() function, we are drawing the shape as per your choice like a circle, square, etc. Then, there will be a user-defined function that will be used to generate the random color and fill the shape with this color.

Creating and Running the Applet

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

PS

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.

HTML




<!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




// 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

Applet

Explanation of the Program

  • We have first, imported all the essential packages using the import statement. This package includes the main Applet and AWT components for GUI creation,
  • Then we have defined the class names as ShapeApplet. This class implements the ActionListener interface.
  • Then we have defined the int() method, which will be used to create the UI components like Buttons, colors, and fonts in the application.
  • We have customized the GUI components using different customization properties.
  • Then as the user clicks on the specific button, the action to perform the shape and color creation is handled in the actionPerformed() function.
  • In the paint() function, the actual shape creation is done using different functions. Here, we are creating the shape as Rectangles, Circles, and Squares.
  • Then there is a user-defined function as generateRandomColor() which is used to generate the RGB random color on every click on the button.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads