
/**
 *
 * demonstration of AWT - menus, files, images
 *
 * Written by: Roger Garside
 *
 * First Written: 11/July/96
 * Last Rewritten: 30/May/97
 *
 */

import java.awt.* ;
import java.awt.event.* ;

public class Chapter16n4 extends Frame
			implements WindowListener, ActionListener
    {
    Canvas4 canvas ;
    MenuItem load, quit ;

    String name ;
    String directory ;

    /**
     *
     * constructor
     *
     */

    public Chapter16n4()
	{
	// set up basic window
	setTitle("Chapter16n4") ;
        setBackground(Color.green) ;
        setSize(500, 400) ;
        addWindowListener(this) ;

	// set up menu system
	Menu menu = new Menu("File") ;
        load = new MenuItem("Load") ;
	menu.add(load) ;
        load.addActionListener(this) ;
        quit = new MenuItem("Quit") ;
	menu.add(quit) ;
        quit.addActionListener(this) ;
	MenuBar menuBar = new MenuBar() ;
	menuBar.add(menu) ;
	setMenuBar(menuBar) ;

	name = null ;

	// set up area on which to display images
	canvas = new Canvas4(this) ;
	add("Center", canvas) ;
	} // end of constructor method

    /**
     *
     * main
     *
     */

    public static void main(String[] args)
        {
        Chapter16n4 f = new Chapter16n4() ;
        f.setVisible(true) ;
        } // end of main method

    /**
     *
     * actionPerformed
     *
     */

    public void actionPerformed(ActionEvent event)
        {
	// deal with "Quit" button
	if (event.getSource() == quit)
	    {
	    dispose();
	    System.exit(0);
	    }
	// deal with "Load" button
	else if (event.getSource() == load)
	    loadFile() ;
        } // end of method actionPerformed
					 
    /**
     *
     * loadFile
     *
     */

    private void loadFile()
        {
        FileDialog d = new FileDialog(this, "Load File", FileDialog.LOAD) ;
        d.setDirectory("./") ;
        d.setVisible(true) ;
        name = d.getFile() ;
        directory = d.getDirectory() ;
        canvas.repaint() ;
        } // end of method loadFile

    /**
     *
     * windowClosing
     *
     */

    public void windowClosing(WindowEvent event)
        {
        dispose();
        System.exit(0);
        } // end of method windowClosing

    public void windowOpened(WindowEvent event) {}
    public void windowIconified(WindowEvent event) {}
    public void windowDeiconified(WindowEvent event) {}
    public void windowClosed(WindowEvent event) {}
    public void windowActivated(WindowEvent event) {}
    public void windowDeactivated(WindowEvent event) {}
    } // end of class Chapter16n4

class Canvas4 extends Canvas
    {
    Chapter16n4 parent ;

    /**
     *
     * constructor
     *
     */

    public Canvas4(Chapter16n4 f)
        {
        parent = f ;
        } // end of constructor method

    /**
     *
     * paint
     *
     */

    public void paint(Graphics g)
        {
        Dimension d = getSize() ;
        int cx = d.width / 2,
 	    cy = d.height /2 ;
	// draw a line round the edge of the canvas
        g.setColor(Color.black) ;
        g.drawRoundRect(2, 2, d.width - 5, d.height - 5, 20, 20) ;

        if (parent.name != null)
	    {
            String filename = parent.directory + parent.name ;
            Image image = Toolkit.getDefaultToolkit().getImage(filename) ;
            g.drawImage(image,
		        cx - (image.getWidth(this) / 2),
		        cy - (image.getHeight(this) / 2),
		        this) ;
          
	    // write the name of the file under the image
            Font f1 = new Font("TimesRoman", Font.PLAIN, 14) ;
            FontMetrics fm1 = g.getFontMetrics(f1) ;
            int w1 = fm1.stringWidth(parent.name) ;
            g.setColor(Color.black) ;
            g.setFont(f1) ;
            int ctx = cx - (w1 / 2) ;
            int cty = cy + (image.getHeight(this) / 2) + 30 ;
            g.drawString(parent.name, ctx, cty) ;
	    }
        else
	    {
	    // write the message "No File" in the centre of the canvas
            Font f1 = new Font("TimesRoman", Font.PLAIN, 14) ;
            FontMetrics fm1 = g.getFontMetrics(f1) ;
	    String s1 = "No File" ;
            int w1 = fm1.stringWidth(s1) ;
            g.setColor(Color.black) ;
            g.setFont(f1) ;
            int ctx = cx - (w1 / 2) ;
            int cty = cy ;
            g.drawString(s1, ctx, cty) ;
	    }
        } // end of method paint
    } // end of class Canvas4

