/* animationControl.java Paul Carlisle A control which sets the state of animation, as well as the direction. */ import java.awt.*; public class animationControl extends Panel { private GridBagLayout mainLayout; private GridBagLayout Flayout; private GridBagLayout Blayout; private GridBagConstraints mainConstraints; private Button Animate; private Button Backward; private Button Forward; private Panel Bbackgnd; private Panel Fbackgnd; private boolean Fselected = true; private boolean Aselected = false; private boolean gotMessage = false; public static int NONE = 0; public static int ANIMATE = 1; public static int REVERSE = 2; private int messageID = NONE; public animationControl(Font font) { Blayout = new GridBagLayout(); Flayout = new GridBagLayout(); mainLayout = new GridBagLayout(); mainConstraints = new GridBagConstraints(); setLayout(mainLayout); Animate = new Button("Animate"); Animate.setFont(font); Animate.setBackground(Color.white); Backward = new Button("<<"); Backward.setFont(font); Backward.setBackground(Color.white); Forward = new Button(">>"); Forward.setFont(font); Forward.setBackground(Color.white); // Create a Panel with a light background, and add // the Backward button to it. Bbackgnd = new Panel(); //Bbackgnd.setBackground(Color.lightGray); Bbackgnd.setLayout(Blayout); // Use the mainConstraints object here to set up placement. // This simply avoids creating a new variable. mainConstraints.fill = GridBagConstraints.NONE; mainConstraints.anchor = GridBagConstraints.CENTER; mainConstraints.insets = new Insets(4, 4, 4, 4); mainConstraints.gridx = 0; mainConstraints.gridy = 0; Blayout.setConstraints(Backward, mainConstraints); Bbackgnd.add(Backward); // Create a Panel with a dark background, and add // the Forward button to it. Fbackgnd = new Panel(); //Fbackgnd.setBackground(Color.darkGray); Fbackgnd.setLayout(Flayout); // Use the mainConstraints object here to set up placement. // This simply avoids creating a new variable. mainConstraints.gridx = 0; Flayout.setConstraints(Forward, mainConstraints); Fbackgnd.add(Forward); // Add the Animate button to the main layout. mainConstraints.insets = new Insets(2, 2, 2, 2); mainConstraints.gridx = 1; mainLayout.setConstraints(Animate, mainConstraints); add(Animate); // Add the Backward button/Panel combination to the main layout. mainConstraints.gridx = 0; mainLayout.setConstraints(Bbackgnd, mainConstraints); add(Bbackgnd); // Add the Forward buton/Panel combination to the main layout. mainConstraints.gridx = 2; mainLayout.setConstraints(Fbackgnd, mainConstraints); add(Fbackgnd); setControls(true, false); } public boolean action(Event e, Object what) { if (e.target == Backward) { setControls(false, Aselected); gotMessage = true; messageID = REVERSE; } else if (e.target == Forward) { setControls(true, Aselected); gotMessage = true; messageID = REVERSE; } else if (e.target == Animate) { setControls(Fselected, !Aselected); gotMessage = true; messageID = ANIMATE; } return false; } public boolean getAnimate() { return Aselected; } public boolean getForward() { return Fselected; } public boolean getMessage() { boolean temp = gotMessage; gotMessage = false; return temp; } public int getMessageID() { return messageID; } public void paint(Graphics g) { if(Fselected) { Fbackgnd.setBackground(Color.darkGray); Bbackgnd.setBackground(Color.lightGray); } else if (!Fselected) { Fbackgnd.setBackground(Color.lightGray); Bbackgnd.setBackground(Color.darkGray); } } public void setControls(boolean forward, boolean animate) { if (animate) { Aselected = true; Animate.setLabel("Stop"); } else if (!animate) { Aselected = false; Animate.setLabel("Animate"); } // Have to call repaint() to get some browsers to redraw the // highlighting around the selected button. if (forward) { Fselected = true; Fbackgnd.setBackground(Color.darkGray); Bbackgnd.setBackground(Color.lightGray); Fbackgnd.repaint(); Bbackgnd.repaint(); } else if (!forward) { Fselected = false; Fbackgnd.setBackground(Color.lightGray); Bbackgnd.setBackground(Color.darkGray); Fbackgnd.repaint(); Bbackgnd.repaint(); } } public void update(Graphics g) { paint(g); } }