Java – Topic 31

(Images)

 

 

LESSON NOTE

 

 

INTRO

 

In this short lesson, we will look at the basics of importing a raster image into an applet.  Unfortunately, it is not a single line statement.  That said, it is fairly easy.

 

There are new import statements:

import java.io.File;
import java.awt.image.BufferedImage;

You will require two data fields. 

public File f;
public BufferedImage bi;

In the init() method, you need to open the image file and create a File object with it.  Then you will create a BufferedImage with that File object.  Because reading a file can throw an error, we need to place this inside a try statement.

try
      {
         String path = "C:\\Documents and Settings\\";  //Notice the double backslashes
         f = new File(path + "senators.jpg");
         bi = ImageIO.read(f);
      }

      catch (IOException e)
      {
         //we could place code to handle error here
         //but we will leave this blank
      }

Inside the paint method, we can now draw the image at the desired location.  You are required to place an object that will observe the image’s transfer.  Applet objects know how to do this.  Therefore, we will simply but ‘this’ as the fourth argument in the method call.

g2D.drawImage(bi, 50, 50, this);

DOUBLE BACKSLASH

The backslash character inside a String object is used to signify special characters.  For example, \n inside a String means that the String should be restart on the next line. 

So, if you actually want to put a backslash in a string, you simply need to double it up.  That is why the path above contains double backslashes.

SLASH OR BACKSLASH

You should note that windows allows you to use the normal slash or the backslash as a folder separator in paths.  Therefore, the two following paths are the same:

C:\campeau\is\awesome\
C:/campeau/is/awesome/

Of course, if you want to use the version with the backslash in java, you need to double up those backslashes.

PATH

This is a little annoying.  Java requires that you provide the path to your image.  The easiest thing to do is to go into Internet Explorer, find your image, and copy and paste the path that is at the top.  (Don’t forget to double backslash!)

However, using the approach in the above paragraph is bad for files that will be moved to different computers (such as your projects being moved to Mr. Campeau’s computer.)  There are many solutions that are all somewhat yucky!

Here is the simplest solution that Mr. Campeau was able to figure out:

PATH SOLUTION (MR. CAMPEAU’S VERSION)

The Applet class contains a method called getCodeBase() which returns an Url object to the location of your applet file.  That file should contain the same path as your image.  Therefore, all we need to do is convert that Url to a path and we are done.

Here are the steps:

1-     Get the string version of the Url object.

2-     Remove the starting “file:/” part.

3-     Replace all occurrences of %20 by a space.  This is needed for Windows machines but might cause problems on other operating systems.

Fortunately, all this can be done in one statement:

                        getCodeBase().toString().substring(6).replace("%20"," ")

EXAMPLE 1

            This first applet shows the most basic importing of an image.  No listeners were incorporated.

            Click here to see the applet.  Click here to see the code.  Click here to download all files.

EXAMPLE 2

            This applet simply shows that an image can be relocated to where a mouse is clicked.

            Click here to see the applet.  Click here to see the code.  Click here to download all files.