DISPLAYING IMAGES

Contents

1. Introduction
2. Example 1


1. INTRODUCTION

Images can be displayed in GUIs in two fortmats:

To load an image into an application we use the getImage method contained in the ToolKit class. This method must consequently be invoked useing a ToolKit object.

The Toolkit abstract class contains method which give GUIs a particular look and feel. Typical methods of interest contained in this class are methods to: display system properties, print, access the system clipboard and manipulate images (GIFs and JPEGs). The Toolkit class is an abstract class and thus to use the methods contained in this class we must create an object of some subclass of this class. An appropraite subclass can be obtained using the method getDefaultToolkit (also contained in the ToolKit class:

Toolkit tk = Toolkit.getDefaultToolKit();
Image < NAME > tk.getImage( < LOCATION > )

To display an image, once we have got it, we use the drawImage contained in the Graphics class. This comes in several versions including:

g.drawImage(image,x,y,this);

g.drawImage(image,x,y,width,height,this);

g.drawImage(image,x,y,bgColor,this);

g.drawImage(image,x,y,width,height,bgColor,this);

Where the first argument is an Image object and the second two are the top left hand coordinates of the image. In the second example the width nand height arguments are used to specify the size the image is to be drawn at. It is also possible to specify the background colour.



2. EXAMPLE 1

Some sample code to load and display an image (stored in the same directory as the code is given in Table 1. The file is loaded using the file checking system described previously. The positioning of the image has been achieved (in this case) purely through a trial and error approach.

// Image Viewer
// Frans Coenen
// Dept. Comp. Sci., University of Liverpool
// Friday 19 January 20001

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.File;

/* ----------------------------------------------- */
/*                                                 */
/*                 MY CANVAS CLASS                 */
/*                                                 */
/* ----------------------------------------------- */
  
class MyCanvas extends Canvas {
    private String fileName;
    private int width;
    private int height;
    private int picWidth;
    private int picHeight;
    
    MyCanvas(String fn, int wd, int ht) {
        fileName  = fn;
	width     = wd;
	height    = ht;
	picWidth  = width-30;
	picHeight = height-48;
	}
	
    /* Paint */
    
    public void paint(Graphics g) {
        
	// Get the default tool

        Toolkit tk = Toolkit.getDefaultToolkit();

        // Get image, create a File object, check that given file exists
	// and get the image using the default ToolKit object
	
	File f = new File(fileName);
	if (f.exists() && f.isFile() && f.canRead()) {
	    System.out.println("Loading file " + fileName);
	    Image graphic = tk.getImage(fileName);
	    g.drawImage(graphic,11,10,picWidth,picHeight,Color.blue,this);
	    }
	else System.err.println("Unable to load file " + fileName);
	}
    }

/* ----------------------------------------------- */
/*                                                 */
/*                 IMAGE VIEWER CLASS              */
/*                                                 */
/* ----------------------------------------------- */	    

class ImageViewer extends Frame implements WindowListener {
    
   /* Constructor */
    
    public ImageViewer(String text, int width, int height, String fileName) {
        super(text);    
        setBackground(Color.yellow);
        addWindowListener(this);
        
	// Create instance of class MyCanvas
	
	MyCanvas pic = new MyCanvas(fileName,width,height);
        add(pic); 
        }     
	  
    /* Window Listeners */
                                      
    public void windowClosed(WindowEvent event) {}
    public void windowDeiconified(WindowEvent event) {}
    public void windowIconified(WindowEvent event) {}
    public void windowActivated(WindowEvent event) {}
    public void windowDeactivated(WindowEvent event) {}
    public void windowOpened(WindowEvent event) {}
    public void windowClosing(WindowEvent event) {
        System.exit(0);
        } 
    }

/* ----------------------------------------------- */
/*                                                 */
/*         IMAGE VIEWER APPLICATION CLASS          */
/*                                                 */
/* ----------------------------------------------- */	
    
class ImageViewerApp {
    
    static final int WIDTH  = 300;
    static final int HEIGHT = 300;
    
    /* Main  method */    
    
    public static void main(String[] args) {
        ImageViewer screen = new ImageViewer("Image Viewer",WIDTH,HEIGHT,args[0]);
	screen.setSize(WIDTH,HEIGHT);
        screen.setVisible(true);
        }
    }

Table 1: Image Viewer Application

EXAMPL OUTPUT 1

(a)

EXAMPL OUTPUT 2

(b)

Figure 1: Image Viewer Application Example output




Created and maintained by Frans Coenen. Last updated 27 January 2001