import objectdraw.*; import java.awt.*; import java.awt.event.*; /** * Demonstrates use of button, text field, and label widgets, panels, * flow and border layout, and key listening with focus management. * * A vertical bar is displayed in the canvas area with X coordinate determined * by the displayed value, which is incremented and decremented by a given change * amount using the arrow keys. The change amount can be modified with the text * field, and the value may be reset to its initial setting with a reset button. */ public class GuiDemo extends FrameWindowController implements KeyListener, ActionListener, FocusListener { private static final int INIT_INCREMENT = 10; private static final int INIT_VALUE = 100; private int value = INIT_VALUE; private int change = INIT_INCREMENT; private Label valueLabel; private Button reset; private TextField changeField; private FilledRect rect; public void begin() { // the following widgets can't be created before begin is called rect = new FilledRect(value, 0, 10, 1000, canvas); reset = new Button("Reset value"); valueLabel = new Label(" "); // 5 spaces for adequate label length changeField = new TextField("" + INIT_INCREMENT); Panel panel = new Panel(); panel.add(new Label("Value:")); panel.add(valueLabel); panel.add(reset); panel.add(new Label("Change:")); panel.add(changeField); // use the default FlowLayout layout manager add(panel, BorderLayout.NORTH); add(new Label("Use right and left arrow keys to increment and deccrement."), BorderLayout.SOUTH); // the canvas is in the center of the layout // add all the listeners reset.addActionListener(this); changeField.addActionListener(this); changeField.addFocusListener(this); canvas.addKeyListener(this); validate(); // need this or widgets don't show up until frame is resized // w/o the following key events aren't recognized until you click on the canvas focusOnCanvas(); setValue(INIT_VALUE); } // implement ActionListener public void actionPerformed(ActionEvent e) { if (e.getSource() == changeField) { change = Integer.parseInt(changeField.getText()); } else setValue(INIT_VALUE); // source is reset button focusOnCanvas(); } // implement KeyListener public void keyPressed(KeyEvent e) { switch (e.getKeyCode()) { case KeyEvent.VK_LEFT: { setValue(value - change); break; } case KeyEvent.VK_RIGHT: { setValue(value + change); break; } } } public void keyReleased(KeyEvent e) {} public void keyTyped(KeyEvent e) {} // implement FocusListener public void focusGained(FocusEvent e) {} public void focusLost(FocusEvent e) { focusOnCanvas(); } private void setValue(int value) { this.value = value; valueLabel.setText("" + value); rect.moveTo(value, 0); } /* Force the keyboard listener focus to be on the objectdraw canvas. * * You don't want to know how this works, other than noting that it * uses getAppletFrame, so it has to be in the FrameWindowController class. */ private void focusOnCanvas() { ((AWTDrawingCanvas)getAppletFrame().getAccessibleContext() .getAccessibleChild(0).getAccessibleContext().getAccessibleChild(0)) .requestFocus(); } }