Wednesday, 22 July 2015

How to Display an Image in a Java Applet

I created a series of images of the numbers 1 through 9 by typing them in a Word document in a suitable font. Then I copied them from there into Microsoft Paint and saved them as JPEGs. Finally, I found an application online to trim them down to size. I wrote the following Java applet to display the number 1:

<applet code="DisplayNumbers" width=200 height=200>
import java.awt.*;
import java.applet.*;
public class DisplayNumbers extends Applet
  Image Number1;
  public void init()
    Number1 = getImage(getCodeBase(),"1r.jpg");
  public void paint(Graphics g)

The code at the start allowed me to run the applet using appletviewer but without needing to create an HTML file.

Then I created an Image variable called Number1.

The getCodeBase() command provided the name of the directory where I ran the applet from. I used the getImage command to store this and the name of the JPEG in the Number1 variable.

I displayed the JPEG using g.drawImage and ran it as follows:


The output looked like this (as usual, click on the image to enlarge it and bring it into focus if necessary):






  1. Thank you for sharing such a nice and interesting blog with us. I have seen that all will say the same thing repeatedly. But in your blog, I had a chance to get some useful and unique information. I would like to suggest your blog in my dude circle.
    Isoft Innovations Company Address
    Isoft Innovations Adyar
    Isoft Innovations Reviews
    Isoft Innovation Chennai
    Isoft Innovation