ActionTest05.java

package com.bozoid.cis370.hw05;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*; // for Ellipse2D
import java.awt.font.*;
import java.util.Random;


/**
CISC370-011 homework #5.

This application demontrates <code>Action</code> objects attached
to menu items and toolbars, and the use of popup menus.

@author Walt Leipold
*/
public class ActionTest05
{
    public static void main(String argv[])
    {
        Frame05 myframe = new Frame05();
        myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myframe.show();
    }
}


class Frame05 extends JFrame
{
    public Frame05()
    {
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension ss = kit.getScreenSize();
        setSize(3*ss.width/4,3*ss.height/4);
        setLocation(ss.width/8,ss.height/8);
        setTitle("CISC370-011 homework #5");
        Container contentPane = getContentPane();

        // Build a menu bar...
        JMenuBar mb = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        JMenu animationMenu = new JMenu("Animation");
        mb.add(fileMenu);
        mb.add(animationMenu);

        // ...and a toolbar...
        JToolBar tb = new JToolBar("Animations");

        // ...and a popup menu...
        JPopupMenu pop = new JPopupMenu();

        // ...and a drawing pane...
        Panel05 cp = new Panel05(40,this); // 40ms frame rate

        // ...and add them to the frame.
        setJMenuBar(mb);
        contentPane.setLayout(new BorderLayout());
        contentPane.add(tb,BorderLayout.NORTH);
        contentPane.add(cp);

        // Create some Action objects and their animators...
        Action[] acts = new Action[6];
        acts[0] = new AnimationAction(
            "Bounce20","Bouncing balls 20",cp,
            new AnimatedBalls(20));
        acts[1] = new AnimationAction(
            "Bounce100","Bouncing balls 100",cp,
            new AnimatedBalls(100));
        acts[2] = new AnimationAction(
            "Line20","Bouncing lines 20",cp,
            new AnimatedLines(20));
        acts[3] = new AnimationAction(
            "Line50","Bouncing lines 50",cp,
            new AnimatedLines(50));
        acts[4] = new AnimationAction(
            "SpinShort","Spinning short message",cp,
            new Spinner("Hello!",144));
        acts[5] = new AnimationAction(
            "SpinLong","Spinning long message",cp,
            new Spinner("Hello, handsome!",72));

        // ...and add them to the Animation menu...
        for (int i=0; i<acts.length; i++)
            animationMenu.add(acts[i]);

        // ...and to the toolbar...
        for (int i=0; i<acts.length; i++)
            tb.add(acts[i]);

        // ...and to the popup menu.
        for (int i=0; i<acts.length; i++)
            pop.add(acts[i]);

        // Add an 'Exit' item to the File menu.
        JMenuItem exitItem = new JMenuItem("Exit");
        fileMenu.add(exitItem);
        exitItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
                { System.exit(0); }
            });

        // Tie a mouse listener into the animation panel.
        cp.addMouseListener(new AMouse(pop));
    }

    class AMouse extends MouseAdapter
    {
        public AMouse(JPopupMenu pop) 
        {
            myPopup = pop;
        }

        public void mouseClicked(MouseEvent e) { tryit(e); }
        public void mousePressed(MouseEvent e) { tryit(e); }
        public void mouseReleased(MouseEvent e) { tryit(e); }

        private void tryit(MouseEvent e)
        {
            if (e.isPopupTrigger())
                myPopup.show(e.getComponent(),e.getX(),e.getY());
        }

        private JPopupMenu myPopup;
    }
}