ActionTest07.java

package com.bozoid.cis370.hw07;

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


/**
CISC370-011 homework #7.

Application tucked into a double-clickable JAR file.

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

    public ActionTest07()
    {
        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 #7");
        Container contentPane = getContentPane();

        prefs = Preferences.userRoot().node("com/bozoid/cis370/hw07");

        // 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...
        cpane = new Panel07(40,this); // 40ms frame rate

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

        // Create some Action objects and their animators...
        acts = new AnimationAction[6];
        acts[0] = new AnimationAction(
            "Bounce20","Bouncing balls 20",cpane,
            new AnimatedBalls(20));
        acts[1] = new AnimationAction(
            "Bounce100","Bouncing balls 100",cpane,
            new AnimatedBalls(100));
        acts[2] = new AnimationAction(
            "Line20","Bouncing lines 20",cpane,
            new AnimatedLines(20));
        acts[3] = new AnimationAction(
            "Line50","Bouncing lines 50",cpane,
            new AnimatedLines(50));
        acts[4] = new AnimationAction(
            "SpinShort","Spinning short message",cpane,
            new Spinner("Hello!",144));
        acts[5] = new AnimationAction(
            "SpinLong","Spinning long message",cpane,
            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 ExitListener());

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

        // If possible, restore window size/location/animation.
        restoreState();
    }

    public void saveState()
    {
        int estate = getExtendedState();
        prefs.putInt("estate",estate);

        prefs.putInt("wsize",getWidth());
        prefs.putInt("hsize",getHeight());

        Point loc = getLocation();
        prefs.putInt("xloc",loc.x);
        prefs.putInt("yloc",loc.y);

        Animator cur = cpane.getAnimator();
        if (cur == null) 
            prefs.putInt("animation",-1);
        else {
            for (int i=0; i<acts.length; i++) {
                if (acts[i].getAnimator() == cur) {
                    prefs.putInt("animation",i);
                    break;
                    }
                }
            }
    }

    public void restoreState()
    {
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension ss = kit.getScreenSize();

        int wDefault = 3*ss.width/4;
        int hDefault = 3*ss.height/4;
        int xDefault = ss.width/8;
        int yDefault = ss.height/8;

        int wsize = prefs.getInt("wsize",wDefault);
        int hsize = prefs.getInt("hsize",hDefault);
        int xloc = prefs.getInt("xloc",xDefault);
        int yloc = prefs.getInt("yloc",yDefault);
        int estate = prefs.getInt("estate",getExtendedState());
        setExtendedState(estate);
        setSize(wsize,hsize);
        setLocation(xloc,yloc);

        // If there was a previous animation, start it.
        int lastAnimation = prefs.getInt("animation",-1);
        if (lastAnimation >= 0 && lastAnimation < acts.length)
            cpane.setAnimator(acts[lastAnimation].getAnimator());
    }

    class ExitListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            saveState();
            System.exit(0);
        }
    }

    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;
    }

    private Preferences prefs;
    private AnimationAction[] acts;
    private Panel07 cpane;
}