Javatpoint Logo
Javatpoint Logo

Applet Program in Java to Draw House with Output

Java is a flexible programming language that lets in developers to create a huge range of applications, from easy command-line programs to complicated web applications. One thrilling thing of the Java is its capability to create the graphical applications, and one manner to do that is by way of using the Java Applets. In this section, we can manual you via growing a simple Java Applet application to draw a primary house.

Java Applets

A Java Applet is a special kind of Java program that is designed to be run within a web browser. Unlike standalone Java programs, that are performed outdoor the browser, applets are embedded in web pages and run within the browser's Java Virtual Machine (JVM). It allows us to create interactive and graphical content that can be displayed on websites.

Drawing a House in Java

Step 1: Set Up the Applet

First, we need to create a Java class that extends the java.Applet.Applet class and overrides the paint method. The method is answerable for drawing the pictures on the applet's window. Here's the preliminary setup of the applet:

HouseApplet.java

Step 2: Draw the House Structure

Now, permit's add the code to draw the basic shape of the house. We will use easy geometric shapes like rectangles to symbolize the house, roof, door, and home windows. Here's the code to draw the house structure:

In this code, we use the drawRect approach to draw rectangles representing the house, roof, door, and home windows. The drawLine technique is used to create the roof's triangular shape.

Step 3: Compile and Run the Applet

We have written the code to draw the house, we need to assemble the Java document after which run it as an applet in a web browser. Here's how we can collect the Java code:

After compiling the code, we must see a HouseApplet.class document in the same directory.

Step 4: Create an HTML Page for the Applet

To run the applet in a web browser, we will create an HTML page that embeds the applet. Here's an instance HTML web page:

In this HTML code, we specify the applet's class name and dimensions. If the user's browser supports Java applets, the applet may be displayed. Otherwise, the furnished message could be shown.

Step 5: View the Output

Now, open the HTML page in an internet browser. We have to see a simple house drawing displayed at the web page. The applet will render the house structure based totally at the code we supplied inside the paint method of the Java class.

Applet Program in Java to Draw House with Output

In Conclusion, we have walked you through developing a simple Java Applet application to draw a basic house. Java Applets allows us to create interactive and graphical content material within a web browser, making them a useful tool for web builders.

Creating a house drawing applet is simply the beginning. We can explore and enlarge in this assignment with the aid of including extra info to the house, enforcing interactivity, or even developing greater complex graphical applets. Java's rich library of graphical features and the capability to embed applets in web pages offer a great platform for creating engaging web content material. So, now which we have successfully created an easy house drawing applet in Java, we could use this as a foundation to explore and create extra interesting graphical programs.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA