Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
How to display images in Angular2 ?
Next article icon

How to Display Image using Applet?

Last Updated : 23 Oct, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will be using the Applet to display the image in proper layout. Here, we will display the image along with the text in the Applet Viewer.

Approach for Displaying Images Using Applet

  • We need to import the essential packages like Applet and AWT for customization.
  • When the packages get imported, then we need to create the class that implemented the ActionListener interface in Java.
  • After that, we have to initialize the UI component of the Image.
  • After initializing the component, we used the init() method in Java to load the image from the local storage. We will be displaying the GFGLOGO in the applet.
  • Then, we have to define the paint() method, which will help render the content inside the Applet window.
  • We will make the HTML file to run the applet.
  • We have to compile the Java code using Javac and then we need to run using appletviewer.

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 AppletImage.java and enter the code into it.

Step 2: Once the code is been inserted then we need to create the AppletImage.html file, through which we will display the output.

HTML
<!DOCTYPE html> <html> <head>     <title>Image Applet</title> </head> <body>     <applet code="AppletImage.class" width="300" height="300">     </applet> </body> </html> 

Step 3: We need to have the image which we need to display in the applet. Here, we have used the gfglogo.png which is present in the code directory.

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

javac AppletImage.java

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

appletviewer AppletImage.html

Program to Display Image using Applet

Implementation of the AppletImage program is mentioned below:

Java
// Java Program to display image using Applet  //Importing Necessary Packages import java.applet.Applet; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Image; import java.awt.Toolkit;  // Driver Class public class AppletImage extends Applet {     private Image img;     public void init() {         // Loading the image from a local file (gfglogo.png)         img = Toolkit.getDefaultToolkit().getImage("gfglogo.png");     }     public void paint(Graphics g) {         if (img != null) {             // Clearing the applet background initially             g.setColor(Color.WHITE);             g.fillRect(0, 0, getWidth(), getHeight());             // Drawing a border around the image             g.setColor(Color.BLACK);             g.drawRect(10, 10, getWidth() - 20, getHeight() - 20);             // Drawing the image, scaled to fit within the border             int widImg = img.getWidth(this);             int heiImg = img.getHeight(this);             if (widImg > 0 && heiImg > 0) {                 int x = 20;                 int y = 20;                 int maxWid = getWidth() - 40;                 int maxHei = getHeight() - 40;                 // Calculating the new dimensions to fit within the border                 int nWid, nHei;                 if (widImg <= maxWid && heiImg <= maxHei) {                     nWid = widImg;                     nHei = heiImg;                 } else {                     double wRatio = (double) maxWid / widImg;                     double hRatio = (double) maxHei / heiImg;                     double scale = Math.min(wRatio, hRatio);                     nWid = (int) (widImg * scale);                     nHei = (int) (heiImg * scale);                 }                 g.drawImage(img, x, y, nWid, nHei, this);             }             // Adding a title text             g.setColor(Color.BLACK);             g.setFont(new Font("SansSerif", Font.BOLD, 16));             g.drawString("GeeksforGeeks Logo", 20, getHeight() - 10);         }     } } 

Output for the program

Output

Explanation of the Program

  • We have imported the packages which are necessary to define the Applet and the Label to display the image name in the application.
  • Then we have defined the AppletImage class which is implementing the ActionListener interface.
  • There is an init() method, which is responsible for loading the gfglogo.png image from the local disk of the system.
  • We have defined the paint() method, which performs the entire functionality of displaying the image to the Applet. We have set the custom color to the Applet window, also, we have defined the dimensions in which the gfglogo will be displayed on the Applet window.
  • We have customized the appearance of the image by adding a border to it. Also, we have added the image title text using the drawString() method.

Next Article
How to display images in Angular2 ?

G

gauravgandal
Improve
Article Tags :
  • Java
  • Geeks Premier League
  • java-applet
  • Geeks Premier League 2023
Practice Tags :
  • Java

Similar Reads

  • How to Read a File using Applet?
    In this article, we will learn how to read the content of the file using Java and display those contents using Applet. Approach UsedThe user should type the name of the file in the input field that asks for the filename in the output applet window. If the file is already present, it will display the
    2 min read
  • How to Draw a Car using Java Applet?
    An Applet is a Java program that runs in a web browser. An Applet is a Java class that extends the java.applet.Applet class, which means that to create any Applet, you need to import this class and extend it in your Applet class. The applet does not have a main() method. Applets are designed to embe
    2 min read
  • How to Play Sound using Applet?
    In this article, we will be using the Applet window to play the sound or music. Here, we will have buttons like Play, Pause, and Restart. Using these buttons we will manage the sound in the Applet window. Approach for Playing Sound Using AppletWe need to import the essential packages like Applet and
    4 min read
  • 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 Prog
    4 min read
  • How to display images in Angular2 ?
    We can serve an image in angular2 by first placing the image in the assets directory of your project where you can create a specific directory for the images or just keep it in the assets directory as it is. Once you have put all the required images in the assets directory open the specific componen
    2 min read
  • How to Write to a File Using Applet?
    In this article, we will learn how to write a file using Applet and grant access to write files using Applet. ApproachThe applet output window contains three fields. Those are the file name field, Text area field, and save button field. File name field: It asks the user to enter the file name to wri
    3 min read
  • How to Play Audio File (.WAV) Using Java Applet?
    In this article, we will learn how to read and play audio (.wav) files using Java Applet. Approach for Playing Audio File(.wav) using AppletMusicPlayerApplet class inherits the property of Applet in Java. The interface consists of the input text field to enter a filename with an extension of the aud
    3 min read
  • How to Display Images using Handlebars in Node.js ?
    In this article, we will discuss how to display images using handlebars in Node.js. You may refer to this article for setting up handlebars View Engine in Node.js ApproachTo display images using Handlebars in Node.js, pass the image URLs to the template from your server. In the Handlebars template,
    3 min read
  • How to Create a Banner Using Applet?
    In this article, we shall be building a Java Applet that shows a scrolling banner with a message that moves continually from right to left. In this article, you will learn the fundamentals of Java Applet Basics by creating a banner using Applet. Note: java.applet package package has been deprecated
    6 min read
  • How to Use Swing Applet in Java?
    In this article, we will be using the swing Applet or JApplet in Java. Here, we will make a simple multiplication application that will multiply the two input numbers. Approach to Using Swing Applet in JavaWe need to import the packages for Swing and AWT Components.Once the packages are imported, we
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences