
/**
 *
 * demonstration of AWT - move image with mouse
 *
 * Written by: Roger Garside
 *
 * First Written: 11/July/96
 * Last Rewritten: 30/May/97
 *
 */

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

public class Chapter16n5 extends Frame
			implements WindowListener, ActionListener
    {
    Canvas5 canvas ;
    MenuItem load, quit ;

    String name ;
    String directory ;

    /**
     *
     * constructor
     *
     */

    public Chapter16n5()
	{
	// set up basic window
	setTitle("Chapter16n5") ;
        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 Canvas5(this) ;
	add("Center", canvas) ;
	} // end of constructor method

    /**
     *
     * main
     *
     */

    public static void main(String[] args)
        {
        Chapter16n5 f = new Chapter16n5() ;
        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 Chapter16n5

class Canvas5 extends Canvas implements MouseListener
    {
    Chapter16n5 parent ;

    // mouse coordinates
    int mx = -1, my = -1 ;

    /**
     *
     * constructor
     *
     */
 
    public Canvas5(Chapter16n5 f)
        {
        parent = f ;
        addMouseListener(this) ;
        } // 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) ;

	    // obtain mouse coordinates if available
            int xx, yy ;
	    if (mx == -1)
	        {
	        xx = cx ;
	        yy = cy ;
	        }
	    else
	        {
	        xx = mx ;
	        yy = my ;
	        }
            g.drawImage(image,
		        xx - (image.getWidth(this) / 2),
		        yy - (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 = xx - (w1 / 2) ;
            int cty = yy + (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

    /**
     *
     * mouseClicked
     *
     */

    public void mouseClicked(MouseEvent event)
        {
	mx = event.getX() ;
	my = event.getY() ;
        repaint() ;
        } // end of method mouseClicked
					 
    public void mousePressed(MouseEvent event) {}
    public void mouseReleased(MouseEvent event) {}
    public void mouseEntered(MouseEvent event) {}
    public void mouseExited(MouseEvent event) {}
    } // end of class Canvas5

