applet

Draw Image in Applet

Photo of Ilias TsagklisIlias TsagklisNovember 11th, 2012Last Updated: October 20th, 2013
0 326 1 minute read

With this example we are going to demonstrate how to draw an image in an Applet. A Java applet is a special kind of Java program that a browser enabled with Java technology can download from the internet and run. An applet is typically embedded inside a web page and runs in the context of a browser. An applet must be a subclass of thejava.applet.Applet class. TheApplet class provides the standard interface between the applet and the browser environment. In short, to draw an image in an Applet you should:

  • Create a class that extends theApplet, such asDrawImageInApplet class in the example.
  • Useinit() API method of Applet. This method is called by the browser or applet viewer to inform this applet that it has been loaded into the system. In this method call thegetImage(URL url, String name) API method of Applet to get anImage object that can then be painted on the screen.
  • Inpaint(Graphics g) method calldrawImage(Image img, int x, int y, ImageObserver observer) API method ofGraphics to draw as much of the specified image as is currently available.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;import java.applet.Applet;import java.awt.Graphics;import java.awt.Image;public class DrawImageInApplet extends Applet {private static final long serialVersionUID = 2530894095587089544L;private Image image;// Called by the browser or applet viewer to inform// this applet that it has been loaded into the system.    public void init() {        image = getImage(getDocumentBase(), "http://www.myserver.com/image.jpg");        }        // Paints the container. This forwards the paint to any    // lightweight components that are children of this container.    public void paint(Graphics g) {        // draws as much of the specified image as is currently available    g.drawImage(image, 0, 0, this);        }}

 
This was an example of how to draw an image in an Applet in Java.

Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Ilias TsagklisIlias TsagklisNovember 11th, 2012Last Updated: October 20th, 2013
0 326 1 minute read
Photo of Ilias Tsagklis

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor atJava Code Geeks.

Related Articles

Bipartite Graph

Play audio in Applet

November 11th, 2012
Bipartite Graph

Applet lifecycle methods

November 11th, 2012
Bipartite Graph

Get an applet parameter

November 11th, 2012
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.