Example #1
0
  private boolean buildNoteSequence() {

    noteSequence = new NoteSequence();

    double quantizeBeatFactor = quantizeBeatSetting * 1000.0 * 60.0 / (double) getBPM();
    double quantizeDurationFactor = quantizeDurationSetting * 1000.0 * 60.0 / (double) getBPM();

    for (int i = 0; i < noteList.size(); i++) {
      noteListElement = noteList.get(i);

      if (noteListElement.underTone == true) {
        continue;
      }

      note = noteListElement.note;

      if (note < getLowPitch()) continue;
      if (note > getHighPitch()) continue;

      double startTime = (double) (noteListElement.startTime);

      if (quantizeBeatFactor != 0.0)
        startTime = Math.floor(startTime / quantizeBeatFactor) * quantizeBeatFactor;
      long startTick = 1 + (long) (startTime * getTickRate() / 1000.0);

      double endTime = (double) (noteListElement.endTime);

      if (quantizeBeatFactor != 0.0)
        endTime = Math.ceil(endTime / quantizeBeatFactor) * quantizeBeatFactor;
      if (quantizeDurationFactor != 0)
        endTime =
            startTime
                + (Math.ceil((endTime - startTime) / quantizeDurationFactor)
                    * quantizeDurationFactor);

      long endTick = 1 + (long) (endTime * getTickRate() / 1000.0);

      if ((endTick - startTick) < 1) endTick = startTick + 1;
      System.out.println(
          "times: " + startTime + ", " + endTime + ", " + getStartTime() + ", " + getEndTime());
      if (endTime < getStartTime()) continue;
      if (startTime > getEndTime()) continue;

      velocity = 64;
      noteSequence.add(new NoteSequenceElement(note, ON, startTick, velocity));
      noteSequence.add(new NoteSequenceElement(note, OFF, endTick, velocity));
    }

    if (noteSequence.size() == 0) {
      return false;
    } else {
      noteSequence.sort();
      return true;
    }
  }
  private int[][] apply(boolean preview) {
    int[][] heightmap = new int[128][128];
    int step = 1;

    if (!preview) {
      parent.setProgressBar(sldIterations.getValue());
      heightmap = parent.cloneArray(parent.getHeightMap());
    } else {
      heightmap = parent.getPreviewMap();
      step =
          (int)
              Math.ceil(
                  (double) parent.getHeightMap().length / (double) parent.getPreviewMap().length);
    }

    for (int i = 0; i < sldIterations.getValue() / step; i++) {
      heightmap = applyThermal(heightmap);

      if (!preview) parent.increaseProgressBar();
    }

    if (!preview) parent.resetProgressBar();

    return heightmap;
  }
Example #3
0
 /**
  * This deals with a bug/peculiarity for the default Mac font: several pixels of the ascent are
  * actually empty. This screws up certain measurements which assume the font is actually a few
  * pixels taller than it really is.
  */
 private static int getUnusedAscent(FontMetrics fm, Font font) {
   Integer value = ascentTable.get(font);
   if (value == null) {
     int recordedAscent = fm.getAscent();
     FontRenderContext frc = new FontRenderContext(new AffineTransform(), false, false);
     GlyphVector gv = font.createGlyphVector(frc, "XYZ");
     Rectangle2D bounds = ShapeBounds.getBounds(gv.getOutline());
     int observedAscent = (int) (Math.ceil(bounds.getHeight()) + .5);
     value = new Integer(recordedAscent - observedAscent);
     ascentTable.put(font, value);
   }
   return value.intValue();
 }
  /** Initialize cycleTime, repaintInterval, numFrames, animationIndex. */
  private void initIndeterminateDefaults() {
    initRepaintInterval(); // initialize repaint interval
    initCycleTime(); // initialize cycle length

    // Make sure repaintInterval is reasonable.
    if (repaintInterval <= 0) {
      repaintInterval = 100;
    }

    // Make sure cycleTime is reasonable.
    if (repaintInterval > cycleTime) {
      cycleTime = repaintInterval * 20;
    } else {
      // Force cycleTime to be a even multiple of repaintInterval.
      int factor = (int) Math.ceil(((double) cycleTime) / ((double) repaintInterval * 2));
      cycleTime = repaintInterval * factor * 2;
    }
  }
  /** Class constructor - make the JFrame ready */
  public TestProgram() {
    // Set screen size
    Toolkit tk = Toolkit.getDefaultToolkit();
    int x = (int) tk.getScreenSize().getWidth() - 150;
    int y = (int) tk.getScreenSize().getHeight() - 100;
    this.setSize(x, y);

    // Some settings
    this.setTitle("Friends With Benefits - Test program");
    field = new Field();
    random = new Random(System.currentTimeMillis());

    // Set the panel
    c = this.getContentPane();
    c.setLayout(new BorderLayout());

    contentpanel = new ContentPanel();
    contentpanel.setField(field);
    c.add(contentpanel, BorderLayout.CENTER);

    // Menupanel
    menupanel = new JPanel();
    layout = new GroupLayout(menupanel);
    menupanel.setLayout(layout);
    c.add(menupanel, BorderLayout.EAST);

    progress = new JProgressBar(0, 99);
    progress.setValue(99);

    open = new JButton("Open");
    open.setFocusPainted(false);
    save = new JButton("Save");
    save.setFocusPainted(false);
    open.addActionListener(this);
    save.addActionListener(this);

    everywhere = new JRadioButton("Everywhere");
    everywhere.setFocusPainted(false);
    inRectangle = new JRadioButton("In bounding rectangle");
    inRectangle.setFocusPainted(false);
    inRectangle.setSelected(true);

    placeOfCluster = new ButtonGroup();
    placeOfCluster.add(everywhere);
    placeOfCluster.add(inRectangle);

    addnoise = new JButton("Add noise");
    addnoise.setFocusPainted(false);
    addnoise.addActionListener(this);

    circle = new JRadioButton("Circle");
    circle.setFocusPainted(false);
    square = new JRadioButton("Square");
    square.setFocusPainted(false);
    circle.setSelected(true);

    squarecircle = new ButtonGroup();
    squarecircle.add(circle);
    squarecircle.add(square);

    fillFactor = new JSlider();
    fillFactor.setMajorTickSpacing(20);
    fillFactor.setMinorTickSpacing(5);
    fillFactor.setPaintTicks(true);

    addacluster = new JButton("Add simple cluster");
    addacluster.setFocusPainted(false);
    addacluster.addActionListener(this);

    clear = new JButton("Clear field");
    clear.addActionListener(this);
    clear.setFocusPainted(false);

    center = new JButton("Center field");
    center.addActionListener(this);
    center.setFocusPainted(false);

    minAlgo = new JTextField();
    minAlgo.setText("0");
    maxAlgo = new JTextField();
    maxAlgo.setText("10");
    run = new JButton("Run algo");
    run.addActionListener(this);
    run.setFocusPainted(false);

    empty = new JPanel();
    empty.setLayout(new BorderLayout());

    JSeparator sep1 = new JSeparator();
    JSeparator sep2 = new JSeparator();
    JSeparator sep3 = new JSeparator();
    JSeparator sep4 = new JSeparator();
    JLabel runalgo = new JLabel("Run algorithm"), addcluster = new JLabel("Add simple cluster");
    JLabel addNoise = new JLabel("Add noise"), fillf = new JLabel("Fill factor:");
    JLabel minalgo = new JLabel("Min:"), maxalgo = new JLabel("Max:");
    addcluster.setFont(f);
    addNoise.setFont(f);
    runalgo.setFont(f);

    fillFactor.setPreferredSize(
        new Dimension(
            menupanel.getPreferredSize().width / 2, fillFactor.getPreferredSize().height));

    layout.setHorizontalGroup(
        layout
            .createParallelGroup(GroupLayout.Alignment.CENTER)
            .addGroup(
                layout.createSequentialGroup().addComponent(open).addGap(2).addComponent(save))
            .addComponent(sep1)
            .addComponent(addNoise)
            .addGroup(
                layout
                    .createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(everywhere)
                    .addComponent(inRectangle))
            .addComponent(addnoise)
            .addComponent(sep2)
            .addComponent(addcluster)
            .addGroup(
                layout
                    .createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(circle)
                    .addComponent(square))
            .addComponent(fillf)
            .addComponent(fillFactor)
            .addComponent(addacluster)
            .addComponent(sep3)
            .addComponent(center)
            .addComponent(clear)
            .addComponent(sep4)
            .addComponent(runalgo)
            .addGroup(
                layout
                    .createSequentialGroup()
                    .addComponent(minalgo)
                    .addGap(2)
                    .addComponent(minAlgo)
                    .addGap(10)
                    .addComponent(maxalgo)
                    .addGap(2)
                    .addComponent(maxAlgo))
            .addComponent(run)
            .addComponent(empty)
            .addComponent(progress));
    layout.setVerticalGroup(
        layout
            .createSequentialGroup()
            .addGroup(
                layout
                    .createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(open)
                    .addComponent(save))
            .addGap(5)
            .addComponent(sep1)
            .addGap(5)
            .addComponent(addNoise)
            .addComponent(everywhere)
            .addComponent(inRectangle)
            .addGap(3)
            .addComponent(addnoise)
            .addGap(5)
            .addComponent(sep2)
            .addGap(5)
            .addComponent(addcluster)
            .addComponent(circle)
            .addComponent(square)
            .addGap(3)
            .addComponent(fillf)
            .addGap(3)
            .addComponent(fillFactor)
            .addGap(3)
            .addComponent(addacluster)
            .addGap(5)
            .addComponent(sep3)
            .addGap(5)
            .addComponent(center)
            .addGap(3)
            .addComponent(clear)
            .addGap(5)
            .addComponent(sep4)
            .addGap(5)
            .addComponent(runalgo)
            .addGap(3)
            .addGroup(
                layout
                    .createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(minalgo)
                    .addComponent(minAlgo)
                    .addComponent(maxalgo)
                    .addComponent(maxAlgo))
            .addComponent(run)
            .addComponent(empty)
            .addComponent(progress));

    int width = menupanel.getPreferredSize().width;

    setSize(addNoise, width - 10);
    setSize(everywhere, width - 20);
    setSize(inRectangle, width - 20);
    setSize(addnoise, width);

    setSize(addcluster, width - 10);
    setSize(circle, width - 20);
    setSize(square, width - 20);
    setSize(addacluster, width);

    setSize(center, width);
    setSize(clear, width);

    setSize(runalgo, width - 10);
    setSize(minalgo, (int) Math.floor(width / 4) - 20);
    setMaxSize(minAlgo, (int) Math.ceil(width / 4));
    setSize(maxalgo, (int) Math.floor(width / 4) - 20);
    setMaxSize(maxAlgo, (int) Math.ceil(width / 4));
    setSize(run, width);

    empty.setPreferredSize(new Dimension(width, 1000));

    menupanel.setBorder(
        BorderFactory.createTitledBorder(
            BorderFactory.createLineBorder(Color.BLACK),
            "Menu",
            TitledBorder.CENTER,
            TitledBorder.DEFAULT_POSITION,
            f));
  }
// ----------------------------------------------------------------------------
Example #7
0
public class MapPanel extends JPanel implements MouseListener, MouseMotionListener {

  private ArrayList<Hex> _hexes;
  // private ArrayList<BoardObject> _objects;

  private int[] _mousedown;
  private int[] _display_offset = {400, 0};

  private Hashtable<CoordPair, Pair> vertexContents;
  private Hashtable<Pair, Integer> roadContents;
  private Hashtable<Pair, BoardObject.type> portContents;

  public BoardObject _up;

  public SideBar sb;

  public int _x;
  public int _h;

  private int hexleft;
  private int hextop;
  private int radius = 75;

  private int _dieRoll;
  private int[] twoDice;

  private String _gameOver = "";
  private float _currAlpha = (float) 0.0;

  int intervalUp = (int) Math.ceil(radius * 0.866);
  int[] intervalSide = new int[] {(int) (radius / 2), radius};
  int rings;
  private final int[] DIE_DIST = {2, 3, 4, 4, 5, 5, 5, 6, 6, 8, 8, 9, 9, 9, 10, 10, 11, 12};
  private BufferedImage diceImage;
  // Robot r;

  private boolean _dismiss;

  private ClientGameBoard gameLogic;

  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);
  }

  private void callObjectAtLocation(int x, int y) {
    for (Hex o : _hexes) {
      if (Math.sqrt((o.getX() - x) * (o.getX() - x) + (o.getY() - y) * (o.getY() - y))
          < o.getRadius()) {
        o.clicked(x, y);
        return;
      }
    }
  }

  public void updateVertexContents(Hashtable<CoordPair, Pair> newy) {
    synchronized (vertexContents) {
      vertexContents = newy;
    }
    repaint();
  }

  public void updateEdgeContents(Hashtable<Pair, Integer> newy) {
    synchronized (roadContents) {
      roadContents = newy;
    }
    repaint();
  }

  public void updatePortContents(Hashtable<Pair, BoardObject.type> newy) {
    synchronized (portContents) {
      portContents = newy;
    }
    repaint();
  }

  public void updateRoll(int i) {
    _dieRoll = i;
    twoDice =
        new int[] {
          (int) Math.max(Math.floor(Math.random() * Math.min(_dieRoll - 1, 5)) + 1, _dieRoll - 6), 1
        };
    twoDice[1] = _dieRoll - twoDice[0];
    repaint();
  }

  public void gameOver(String s) {
    _gameOver = s;
    repaint();
  }

  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();
    }
  }

  /** MOUSE EVENTS! */
  @Override
  public void mouseClicked(MouseEvent e) {
    callObjectAtLocation(e.getX() - _display_offset[0], e.getY() - _display_offset[1]);
    repaint();
  }

  @Override
  public void mouseEntered(MouseEvent e) {
    repaint();
  }

  @Override
  public void mouseExited(MouseEvent e) {

    if (_up != null) { // && (e.getY() < _h-10)) {
      if (_up.getType() == BoardObject.type.SETTLEMENT) sb.activateExchanger(0, true);
      else if (_up.getType() == BoardObject.type.ROAD) sb.activateExchanger(1, true);
      else if (_up.getType() == BoardObject.type.CITY) sb.activateExchanger(3, true);
      //                 sb.setUp(_up,e.getX(),e.getY());
      _up = null;
      repaint();
    } else {

    }
  }

  @Override
  public void mousePressed(MouseEvent e) {
    if (_up == null) _mousedown = new int[] {e.getX(), e.getY()};
  }

  @Override
  public void mouseReleased(MouseEvent e) {

    if (_up != null) {
      int[] pos = setUpNearest(e);

      if (_up.getType() == BoardObject.type.SETTLEMENT)
        gameLogic.writeBuildSettlement(pos[0], pos[1]);
      else if (_up.getType() == BoardObject.type.CITY) gameLogic.writeBuildCity(pos[0], pos[1]);
      else if ((_up.getType() == BoardObject.type.ROAD) && (((Road) _up).oneDown == true)) {

        gameLogic.writeBuildRoad(((Road) _up).mycoord[0], ((Road) _up).mycoord[1], pos[0], pos[1]);
      }

      if (_up.getType() == BoardObject.type.ROAD) {
        if (((Road) _up).oneDown == false) {

          _up.setX(
              hexleft
                  + ((pos[0] - (pos[0] % 2)) / 2 * intervalSide[0]
                      + (pos[0] - (pos[0] % 2)) / 2 * intervalSide[1]
                      + (pos[0] % 2) * intervalSide[0])
                  + _display_offset[0]);
          _up.setY(hextop + pos[1] * intervalUp + _display_offset[1]);

          ((Road) _up).oneDown = true;
          ((Road) _up).mycoord = pos;

          _mousedown = null;
          e.consume();
        } else {
          ((Road) _up)
              .setX2(
                  hexleft
                      + ((pos[0] - (pos[0] % 2)) / 2 * intervalSide[0]
                          + (pos[0] - (pos[0] % 2)) / 2 * intervalSide[1]
                          + (pos[0] % 2) * intervalSide[0]));
          ((Road) _up).setY2(hextop + pos[1] * intervalUp);
          ((Road) _up).oneDown = true;
          _up = null;
          _mousedown = null;
        }
      } else {
        _up.setX(
            hexleft
                + ((pos[0] - (pos[0] % 2)) / 2 * intervalSide[0]
                    + (pos[0] - (pos[0] % 2)) / 2 * intervalSide[1]
                    + (pos[0] % 2) * intervalSide[0])
                - _up.getW() / 2);
        _up.setY(hextop + pos[1] * intervalUp - _up.getH() / 2);
        _up = null;
        _mousedown = null;
      }
    }
    repaint();
  }

  private int[] setUpNearest(MouseEvent e) {

    int mousex = e.getX() - _display_offset[0];
    int mousey = e.getY() - _display_offset[1];

    int i = 0;
    int j;

    j = (int) Math.round((mousey - hextop) * 1.0 / intervalUp);

    if ((j % 2 + (rings - 1) % 2) == 0 || (((j - 1) % 2 + (rings) % 2) == 0)) {
      int glob = 1;
      for (i = 0;
          (Math.floor((i + 1) / 2) * radius + Math.floor(i / 2) * radius * 2)
              < mousex - hexleft - radius / 2;
          i++) glob += (((i % 2) == 0) ? 1 : 3);

      double dx =
          (Math.floor((i + 1) / 2) * radius + Math.floor(i / 2) * radius * 2)
              - (mousex - hexleft - radius / 2);

      if ((i % 2) == 1 && dx > radius / 2) {
        i = i - 1;
        glob -= 1;
      } else if ((i % 2) == 0 && dx > radius) {
        i = i - 1;
        glob -= 3;
      }

      i = glob;

    } else {
      int glob = 0;
      for (i = 0;
          (Math.floor((i + 1) / 2) * radius * 2 + Math.floor(i / 2) * radius) < mousex - hexleft;
          i++) glob += (((i % 2) == 0) ? 3 : 1);

      double dx =
          (Math.floor((i + 1) / 2) * radius * 2 + Math.floor(i / 2) * radius) - (mousex - hexleft);

      if ((i % 2) == 0 && dx > radius / 2) {
        i = i - 1;
        glob -= 1;
      } else if ((i % 2) == 1 && dx > radius) {
        i = i - 1;
        glob -= 3;
      }

      i = glob;
    }

    return new int[] {i, j};
  }

  @Override
  public void mouseDragged(MouseEvent e) {

    if (_mousedown != null && _up == null) {
      _display_offset[0] += e.getX() - _mousedown[0];
      _display_offset[1] += e.getY() - _mousedown[1];
      _mousedown = new int[] {e.getX(), e.getY()};
    }
    if (_up != null) {
      if (_up.getType() == BoardObject.type.ROAD) {
        if (((Road) _up).oneDown == false) {
          _up.setX(e.getX());
          _up.setY(e.getY());
        } else {
          ((Road) _up).setX2(e.getX());
          ((Road) _up).setY2(e.getY());
        }
      } else {
        _up.setX(e.getX() - _up.getW() / 2);
        _up.setY(e.getY() - _up.getH() / 2);
      }
    }

    repaint();
  }

  @Override
  public void mouseMoved(MouseEvent e) {
    if (_up != null) mouseDragged(e);
  }

  public void setUp(BoardObject u, int x, int y) {

    u.setX(5);
    _up = u;
  }
}