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
- Initially, we have to import all the necessary packages like AWT and Applet for customization and creation of UI.
- When the packages get imported, we need to define the class that extends from the Applet class and also implement the ActionListener interface.
- Then we have to initialize all the UI components like TextField, Button, etc, and apply those components on the Applet Window.
- We can also customize the appearance of these components by applying different Fonts, Background Colors, and more properties.
- Then, as per the user input, we have to define the switch cases, that will create the shape according to the input. Example: If the user enters the input as “Circle”. Then the code wrapped in the Circle Switch case will get executed and the shape of a Circle will be created.
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.
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 Create Different Shapes in Applet
Implementation of the ShapeApplet program is mentioned below:
Java
// 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:
- First, we have imported all the necessary packages that are required to create the Applet Window and the UI components like TextField, Buttons, Colors, etc.
- Then we have defined the class as ShapeApplet which extends the Applet Class and also implements the ActionListener interface.
- Then we have initiated all the UI components and by using the init() method we are adding those elements in the Applet Window. We have also customized the behavior of the components by adding the customization properties like setFont(), setBounds(), etc.
- As we are creating the shape once the button click event is been triggered, to manage the event we have used the function actionPerformed().
- There is a switch block that is used to create the color as per the user’s input in the TextField component.