public void actionPerformed(ActionEvent e) { if (e.getSource() instanceof DirectionButton) { DirectionButton button = (DirectionButton) e.getSource(); setSelectedDirection(button.getDirection()); button.repaint(); } }
private JComponent createDisplayLocationPanel() { ButtonGroup group = new ButtonGroup(); SelectAction selectAction = new SelectAction(); DirectionButton bottom = new DirectionButton(SwingConstants.SOUTH); bottom.addActionListener(selectAction); DirectionButton right = new DirectionButton(SwingConstants.EAST); right.addActionListener(selectAction); DirectionButton left = new DirectionButton(SwingConstants.WEST); left.addActionListener(selectAction); DirectionButton top = new DirectionButton(SwingConstants.NORTH); top.addActionListener(selectAction); DirectionButton bottomLeft = new DirectionButton(SwingConstants.SOUTH_WEST); bottomLeft.addActionListener(selectAction); DirectionButton bottomRight = new DirectionButton(SwingConstants.SOUTH_EAST); bottomRight.addActionListener(selectAction); DirectionButton topLeft = new DirectionButton(SwingConstants.NORTH_WEST); topLeft.addActionListener(selectAction); DirectionButton topRight = new DirectionButton(SwingConstants.NORTH_EAST); topRight.addActionListener(selectAction); group.add(bottom); group.add(left); group.add(right); group.add(top); group.add(bottomLeft); group.add(bottomRight); group.add(topLeft); group.add(topRight); JPanel panel = new JPanel(new GridLayout(3, 3)); panel.add(topLeft); panel.add(top); panel.add(topRight); panel.add(left); panel.add(new JPanel()); panel.add(right); panel.add(bottomLeft); panel.add(bottom); panel.add(bottomRight); return panel; }