示例#1
0
  public JTarget(ITarget target) {
    if (target instanceof MetaTarget) {
      this.directions = ((MetaTarget) target).getDirections();
    } else {
      this.directions = new ArrayList<RelativeCoordinate>();
      RelativeCoordinate direction = ((SingleTarget) target).getDirection();

      if (direction != null) {
        this.directions.add(direction);
      }
    }
    setLayout(new GridBagLayout());

    JPanel directionsPanel = new JPanel();
    directionsPanel.setLayout(new GridLayout(3, 3, 3, 3));
    GridBagConstraints c = new GridBagConstraints();
    c.anchor = GridBagConstraints.NORTH;
    add(directionsPanel, c);

    JButton neighborsButton = new JButton("neighbors");
    neighborsButton.addActionListener(new AllNeighborsButtonListener());
    c.gridy = 1;
    c.insets = new Insets(5, 0, 0, 0);
    add(neighborsButton, c);

    buttonsDirections = new IdentityHashMap<JToggleButton, RelativeCoordinate>();

    ImageIcon protoIconNorth = new ImageIcon(getClass().getResource("icons/target-north.png"));

    JToggleButton btn = new JToggleButton();
    btn.setIcon(ImageRotator.createRotatedImage(btn, protoIconNorth, 315));
    directionsPanel.add(btn);
    buttonsDirections.put(btn, RelativeCoordinate.NORTH_WEST);

    btn = new JToggleButton();
    btn.setIcon(protoIconNorth);
    directionsPanel.add(btn);
    buttonsDirections.put(btn, RelativeCoordinate.NORTH);

    btn = new JToggleButton();
    btn.setIcon(ImageRotator.createRotatedImage(btn, protoIconNorth, 45));
    directionsPanel.add(btn);
    buttonsDirections.put(btn, RelativeCoordinate.NORTH_EAST);

    btn = new JToggleButton();
    btn.setIcon(ImageRotator.createRotatedImage(btn, protoIconNorth, 270));
    directionsPanel.add(btn);
    buttonsDirections.put(btn, RelativeCoordinate.WEST);

    btn = new JToggleButton();
    btn.setIcon(new ImageIcon(getClass().getResource("icons/target-self.png")));
    directionsPanel.add(btn);
    buttonsDirections.put(btn, RelativeCoordinate.CENTER);

    btn = new JToggleButton();
    btn.setIcon(ImageRotator.createRotatedImage(btn, protoIconNorth, 90));
    directionsPanel.add(btn);
    buttonsDirections.put(btn, RelativeCoordinate.EAST);

    btn = new JToggleButton();
    btn.setIcon(ImageRotator.createRotatedImage(btn, protoIconNorth, 225));
    directionsPanel.add(btn);
    buttonsDirections.put(btn, RelativeCoordinate.SOUTH_WEST);

    btn = new JToggleButton();
    btn.setIcon(ImageRotator.createRotatedImage(btn, protoIconNorth, 180));
    directionsPanel.add(btn);
    buttonsDirections.put(btn, RelativeCoordinate.SOUTH);

    btn = new JToggleButton();
    btn.setIcon(ImageRotator.createRotatedImage(btn, protoIconNorth, 135));
    directionsPanel.add(btn);
    buttonsDirections.put(btn, RelativeCoordinate.SOUTH_EAST);

    for (JToggleButton b : buttonsDirections.keySet()) {
      b.setPreferredSize(new Dimension(30, 30));
      b.setMaximumSize(b.getPreferredSize());
      b.setMinimumSize(b.getPreferredSize());
      b.addActionListener(new TargetActionListener());
      for (RelativeCoordinate rc : directions) {
        if (rc == this.buttonsDirections.get(b)) {
          b.setSelected(true);
          break;
        }
      }
    }
  }