public static Pair<JPanel, JBList> createActionGroupPanel(
      ActionGroup action, final JComponent parent, final Runnable backAction) {
    JPanel actionsListPanel = new JPanel(new BorderLayout());
    actionsListPanel.setBackground(getProjectsBackground());
    final JBList list = new JBList(action.getChildren(null));
    list.setBackground(getProjectsBackground());
    list.installCellRenderer(
        new NotNullFunction<AnAction, JComponent>() {
          final JLabel label = new JLabel();

          {
            label.setBorder(JBUI.Borders.empty(3, 7));
          }

          @NotNull
          @Override
          public JComponent fun(AnAction action) {
            label.setText(action.getTemplatePresentation().getText());
            Icon icon = action.getTemplatePresentation().getIcon();
            label.setIcon(icon);
            return label;
          }
        });
    JScrollPane pane = ScrollPaneFactory.createScrollPane(list, true);
    pane.setBackground(getProjectsBackground());
    actionsListPanel.add(pane, BorderLayout.CENTER);
    if (backAction != null) {
      final JLabel back = new JLabel(AllIcons.Actions.Back);
      back.setBorder(JBUI.Borders.empty(3, 7, 10, 7));
      back.setHorizontalAlignment(SwingConstants.LEFT);
      new ClickListener() {
        @Override
        public boolean onClick(@NotNull MouseEvent event, int clickCount) {
          backAction.run();
          return true;
        }
      }.installOn(back);
      actionsListPanel.add(back, BorderLayout.SOUTH);
    }
    final Ref<Component> selected = Ref.create();
    final JPanel main = new JPanel(new BorderLayout());
    main.add(actionsListPanel, BorderLayout.WEST);

    ListSelectionListener selectionListener =
        new ListSelectionListener() {
          @Override
          public void valueChanged(ListSelectionEvent e) {
            if (e.getValueIsAdjusting()) {
              // Update when a change has been finalized.
              // For instance, selecting an element with mouse fires two consecutive
              // ListSelectionEvent events.
              return;
            }
            if (!selected.isNull()) {
              main.remove(selected.get());
            }
            Object value = list.getSelectedValue();
            if (value instanceof AbstractActionWithPanel) {
              JPanel panel = ((AbstractActionWithPanel) value).createPanel();
              panel.setBorder(JBUI.Borders.empty(7, 10));
              selected.set(panel);
              main.add(selected.get());

              for (JButton button : UIUtil.findComponentsOfType(main, JButton.class)) {
                if (button.getClientProperty(DialogWrapper.DEFAULT_ACTION) == Boolean.TRUE) {
                  parent.getRootPane().setDefaultButton(button);
                  break;
                }
              }

              main.revalidate();
              main.repaint();
            }
          }
        };
    list.addListSelectionListener(selectionListener);
    if (backAction != null) {
      new AnAction() {
        @Override
        public void actionPerformed(@NotNull AnActionEvent e) {
          backAction.run();
        }
      }.registerCustomShortcutSet(KeyEvent.VK_ESCAPE, 0, main);
    }
    return Pair.create(main, list);
  }
  AbstractPopup init(
      final Project project,
      @NotNull final JComponent component,
      @Nullable final JComponent preferredFocusedComponent,
      final boolean requestFocus,
      final boolean focusable,
      final boolean forceHeavyweight,
      final boolean movable,
      final String dimensionServiceKey,
      final boolean resizable,
      @Nullable final String caption,
      @Nullable final Computable<Boolean> callback,
      final boolean cancelOnClickOutside,
      @Nullable final Set<JBPopupListener> listeners,
      final boolean useDimServiceForXYLocation,
      InplaceButton commandButton,
      @Nullable final IconButton cancelButton,
      @Nullable final MouseChecker cancelOnMouseOutCallback,
      final boolean cancelOnWindow,
      @Nullable final ActiveIcon titleIcon,
      final boolean cancelKeyEnabled,
      final boolean locateBycontent,
      final boolean placeWithinScreenBounds,
      @Nullable final Dimension minSize,
      float alpha,
      @Nullable MaskProvider maskProvider,
      boolean inStack,
      boolean modalContext,
      @Nullable Component[] focusOwners,
      @Nullable String adText,
      int adTextAlignment,
      final boolean headerAlwaysFocusable,
      @NotNull List<Pair<ActionListener, KeyStroke>> keyboardActions,
      Component settingsButtons,
      @Nullable final Processor<JBPopup> pinCallback,
      boolean mayBeParent,
      boolean showShadow) {
    if (requestFocus && !focusable) {
      assert false
          : "Incorrect argument combination: requestFocus="
              + requestFocus
              + " focusable="
              + focusable;
    }

    myActivityKey = new UiActivity.Focus("Popup:" + this);
    myProject = project;
    myComponent = component;
    myPopupBorder = PopupBorder.Factory.create(true, showShadow);
    myShadowed = showShadow;
    myPaintShadow =
        showShadow
            && !SystemInfo.isMac
            && !movable
            && !resizable
            && Registry.is("ide.popup.dropShadow");
    myContent = createContentPanel(resizable, myPopupBorder, isToDrawMacCorner() && resizable);
    myMayBeParent = mayBeParent;

    myContent.add(component, BorderLayout.CENTER);
    if (adText != null) {
      setAdText(adText, adTextAlignment);
    }

    myCancelKeyEnabled = cancelKeyEnabled;
    myLocateByContent = locateBycontent;
    myLocateWithinScreen = placeWithinScreenBounds;
    myAlpha = alpha;
    myMaskProvider = maskProvider;
    myInStack = inStack;
    myModalContext = modalContext;
    myFocusOwners = focusOwners;
    myHeaderAlwaysFocusable = headerAlwaysFocusable;
    myMovable = movable;

    ActiveIcon actualIcon = titleIcon == null ? new ActiveIcon(EmptyIcon.ICON_0) : titleIcon;

    myHeaderPanel = new JPanel(new BorderLayout());

    if (caption != null) {
      if (caption.length() > 0) {
        myCaption = new TitlePanel(actualIcon.getRegular(), actualIcon.getInactive());
        ((TitlePanel) myCaption).setText(caption);
      } else {
        myCaption = new CaptionPanel();
      }

      if (pinCallback != null) {
        myCaption.setButtonComponent(
            new InplaceButton(
                new IconButton(
                    "Pin",
                    IconLoader.getIcon("/general/autohideOff.png"),
                    IconLoader.getIcon("/general/autohideOff.png"),
                    IconLoader.getIcon("/general/autohideOffInactive.png")),
                new ActionListener() {
                  public void actionPerformed(final ActionEvent e) {
                    pinCallback.process(AbstractPopup.this);
                  }
                }));
      } else if (cancelButton != null) {
        myCaption.setButtonComponent(
            new InplaceButton(
                cancelButton,
                new ActionListener() {
                  public void actionPerformed(final ActionEvent e) {
                    cancel();
                  }
                }));
      } else if (commandButton != null) {
        myCaption.setButtonComponent(commandButton);
      }
    } else {
      myCaption = new CaptionPanel();
      myCaption.setBorder(null);
      myCaption.setPreferredSize(new Dimension(0, 0));
    }

    setWindowActive(myHeaderAlwaysFocusable);

    myHeaderPanel.add(myCaption, BorderLayout.NORTH);
    myContent.add(myHeaderPanel, BorderLayout.NORTH);

    myForcedHeavyweight = true;
    myResizable = resizable;
    myPreferredFocusedComponent = preferredFocusedComponent;
    myRequestFocus = requestFocus;
    myFocusable = focusable;
    myDimensionServiceKey = dimensionServiceKey;
    myCallBack = callback;
    myCancelOnClickOutside = cancelOnClickOutside;
    myCancelOnMouseOutCallback = cancelOnMouseOutCallback;
    myListeners = listeners == null ? new HashSet<JBPopupListener>() : listeners;
    myUseDimServiceForXYLocation = useDimServiceForXYLocation;
    myCancelOnWindow = cancelOnWindow;
    myMinSize = minSize;

    for (Pair<ActionListener, KeyStroke> pair : keyboardActions) {
      myContent.registerKeyboardAction(
          pair.getFirst(), pair.getSecond(), JComponent.WHEN_IN_FOCUSED_WINDOW);
    }

    if (settingsButtons != null) {
      myCaption.addSettingsComponent(settingsButtons);
    }

    return this;
  }
Example #3
0
  public MapPanel(ClientGameBoard gl) {
    super();

    _dismiss = false;
    gameLogic = gl;
    gameLogic._mapPanel = this;
    _hexes = new ArrayList<Hex>();
    vertexContents = new Hashtable<CoordPair, Pair>();
    roadContents = new Hashtable<Pair, Integer>();
    portContents = new Hashtable<Pair, BoardObject.type>();

    diceImage = new BufferedImage(582, 98, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = diceImage.createGraphics();
    g.drawImage(BoardObject.images.get(BoardObject.type.DICE), null, null);
    g.dispose();

    rings = gameLogic.getNumRings();

    hexleft =
        100
            - (int)
                (radius
                    + (Math.floor(rings / 2) * radius + Math.floor((rings - 1) / 2) * radius * 2));
    if (rings % 2 == 0) {
      hexleft -= radius / 2;
    }

    hextop = 300 - (int) (radius * 0.866 + (rings - 1) * 2 * (radius * 0.866));

    double border = 0.4;

    HashMap<Pair, Pair> hexData = gameLogic.getHexInfo(); // call the gamelogic

    Pair currCoord = gameLogic.getStartPoint();

    Pair topCoord = currCoord;

    int ring = 0;

    int currentDir = 5;
    int current = 0;
    int[][] directions = {{1, 1}, {0, 2}, {-1, 1}, {-1, -1}, {0, -2}, {1, -1}};

    int[][] HexCoordDirections = {{2, 1}, {0, 2}, {-2, 1}, {-2, -1}, {0, -2}, {2, -1}};

    Hex top =
        new Hex(
            100,
            300,
            radius,
            (BoardObject.type) (hexData.get(currCoord).getA()),
            (Integer) (hexData.get(currCoord).getB()));
    Hex curr = top;

    _hexes.add(top);
    while (true) {
      if (current == ring) {
        currentDir++;
        current = 0;
      }
      if (currentDir > 5) {
        currentDir = 0;
        current = 0;
        ring++;
        if (ring < rings) {
          topCoord = new Pair(currCoord.getA(), (Double) (currCoord.getB()) - 2);
          currCoord = topCoord;

          top =
              new Hex(
                  curr.getX(),
                  (curr.getY() - 2 * (Math.cos(Math.PI / 6) * (curr.getRadius() + border))),
                  curr.getRadius(),
                  (BoardObject.type) (hexData.get(currCoord).getA()),
                  (Integer) (hexData.get(currCoord).getB()));
          curr = top;

        } else {
          break;
        }
      }
      currCoord.setA((Object) ((Double) (currCoord.getA()) + HexCoordDirections[currentDir][0]));
      currCoord.setB((Object) ((Double) (currCoord.getB()) + HexCoordDirections[currentDir][1]));

      curr =
          new Hex(
              (curr.getX() + directions[currentDir][0] * (curr.getRadius() + border) * 3 / 2),
              (curr.getY()
                  + directions[currentDir][1]
                      * (Math.cos(Math.PI / 6) * (curr.getRadius() + border))),
              curr.getRadius(),
              (BoardObject.type) (hexData.get(currCoord).getA()),
              (Integer) (hexData.get(currCoord).getB()));
      _hexes.add(curr);

      current++;
    }

    addMouseListener(this);
    addMouseMotionListener(this);
  }
Example #4
0
  public void simulateBall() {
    ballPreviousX = ballX;
    ballPreviousY = ballY;
    leftEdgeActive = rightEdgeActive = topEdgeActive = bottomEdgeActive = true;

    if (ballY >= MaxY) {
      ballStarted = false;
    }

    if (!ballStarted) {
      ballX = padx - ballDiameter / 2;
      ballY = padTop - ballDiameter;
    } else {
      ballX = ballX + (int) (Math.cos((angle * pi) / 180.0) * unit);
      ballY = ballY - (int) (Math.sin((angle * pi) / 180.0) * unit); // Y increases downward
      if (Math.sin((angle * pi) / 180.0) * unit >= 0) {
        top = true;
      } else top = false;

      if (Math.cos((angle * pi) / 180.0) * unit >= 0) {
        right = true;
      } else right = false;

      if (right) leftEdgeActive = false;
      else rightEdgeActive = false;

      if (top) bottomEdgeActive = false;
      else topEdgeActive = false;

      Pair temp;

      if (leftEdgeActive) {
        int xBlockLeft = ((ballX - blockStartX) / blockLength) * blockLength;
        int yBlockLeft = ((ballY - blockStartY + ballDiameter / 2) / blockHeight) * blockHeight;

        // Left Boundary Edge
        temp = new Pair(xBlockLeft, yBlockLeft);
        if ((blockMap.containsKey(temp.toString())
                && (ballX >= blockStartX)
                && (ballY >= blockStartY))
            || ballX <= 0) {
          if (immutableBlocks.containsKey(temp.toString()) == false) {

            /*				if(fireBlocks.containsKey(temp.toString())){
            				fireEnabled = true;
            			}
            */
            angle = 180.0 - angle;
            blockMap.remove(temp.toString());
            repaintBlocks = true;
            count = numRepaints;
            repaint();
            return;
          } else {
            // angle = 180.0 - angle;
            blockMap.put(temp.toString(), Color.RED);
            repaintBlocks = true;
            count = numRepaints;
            repaint();
            return;
          }
        }
      }

      if (rightEdgeActive) {
        int xBlockRight = ((ballX - blockStartX + ballDiameter) / blockLength) * blockLength;
        int yBlockRight = ((ballY - blockStartY + ballDiameter / 2) / blockHeight) * blockHeight;
        // Right Boundary Edge
        temp = new Pair(xBlockRight, yBlockRight);
        if ((blockMap.containsKey(temp.toString())
                && (ballX - blockStartX + ballDiameter > 0)
                && (ballY - blockStartY + ballDiameter / 2) >= 0)
            || (ballX + ballDiameter) >= MaxX) {
          // System.out.println("Right edge");
          if (immutableBlocks.containsKey(temp.toString()) == false) {
            /*		if(fireBlocks.containsKey(temp.toString())){
            	fireEnabled = true;
            }*/
            angle = 180.0 - angle;
            blockMap.remove(temp.toString());
            repaintBlocks = true;
            count = numRepaints;
            repaint();
            return;
          } else {
            // angle = 180.0 - angle;
            blockMap.put(temp.toString(), Color.RED);
            repaintBlocks = true;
            count = numRepaints;
            repaint();
            return;
          }
        }
      }
      if (topEdgeActive) {
        int xBlockTop = ((ballX - blockStartX + ballDiameter / 2) / blockLength) * blockLength;
        int yBlockTop = ((ballY - blockStartY) / blockHeight) * blockHeight;
        // top Boundary Edge
        temp = new Pair(xBlockTop, yBlockTop);

        if ((blockMap.containsKey(temp.toString())
                && (ballX >= blockStartX)
                && (ballY >= blockStartY))
            || (ballY - ballDiameter) <= 0) {
          if (immutableBlocks.containsKey(temp.toString()) == false) {
            /*if(fireBlocks.containsKey(temp.toString())){
            		fireEnabled = true;
            }*/

            angle = 360.0 - angle;
            blockMap.remove(temp.toString());
            repaintBlocks = true;
            count = numRepaints;
            repaint();
            // System.out.println("temp " + temp.toString());

            return;
          } else {
            // angle = 360.0 - angle;
            blockMap.put(temp.toString(), Color.RED);
            repaintBlocks = true;
            count = numRepaints;
            repaint();
            return;
          }
        }
      }
      if (bottomEdgeActive) {
        int xBlockBottom = ((ballX - blockStartX + ballDiameter / 2) / blockLength) * blockLength;
        int yBlockBottom = ((ballY - blockStartY + ballDiameter) / blockHeight) * blockHeight;
        // top Boundary Edge
        temp = new Pair(xBlockBottom, yBlockBottom);
        if ((blockMap.containsKey(temp.toString())
                && (ballX - blockStartX + ballDiameter / 2 >= 0)
                && (ballY - blockStartY + ballDiameter >= 0))
            || (((((ballY + ballDiameter) >= padTop && (ballY + ballDiameter) <= padBottom))
                    || (ballY >= padTop && ballY <= padBottom))
                && (ballX) >= (padx - padLength / 2)
                && ballX <= (padx + padLength / 2))) {

          if (immutableBlocks.containsKey(temp.toString()) == false) {
            /*if(fireBlocks.containsKey(temp.toString())){
            	fireEnabled = true;
            }*/
            angle = 360.0 - angle;
            blockMap.remove(temp.toString());
            repaintBlocks = true;
            count = numRepaints;
            repaint();
            return;
          } else {
            // angle = 360.0 - angle;
            blockMap.put(temp.toString(), Color.RED);
            repaintBlocks = true;
            count = numRepaints;
            repaint();
            return;
          }
        }
      }
    }
  }
Example #5
0
  public synchronized void paint(Graphics graphics) {

    Graphics2D g = (Graphics2D) graphics;

    Image water = Toolkit.getDefaultToolkit().getImage("catanui/water.jpg");
    g.drawImage(water, 0, 0, this);

    for (Hex o : _hexes) {
      o.paint(g, _display_offset[0], _display_offset[1]);
    }

    g.translate(_display_offset[0] + 2, _display_offset[1] - 1);
    synchronized (portContents) {
      for (Pair c : portContents.keySet()) {

        int lowx =
            hexleft
                + (((CoordPair) c.getA()).getX() - (((CoordPair) c.getA()).getX() % 2))
                    / 2
                    * intervalSide[0]
                + (((CoordPair) c.getA()).getX() - (((CoordPair) c.getA()).getX() % 2))
                    / 2
                    * intervalSide[1]
                + (((CoordPair) c.getA()).getX() % 2) * intervalSide[0];
        int lowy = hextop + ((CoordPair) c.getA()).getY() * intervalUp;
        int highx =
            hexleft
                + (((CoordPair) c.getB()).getX() - (((CoordPair) c.getB()).getX() % 2))
                    / 2
                    * intervalSide[0]
                + (((CoordPair) c.getB()).getX() - (((CoordPair) c.getB()).getX() % 2))
                    / 2
                    * intervalSide[1]
                + (((CoordPair) c.getB()).getX() % 2) * intervalSide[0];
        int highy = hextop + ((CoordPair) c.getB()).getY() * intervalUp;

        int dx = highx - lowx;
        int dy = highy - lowy;
        double rad = Math.atan((1.0) * dy / dx);

        if (dx < 0) rad += Math.PI;

        g.translate(lowx, lowy);
        g.rotate(rad);
        g.drawImage(
            BoardObject.images.get(BoardObject.type2port.get(portContents.get(c))), 0, -75, null);
        g.rotate(-rad);
        g.translate((-1) * lowx, (-1) * lowy);
      }
    }
    g.translate((-1) * _display_offset[0], (-1) * _display_offset[1]);

    synchronized (roadContents) {
      for (Pair c : roadContents.keySet()) {

        Road r =
            new Road(
                hexleft
                    + (((CoordPair) c.getA()).getX() - (((CoordPair) c.getA()).getX() % 2))
                        / 2
                        * intervalSide[0]
                    + (((CoordPair) c.getA()).getX() - (((CoordPair) c.getA()).getX() % 2))
                        / 2
                        * intervalSide[1]
                    + (((CoordPair) c.getA()).getX() % 2) * intervalSide[0],
                hextop + ((CoordPair) c.getA()).getY() * intervalUp);

        r.setX2(
            hexleft
                + (((CoordPair) c.getB()).getX() - (((CoordPair) c.getB()).getX() % 2))
                    / 2
                    * intervalSide[0]
                + (((CoordPair) c.getB()).getX() - (((CoordPair) c.getB()).getX() % 2))
                    / 2
                    * intervalSide[1]
                + (((CoordPair) c.getB()).getX() % 2) * intervalSide[0]);
        r.setY2(hextop + ((CoordPair) c.getB()).getY() * intervalUp);

        r.setColor(roadContents.get(c));
        r.paint(g, _display_offset[0], _display_offset[1]);
      }
    }

    synchronized (vertexContents) {
      for (CoordPair c : vertexContents.keySet()) {
        int newx =
            hexleft
                + ((c._x - (c._x % 2)) / 2 * intervalSide[0]
                    + (c._x - (c._x % 2)) / 2 * intervalSide[1]
                    + (c._x % 2) * intervalSide[0])
                - 20;
        int newy = hextop + c._y * intervalUp - 20;

        if ((BoardObject.type) (vertexContents.get(c).getA()) == BoardObject.type.SETTLEMENT) {
          Settlement s = new Settlement(newx, newy, (Integer) (vertexContents.get(c).getB()));
          s.paint(g, _display_offset[0], _display_offset[1]);
        } else if ((BoardObject.type) (vertexContents.get(c).getA()) == BoardObject.type.CITY) {
          City s = new City(newx, newy, (Integer) (vertexContents.get(c).getB()));
          s.paint(g, _display_offset[0], _display_offset[1]);
        } else System.out.println("neither -_-");
      }
    }

    g.setColor(Color.GRAY);
    g.fill(new Rectangle(0, 0, 110, 60));
    g.setColor(Color.LIGHT_GRAY);
    g.fill(new Rectangle(3, 3, 104, 56));
    if (_dieRoll > 0) {
      BufferedImage r1img =
          diceImage.getSubimage((int) (Math.floor((twoDice[0] - 1) * 94.7)), 0, 94, 93);
      g.drawImage(r1img, 5, 7, 48, 47, null);
      BufferedImage r2img =
          diceImage.getSubimage((int) (Math.floor((twoDice[1] - 1) * 94.7)), 0, 94, 93);
      g.drawImage(r2img, 55, 7, 48, 47, null);
    }

    if (_up != null) _up.paint(g);

    if (!_gameOver.equals("") && !_dismiss) {
      _currAlpha += 0.007;
    }
    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, _currAlpha));
    g.setColor(Color.GRAY);
    g.fill(new Rectangle(-20, 0, 1020, 650));
    g.setColor(Color.BLACK);
    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) 1.0));
    if (!_gameOver.equals("")) {

      if (_currAlpha >= 0.8) {
        if (_gameOver.equals(gameLogic._name)) {
          g.drawString("Congratulations, you won!", 350, 200);
        } else {
          g.drawString(_gameOver + " has won!", 350, 200);
        }
        _dismiss = true;
      } else repaint();
    }
  }