Open In App

Java Program to Create Different Shapes using Applet

In this article, we will be creating different shapes using Applet. Here, we have taken an Input field, where the user can enter the name of the shape and click on the button. After clicking on the button, the shape that is entered will get drawn on the Applet window.

Overview

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 Create Different Shapes in Applet

Implementation of the ShapeApplet program is mentioned below:




// Java Program to create 
// Different 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.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
  
//Defining Class ShapeApplet
public class ShapeApplet extends Applet implements ActionListener {
    
    private String shapeName;
    private int centerX, centerY;
    private boolean drawShape = false;
    private TextField shapeInput;
    private Button drawButton;
    
    public void init() {
        
        // using absolute layout for proper element alignment
        setLayout(null);
        
        // setting background color
        setBackground(new Color(230, 230, 230));
        
        // creating input field for shape name
        shapeInput = new TextField(30);
        shapeInput.setFont(new Font("Arial", Font.PLAIN, 20));
        shapeInput.setBounds(50, 20, 300, 40);
        add(shapeInput);
        
        // creating button 
        drawButton = new Button("Draw Shape");
        drawButton.setFont(new Font("Arial", Font.BOLD, 20));
        drawButton.setBackground(new Color(50, 100, 150));
        drawButton.setForeground(Color.WHITE);
        drawButton.setBounds(380, 20, 150, 40);
        drawButton.addActionListener(this);
        
        add(drawButton);
    }
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == drawButton) {
            shapeName = shapeInput.getText().toLowerCase();
            drawShape = true;
            repaint();
        }
    }
    public void paint(Graphics g) {
        
        centerX = getWidth() / 2;
        centerY = getHeight() / 2;
        
        g.setFont(new Font("Arial", Font.BOLD, 20));
        g.setColor(Color.BLACK);
        g.drawString("Enter a shape name and click 'Draw Shape':", 10, 90);
        g.drawString("Available shapes: 'circle', 'rectangle', 'triangle', 'oval', 'square', 'pentagon'", 10, 120);
          
      if (drawShape) {
            switch (shapeName) {
                  
                // creating circle
                case "circle":
                    g.fillOval(centerX - 75, centerY - 75, 150, 150);
                    break;
                  
                // creating rectangle
                case "rectangle":
                    g.fillRect(centerX - 100, centerY - 50, 200, 100);
                    break;
                  
                // creating triangle
                case "triangle":
                    int[] xPoints = { centerX, centerX - 75, centerX + 75 };
                    int[] yPoints = { centerY - 75, centerY + 75, centerY + 75 };
                    g.fillPolygon(xPoints, yPoints, 3);
                    break;
                  
                // creating oval
                case "oval":
                    g.fillOval(centerX - 100, centerY - 50, 200, 100);
                    break;
                  
                // creating square
                case "square":
                    g.fillRect(centerX - 75, centerY - 75, 150, 150);
                    break;
                  
                // creating pentagon
                case "pentagon":
                    int[] pentagonX = {centerX, centerX + 75, centerX + 47, centerX - 47, centerX - 75};
                    int[] pentagonY = {centerY - 75, centerY - 25, centerY + 58, centerY + 58, centerY - 25};
                    g.fillPolygon(pentagonX, pentagonY, 5);
                    break;
                  
                default:
                    g.drawString("Invalid shape input. Try 'circle', 'rectangle', 'triangle', 'oval', 'square', or 'pentagon'.", 10, 240);
            }
        }
    }
}

Output for the program:

Explanation of the Program:


Article Tags :