// ButtonFrame.java // From Cay Horstmann's Computing Concepts with Java 2 Essentials. // Adapted by Scot Drysdale. Further changes by THC. // This class puts up the frame with the buttons. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ButtonFrame extends JFrame { private static final long serialVersionUID = 1L; private RectanglePanel rectPanel; // drawing area for the rectangle private JButton upButton; // four buttons private JButton downButton; private JButton leftButton; private JButton rightButton; // An enumerated type for what kind of layout to use. enum LayoutType { flowLayout, wideBorderLayout, niceBorderLayout } public ButtonFrame() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); rectPanel = new RectanglePanel(); // to hold the rectangle being moved JPanel buttonPanel = new JPanel(); // to group the buttons ActionListener listener = new DirectionListener(); upButton = new JButton("Up"); upButton.addActionListener(listener); downButton = new JButton("Down"); downButton.addActionListener(listener); leftButton = new JButton("Left"); leftButton.addActionListener(listener); rightButton = new JButton("Right"); rightButton.addActionListener(listener); // What kind of layout to use. LayoutType layoutStyle; layoutStyle = LayoutType.flowLayout; // layoutStyle = LayoutType.wideBorderLayout; // layoutStyle = LayoutType.niceBorderLayout; // Add the four button components to the buttonPanel. We have our choice // of how to lay them out. switch (layoutStyle) { case flowLayout: buttonPanel.add(upButton); buttonPanel.add(downButton); buttonPanel.add(leftButton); buttonPanel.add(rightButton); break; case wideBorderLayout: buttonPanel.setLayout(new BorderLayout()); buttonPanel.add(upButton, BorderLayout.NORTH); buttonPanel.add(downButton, BorderLayout.SOUTH); buttonPanel.add(leftButton, BorderLayout.WEST); buttonPanel.add(rightButton, BorderLayout.EAST); break; case niceBorderLayout: buttonPanel.setLayout(new BorderLayout()); JPanel northPanel = new JPanel(); northPanel.add(upButton); JPanel southPanel = new JPanel(); southPanel.add(downButton); buttonPanel.add(northPanel, BorderLayout.NORTH); buttonPanel.add(southPanel, BorderLayout.SOUTH); buttonPanel.add(leftButton, BorderLayout.WEST); buttonPanel.add(rightButton, BorderLayout.EAST); break; } // Add the two panels to the content pane. // Note that the content pane uses the Border Layout manager. Container contentPane = getContentPane(); contentPane.add(rectPanel, BorderLayout.CENTER); contentPane.add(buttonPanel, BorderLayout.SOUTH); } // Inner class definition to listen for buttons being clicked. private class DirectionListener implements ActionListener { public void actionPerformed(ActionEvent event) { // Find which button was clicked. // event.getSource returns a reference to the Object for which // the action event occurred. Object source = event.getSource(); // Depending on which button was clicked, move the rectangle in the // appropriate direction. // Note that we can compare the Object reference returned by event.getSource // to the individual JButton object references. if (source == upButton) rectPanel.moveRectangle(0, -1); else if (source == downButton) rectPanel.moveRectangle(0, 1); else if (source == leftButton) rectPanel.moveRectangle(-1, 0); else if (source == rightButton) rectPanel.moveRectangle(1, 0); // Having moved the rectangle, redraw the panel. rectPanel.draw(); } } }