Example #1
1
  public void run() {
    String[] sData;
    while (oConn.isConnected()) {
      sData = oConn.getData();
      System.out.println("Received \"" + sData[0] + "\" command.");

      if (sData[0].equals("chat")) panChat.addChat(sData[1]);
      else if (sData[0].equals("loadmap")) {
        try {
          oMap.loadMap(new File(DEFAULT_MAP_DIRECTORY + sData[1]));
          int nRand = (int) Math.floor((Math.random() + 1) + 0.5);
          stateDeploy(nRand);
          if (nRand == 1) nRand = 2;
          else nRand = 1;
          oConn.send("deploy", "" + nRand);
        } catch (FileNotFoundException e) {
          oConn.send("error", "Opponent does not have map.");
          actionDisconnect();
        }
      } else if (sData[0].equals("deploy")) {
        stateDeploy(Integer.parseInt(sData[1]));
      } else if (sData[0].equals("error")) {
        JOptionPane.showMessageDialog(frame, sData[1]);
        actionDisconnect();
      } else if (sData[0].equals("deployed")) {
        bOpponentDeployed = true;
        if (bDeployed) {
          oConn.send("trade parties", "");
          tradeParties();
          makeAvatarOrder();
          long nRandSeed = Math.round((Math.random() * Long.MAX_VALUE));
          oRandom = new Random(nRandSeed);
          oConn.send("random seed", "" + nRandSeed);
          oConn.send("phase", "1");
          phase1();
        }
      } else if (sData[0].equals("trade parties")) {
        tradeParties();
      } else if (sData[0].equals("avatar order")) {
        makeAvatarOrder(sData[1]);
      } else if (sData[0].equals("random seed")) {
        oRandom = new Random(Long.parseLong(sData[1]));
      } else if (sData[0].equals("phase")) {
        switch (Integer.parseInt(sData[1])) {
          case 1:
            phase1();
            break;
          case 2:
            phase2();
            break;
          case 3:
            phase3();
            break;
          case 4:
            iChar = 0;
            bMyAT = false;
            phase4();
            break;
        }
      } else if (sData[0].equals("move")) {
        oMap.moveATAvatar(Integer.parseInt(sData[1]), Integer.parseInt(sData[2]));
        // TEMP
        stateEndTurn();
      }
    }
  }
  /**
   * Stores the position and size of the bouncing box that would be painted for the current
   * animation index in <code>r</code> and returns <code>r</code>. Subclasses that add to the
   * painting performed in this class's implementation of <code>paintIndeterminate</code> -- to draw
   * an outline around the bouncing box, for example -- can use this method to get the location of
   * the bouncing box that was just painted. By overriding this method, you have complete control
   * over the size and position of the bouncing box, without having to reimplement <code>
   * paintIndeterminate</code>.
   *
   * @param r the Rectangle instance to be modified; may be <code>null</code>
   * @return <code>null</code> if no box should be drawn; otherwise, returns the passed-in rectangle
   *     (if non-null) or a new rectangle
   * @see #setAnimationIndex
   * @since 1.4
   */
  protected Rectangle getBox(Rectangle r) {
    int currentFrame = getAnimationIndex();
    int middleFrame = numFrames / 2;

    if (sizeChanged() || delta == 0.0 || maxPosition == 0.0) {
      updateSizes();
    }

    r = getGenericBox(r);

    if (r == null) {
      return null;
    }
    if (middleFrame <= 0) {
      return null;
    }

    // assert currentFrame >= 0 && currentFrame < numFrames
    if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
      if (currentFrame < middleFrame) {
        r.x = componentInnards.x + (int) Math.round(delta * (double) currentFrame);
      } else {
        r.x = maxPosition - (int) Math.round(delta * (currentFrame - middleFrame));
      }
    } else { // VERTICAL indeterminate progress bar
      if (currentFrame < middleFrame) {
        r.y = componentInnards.y + (int) Math.round(delta * currentFrame);
      } else {
        r.y = maxPosition - (int) Math.round(delta * (currentFrame - middleFrame));
      }
    }
    return r;
  }
Example #3
0
  public void mouseClicked(MouseEvent e) {
    int x;
    int y;

    e.consume();

    if (mouseEventsEnabled) {

      x = Math.round(e.getX() / scale);
      y = Math.round(e.getY() / scale);

      // allow for the canvas margin
      y -= margin;

      // System.out.println("Mouse Click: (" + x + ", " + y + ")");

      if (e.getClickCount() < 2) {
        if (nativeSelectItem(x, y)) {
          parentFTAFrame.updateFrame();
        }
      } else {
        if (nativeSelectItem(x, y)) {
          parentFTAFrame.updateFrame();
        }

        editSelected();
        parentFTAFrame.updateFrame();
      }

      if (focusEventsEnabled) {
        // tell the main Canvas to point to this coordinate
        parentFTAFrame.setCanvasFocus(x, y);
      }
    }
  }
 /**
  * Zooms out by making the source rectangle (srcRect) larger and centering it on (x,y). If we
  * can't make it larger, then make the window smaller.
  */
 public void zoomOut(int x, int y) {
   if (magnification <= 0.03125) return;
   double oldMag = magnification;
   double newMag = getLowerZoomLevel(magnification);
   double srcRatio = (double) srcRect.width / srcRect.height;
   double imageRatio = (double) imageWidth / imageHeight;
   double initialMag = imp.getWindow().getInitialMagnification();
   if (Math.abs(srcRatio - imageRatio) > 0.05) {
     double scale = oldMag / newMag;
     int newSrcWidth = (int) Math.round(srcRect.width * scale);
     int newSrcHeight = (int) Math.round(srcRect.height * scale);
     if (newSrcWidth > imageWidth) newSrcWidth = imageWidth;
     if (newSrcHeight > imageHeight) newSrcHeight = imageHeight;
     int newSrcX = srcRect.x - (newSrcWidth - srcRect.width) / 2;
     int newSrcY = srcRect.y - (newSrcHeight - srcRect.height) / 2;
     if (newSrcX < 0) newSrcX = 0;
     if (newSrcY < 0) newSrcY = 0;
     srcRect = new Rectangle(newSrcX, newSrcY, newSrcWidth, newSrcHeight);
     // IJ.log(newMag+" "+srcRect+" "+dstWidth+" "+dstHeight);
     int newDstWidth = (int) (srcRect.width * newMag);
     int newDstHeight = (int) (srcRect.height * newMag);
     setMagnification(newMag);
     setMaxBounds();
     // IJ.log(newDstWidth+" "+dstWidth+" "+newDstHeight+" "+dstHeight);
     if (newDstWidth < dstWidth || newDstHeight < dstHeight) {
       // IJ.log("pack");
       setDrawingSize(newDstWidth, newDstHeight);
       imp.getWindow().pack();
     } else repaint();
     return;
   }
   if (imageWidth * newMag > dstWidth) {
     int w = (int) Math.round(dstWidth / newMag);
     if (w * newMag < dstWidth) w++;
     int h = (int) Math.round(dstHeight / newMag);
     if (h * newMag < dstHeight) h++;
     x = offScreenX(x);
     y = offScreenY(y);
     Rectangle r = new Rectangle(x - w / 2, y - h / 2, w, h);
     if (r.x < 0) r.x = 0;
     if (r.y < 0) r.y = 0;
     if (r.x + w > imageWidth) r.x = imageWidth - w;
     if (r.y + h > imageHeight) r.y = imageHeight - h;
     srcRect = r;
   } else {
     srcRect = new Rectangle(0, 0, imageWidth, imageHeight);
     setDrawingSize((int) (imageWidth * newMag), (int) (imageHeight * newMag));
     // setDrawingSize(dstWidth/2, dstHeight/2);
     imp.getWindow().pack();
   }
   // IJ.write(newMag + " " + srcRect.x+" "+srcRect.y+" "+srcRect.width+" "+srcRect.height+"
   // "+dstWidth + " " + dstHeight);
   setMagnification(newMag);
   // IJ.write(srcRect.x + " " + srcRect.width + " " + dstWidth);
   setMaxBounds();
   repaint();
 }
Example #5
0
 @Override
 public void mousePressed(MouseEvent e) {
   // if (spawnMenuItem.isEnabled()) {
   int newX = (int) Math.round(e.getX() / (double) game.getPixelSize());
   int newY = (int) Math.round(e.getY() / (double) game.getPixelSize());
   if (game.isInBounds(newY, newX)) {
     erase = game.getEntity(newX, newY) != null;
   }
   repaint();
   // }
 }
Example #6
0
  /**
   * Draw picture (gif, jpg, or png) centered on (x, y).
   *
   * @param x the center x-coordinate of the image
   * @param y the center y-coordinate of the image
   * @param s the name of the image/picture, e.g., "ball.gif"
   * @throws RuntimeException if the image is corrupt
   */
  public static void picture(double x, double y, String s) {
    Image image = getImage(s);
    double xs = scaleX(x);
    double ys = scaleY(y);
    int ws = image.getWidth(null);
    int hs = image.getHeight(null);
    if (ws < 0 || hs < 0) throw new RuntimeException("image " + s + " is corrupt");

    offscreen.drawImage(
        image, (int) Math.round(xs - ws / 2.0), (int) Math.round(ys - hs / 2.0), null);
    draw();
  }
  /**
   * This determines the amount of the progress bar that should be filled based on the percent done
   * gathered from the model. This is a common operation so it was abstracted out. It assumes that
   * your progress bar is linear. That is, if you are making a circular progress indicator, you will
   * want to override this method.
   */
  protected int getAmountFull(Insets b, int width, int height) {
    int amountFull = 0;
    BoundedRangeModel model = progressBar.getModel();

    if ((model.getMaximum() - model.getMinimum()) != 0) {
      if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
        amountFull = (int) Math.round(width * progressBar.getPercentComplete());
      } else {
        amountFull = (int) Math.round(height * progressBar.getPercentComplete());
      }
    }
    return amountFull;
  }
Example #8
0
 public static BufferedImage scaleImage(BufferedImage bi, double scale) {
   int w1 = (int) (Math.round(scale * bi.getWidth()));
   int h1 = (int) (Math.round(scale * bi.getHeight()));
   BufferedImage image = new BufferedImage(w1, h1, BufferedImage.TYPE_INT_RGB);
   Graphics2D g2 = image.createGraphics();
   g2.setRenderingHint(
       RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
   g2.setPaint(Color.white);
   g2.fillRect(0, 0, w1, h1);
   g2.drawImage(bi, 0, 0, w1, h1, null); // this);
   g2.dispose();
   return image;
 }
 /**
  * When the ERASE or SMUDGE tools are used and the mouse jumps from (x1,y1) to (x2,y2), the tool
  * has to be applied to a line of pixel positions between the two points in order to cover the
  * entire line that the mouse moves along. The change is made to the off-screen canvas, and
  * repaint() is called to copy the changes to the screen.
  */
 void applyToolAlongLine(int x1, int y1, int x2, int y2) {
   Graphics g = OSC.getGraphics();
   g.setColor(fillColor); // (for ERASE only)
   int w = OSC.getWidth(); // (for SMUDGE only)
   int h = OSC.getHeight(); // (for SMUDGE only)
   int dist = Math.max(Math.abs(x2 - x1), Math.abs(y2 - y1));
   // dist is the number of points along the line from
   // (x1,y1) to (x2,y2) at which the tool will be applied.
   double dx = (double) (x2 - x1) / dist;
   double dy = (double) (y2 - y1) / dist;
   for (int d = 1; d <= dist; d++) {
     // Apply the tool at one of the points (x,y) along the
     // line from (x1,y1) to (x2,y2).
     int x = (int) Math.round(x1 + dx * d);
     int y = (int) Math.round(y1 + dy * d);
     if (currentTool == Tool.ERASE) {
       // Erase a 10-by-10 block of pixels around (x,y)
       g.fillRect(x - 5, y - 5, 10, 10);
       repaint(x - 5, y - 5, 10, 10);
     } else {
       // For the SMUDGE tool, blend some of the color from
       // the smudgeRed, smudgeGreen, and smudgeBlue arrays
       // into the pixels in a 7-by-7 block around (x,y), and
       // vice versa.  The effect is to smear out the color
       // of pixels that are visited by the tool.
       for (int i = 0; i < 7; i++) {
         for (int j = 0; j < 7; j++) {
           int r = y + j - 3;
           int c = x + i - 3;
           if (!(r < 0 || r >= h || c < 0 || c >= w || smudgeRed[i][j] == -1)) {
             int curCol = OSC.getRGB(c, r);
             int curRed = (curCol >> 16) & 0xFF;
             int curGreen = (curCol >> 8) & 0xFF;
             int curBlue = curCol & 0xFF;
             int newRed = (int) (curRed * 0.7 + smudgeRed[i][j] * 0.3);
             int newGreen = (int) (curGreen * 0.7 + smudgeGreen[i][j] * 0.3);
             int newBlue = (int) (curBlue * 0.7 + smudgeBlue[i][j] * 0.3);
             int newCol = newRed << 16 | newGreen << 8 | newBlue;
             OSC.setRGB(c, r, newCol);
             smudgeRed[i][j] = curRed * 0.3 + smudgeRed[i][j] * 0.7;
             smudgeGreen[i][j] = curGreen * 0.3 + smudgeGreen[i][j] * 0.7;
             smudgeBlue[i][j] = curBlue * 0.3 + smudgeBlue[i][j] * 0.7;
           }
         }
       }
       repaint(x - 3, y - 3, 7, 7);
     }
   }
   g.dispose();
 }
Example #10
0
  /**
   * Draw picture (gif, jpg, or png) centered on (x, y), rotated given number of degrees
   *
   * @param x the center x-coordinate of the image
   * @param y the center y-coordinate of the image
   * @param s the name of the image/picture, e.g., "ball.gif"
   * @param degrees is the number of degrees to rotate counterclockwise
   * @throws IllegalArgumentException if the image is corrupt
   */
  public static void picture(double x, double y, String s, double degrees) {
    Image image = getImage(s);
    double xs = scaleX(x);
    double ys = scaleY(y);
    int ws = image.getWidth(null);
    int hs = image.getHeight(null);
    if (ws < 0 || hs < 0) throw new IllegalArgumentException("image " + s + " is corrupt");

    offscreen.rotate(Math.toRadians(-degrees), xs, ys);
    offscreen.drawImage(
        image, (int) Math.round(xs - ws / 2.0), (int) Math.round(ys - hs / 2.0), null);
    offscreen.rotate(Math.toRadians(+degrees), xs, ys);

    draw();
  }
Example #11
0
 @Override
 public void mouseClicked(MouseEvent e) {
   // if (spawnMenuItem.isEnabled()) {
   int newX = (int) Math.round(e.getX() / (double) game.getPixelSize());
   int newY = (int) Math.round(e.getY() / (double) game.getPixelSize());
   if (game.isInBounds(newX, newY)) {
     if (erase) {
       game.removeEntity(newX, newY);
     } else {
       game.placeEntity(newX, newY, spawnID);
     }
   }
   repaint();
   // }
 }
  public UpgradesPanel(ORUIManager orUIManager) {
    super(BoxLayout.Y_AXIS);

    this.orUIManager = orUIManager;

    preferredSize = new Dimension((int) Math.round(100 * (2 + Scale.getFontScale()) / 3), 200);
    setSize(preferredSize);
    setVisible(true);

    upgradePanel = new JPanel();

    upgradePanel.setOpaque(true);
    upgradePanel.setBackground(Color.DARK_GRAY);
    upgradePanel.setBorder(border);
    upgradePanel.setLayout(new GridLayout(defaultNbPanelElements, 1));

    scrollPane = new JScrollPane(upgradePanel);
    scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    scrollPane.setSize(getPreferredSize());

    doneButton.setActionCommand("Done");
    doneButton.setMnemonic(KeyEvent.VK_D);
    doneButton.addActionListener(this);
    cancelButton.setActionCommand("Cancel");
    cancelButton.setMnemonic(KeyEvent.VK_C);
    cancelButton.addActionListener(this);

    add(scrollPane);
  }
Example #13
0
  //	public void savePDF(){
  //		 Rectangle suggestedPageSize = getITextPageSize(page1.getPageSize());
  //		  Rectangle pageSize = rotatePageIfNecessary(suggestedPageSize);
  //		  //rotate if we need landscape
  //		  Document document = new Document(pageSize);
  //
  //		  PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outputFile));
  //		  document.open();
  //		  Graphics2D graphics = cb.createGraphics(pageSize.getWidth(), pageSize.getHeight());
  //
  //		  // call your GTRenderer here
  //		  GTRenderer draw = new StreamingRenderer();
  //		  draw.setMapContent(mapContent);
  //
  //		  draw.paint(graphics, outputArea, mapContent.getLayerBounds() );
  //
  //		  // cleanup
  //		  graphics.dispose();
  //
  //		  //cleanup
  //		  document.close();
  //		  writer.close();
  //	}
  public void saveImage(final MapContent map, final String file, final int imageWidth) {

    GTRenderer renderer = new StreamingRenderer();
    renderer.setMapContent(map);

    Rectangle imageBounds = null;
    ReferencedEnvelope mapBounds = null;
    try {
      mapBounds = map.getMaxBounds();
      double heightToWidth = mapBounds.getSpan(1) / mapBounds.getSpan(0);
      imageBounds = new Rectangle(0, 0, imageWidth, (int) Math.round(imageWidth * heightToWidth));

    } catch (Exception e) {
      // failed to access mapContent layers
      throw new RuntimeException(e);
    }

    BufferedImage image =
        new BufferedImage(imageBounds.width, imageBounds.height, BufferedImage.TYPE_INT_RGB);

    Graphics2D gr = image.createGraphics();
    gr.setPaint(Color.WHITE);
    gr.fill(imageBounds);

    try {
      renderer.paint(gr, imageBounds, mapBounds);
      File fileToSave = new File(file);
      ImageIO.write(image, "jpeg", fileToSave);

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Example #14
0
 void adjustSourceRect(double newMag, int x, int y) {
   // IJ.log("adjustSourceRect1: "+newMag+" "+dstWidth+"  "+dstHeight);
   int w = (int) Math.round(dstWidth / newMag);
   if (w * newMag < dstWidth) w++;
   int h = (int) Math.round(dstHeight / newMag);
   if (h * newMag < dstHeight) h++;
   x = offScreenX(x);
   y = offScreenY(y);
   Rectangle r = new Rectangle(x - w / 2, y - h / 2, w, h);
   if (r.x < 0) r.x = 0;
   if (r.y < 0) r.y = 0;
   if (r.x + w > imageWidth) r.x = imageWidth - w;
   if (r.y + h > imageHeight) r.y = imageHeight - h;
   srcRect = r;
   setMagnification(newMag);
   // IJ.log("adjustSourceRect2: "+srcRect+" "+dstWidth+"  "+dstHeight);
 }
Example #15
0
  /**
   * Called by the paint method to draw the graph and its graph items.
   *
   * @param g the graphics context.
   */
  public void paintComponent(Graphics g) {

    Dimension dim = getSize();
    Insets insets = getInsets();
    dataArea =
        new Rectangle(
            insets.left,
            insets.top,
            dim.width - insets.left - insets.right - 1,
            dim.height - insets.top - insets.bottom - 1);
    // background
    if (isOpaque()) {
      g.setColor(getBackground());
      g.fillRect(0, 0, dim.width, dim.height);
    }
    g.setColor(getForeground());
    // get axis tickmarks
    double xticks[] = xAxis.getTicks();
    double yticks[] = yAxis.getTicks();
    int yb = dataArea.y + dataArea.height;
    // draw grid
    if (showGrid) {
      g.setColor(gridColor != null ? gridColor : getBackground().darker());
      // vertical x grid lines
      for (int i = 0; i < xticks.length; i += 2) {
        int x = dataArea.x + (int) Math.round(xticks[i]);
        g.drawLine(x, dataArea.y, x, dataArea.y + dataArea.height);
      }
      // horizontal y grid lines
      for (int i = 0; i < yticks.length; i += 2) {
        int y = yb - (int) Math.round(yticks[i]);
        g.drawLine(dataArea.x, y, dataArea.x + dataArea.width, y);
      }
    }
    for (int i = 0; i < graphItems.size(); i++) {
      ((GraphItem) graphItems.elementAt(i)).draw(this, g, dataArea, xAxis, yAxis);
    }
    if (sPt != null && ePt != null) {
      g.setColor(getForeground());
      g.drawRect(
          Math.min(sPt.x, ePt.x), Math.min(sPt.y, ePt.y),
          Math.abs(ePt.x - sPt.x), Math.abs(ePt.y - sPt.y));
    }
  }
  /**
   * Designate the place where the progress string will be painted. This implementation places it at
   * the center of the progress bar (in both x and y). Override this if you want to right, left,
   * top, or bottom align the progress string or if you need to nudge it around for any reason.
   */
  protected Point getStringPlacement(
      Graphics g, String progressString, int x, int y, int width, int height) {
    FontMetrics fontSizer = SwingUtilities2.getFontMetrics(progressBar, g, progressBar.getFont());
    int stringWidth = SwingUtilities2.stringWidth(progressBar, fontSizer, progressString);

    if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
      return new Point(
          x + Math.round(width / 2 - stringWidth / 2),
          y
              + ((height + fontSizer.getAscent() - fontSizer.getLeading() - fontSizer.getDescent())
                  / 2));
    } else { // VERTICAL
      return new Point(
          x
              + ((width - fontSizer.getAscent() + fontSizer.getLeading() + fontSizer.getDescent())
                  / 2),
          y + Math.round(height / 2 - stringWidth / 2));
    }
  }
Example #17
0
 public int getHeight() {
   float fHeight;
   int height;
   fHeight = scale * nativeGetMaxDepth();
   height = Math.round(fHeight) + (2 * margin);
   if (parentContainer == null) {
     return height;
   } else {
     return FTAUtilities.max(height, parentContainer.getHeight());
   }
 }
Example #18
0
 public int getWidth() {
   float fWidth;
   int width;
   fWidth = scale * nativeGetMaxWidth();
   width = Math.round(fWidth);
   if (parentContainer == null) {
     return width;
   } else {
     return FTAUtilities.max(width, parentContainer.getWidth());
   }
 }
  /**
   * Updates progress bar progress line every time a progress event has been received.
   *
   * @param event the <tt>FileTransferProgressEvent</tt> that notified us
   */
  public void progressChanged(FileTransferProgressEvent event) {
    progressBar.setValue((int) event.getProgress());

    long transferredBytes = event.getFileTransfer().getTransferedBytes();
    long progressTimestamp = event.getTimestamp();

    ByteFormat format = new ByteFormat();
    String bytesString = format.format(transferredBytes);

    if ((progressTimestamp - lastSpeedTimestamp) >= SPEED_CALCULATE_DELAY) {
      lastProgressSpeed = Math.round(calculateProgressSpeed(transferredBytes));

      this.lastSpeedTimestamp = progressTimestamp;
      this.lastTransferredBytes = transferredBytes;
    }

    if ((progressTimestamp - lastEstimatedTimeTimestamp) >= SPEED_CALCULATE_DELAY
        && lastProgressSpeed > 0) {
      lastEstimatedTime =
          Math.round(
              calculateEstimatedTransferTime(
                  lastProgressSpeed, transferredFileSize - transferredBytes));

      lastEstimatedTimeTimestamp = progressTimestamp;
    }

    progressBar.setString(getProgressLabel(bytesString));

    if (lastProgressSpeed > 0) {
      progressSpeedLabel.setText(
          resources.getI18NString("service.gui.SPEED") + format.format(lastProgressSpeed) + "/sec");
      progressSpeedLabel.setVisible(true);
    }

    if (lastEstimatedTime > 0) {
      estimatedTimeLabel.setText(
          resources.getI18NString("service.gui.ESTIMATED_TIME")
              + GuiUtils.formatSeconds(lastEstimatedTime * 1000));
      estimatedTimeLabel.setVisible(true);
    }
  }
Example #20
0
  /**
   * Draw picture (gif, jpg, or png) centered on (x, y), rotated given number of degrees, rescaled
   * to w-by-h.
   *
   * @param x the center x-coordinate of the image
   * @param y the center y-coordinate of the image
   * @param s the name of the image/picture, e.g., "ball.gif"
   * @param w the width of the image
   * @param h the height of the image
   * @param degrees is the number of degrees to rotate counterclockwise
   * @throws RuntimeException if the image is corrupt
   */
  public static void picture(double x, double y, String s, double w, double h, double degrees) {
    Image image = getImage(s);
    double xs = scaleX(x);
    double ys = scaleY(y);
    double ws = factorX(w);
    double hs = factorY(h);
    if (ws < 0 || hs < 0) throw new RuntimeException("image " + s + " is corrupt");
    if (ws <= 1 && hs <= 1) pixel(x, y);

    offscreen.rotate(Math.toRadians(-degrees), xs, ys);
    offscreen.drawImage(
        image,
        (int) Math.round(xs - ws / 2.0),
        (int) Math.round(ys - hs / 2.0),
        (int) Math.round(ws),
        (int) Math.round(hs),
        null);
    offscreen.rotate(Math.toRadians(+degrees), xs, ys);

    draw();
  }
Example #21
0
 /**
  * Draw picture (gif, jpg, or png) centered on (x, y), rescaled to w-by-h.
  *
  * @param x the center x coordinate of the image
  * @param y the center y coordinate of the image
  * @param s the name of the image/picture, e.g., "ball.gif"
  * @param w the width of the image
  * @param h the height of the image
  * @throws RuntimeException if the width height are negative
  * @throws RuntimeException if the image is corrupt
  */
 public static void picture(double x, double y, String s, double w, double h) {
   Image image = getImage(s);
   double xs = scaleX(x);
   double ys = scaleY(y);
   if (w < 0) throw new RuntimeException("width is negative: " + w);
   if (h < 0) throw new RuntimeException("height is negative: " + h);
   double ws = factorX(w);
   double hs = factorY(h);
   if (ws < 0 || hs < 0) throw new RuntimeException("image " + s + " is corrupt");
   if (ws <= 1 && hs <= 1) pixel(x, y);
   else {
     offscreen.drawImage(
         image,
         (int) Math.round(xs - ws / 2.0),
         (int) Math.round(ys - hs / 2.0),
         (int) Math.round(ws),
         (int) Math.round(hs),
         null);
   }
   draw();
 }
Example #22
0
  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};
  }
Example #23
0
 public void paint(Graphics g) {
   double beatWidth = sp.beatWidth;
   g.setFont(font);
   for (int i = 0; i < (sp.score.getEndTime()); i++) {
     int xLoc = (int) Math.round(i * beatWidth);
     if (i % timeSig == 0) {
       g.drawLine(xLoc, 0, xLoc, height);
       if (beatWidth > 15) g.drawString("" + i, xLoc + 2, height - 2);
     } else {
       g.drawLine(xLoc, height / 2, xLoc, height);
     }
   }
 }
Example #24
0
  @Override
  public void mouseDragged(MouseEvent e) {
    // if (spawnMenuItem.isEnabled()) {
    int newX = (int) Math.round(e.getX() / (double) game.getPixelSize());
    int newY = (int) Math.round(e.getY() / (double) game.getPixelSize());

    if (!game.isInBounds(newX, newY)
        || (newX == placeX && newY == placeY)
        || (game.getEntity(newX, newY) == null == erase)) {

      return;
    }

    placeX = newX;
    placeY = newY;

    if (erase) {
      game.removeEntity(newX, newY);
    } else {
      game.placeEntity(newX, newY, spawnID);
    }
    repaint();
    // }
  }
Example #25
0
    public Component getTableCellRendererComponent(
        JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
      if (isSelected) this.setBackground(table.getSelectionBackground());
      else this.setBackground(table.getBackground());

      Node node = nodes.get(row);
      Request request = nodeIDToRequest.get(node.getNodeID());
      if (request == null) {
        this.setString("NONE");
        this.setValue(0);
      } else {
        int percent = (int) Math.round(request.getPercentDone());
        this.setString(request.getType().toString() + "  " + percent + "%");
        this.setValue(percent);
      }
      return this;
    }
Example #26
0
  public void saveNewImage(MapContent map, String file) {

    GTRenderer renderer = new StreamingRenderer();
    renderer.setMapContent(map);
    System.out.println("line 139");
    Rectangle imageBounds = null;
    try {
      ReferencedEnvelope mapBounds = map.getMaxBounds();

      double heightToWidth = mapBounds.getSpan(1) / mapBounds.getSpan(0);
      int imageWidth = 600;

      imageBounds = new Rectangle(0, 0, imageWidth, (int) Math.round(imageWidth * heightToWidth));
    } catch (Exception e) {

    }
    System.out.println("line 151");
    //	    Rectangle imageSize = new Rectangle(600,600);

    BufferedImage image =
        new BufferedImage(
            imageBounds.width, imageBounds.height, BufferedImage.TYPE_INT_RGB); // darker red fill

    Graphics2D gr = image.createGraphics();
    gr.setPaint(Color.WHITE);
    gr.fill(imageBounds);

    try {
      System.out.println("line 161");
      renderer.paint(gr, imageBounds, map.getMaxBounds());
      System.out.println("line 163");

      File fileToSave = new File(file);
      System.out.println("line 166");
      ImageIO.write(image, "jpeg", fileToSave);
      System.out.println("line 168");
    } catch (IOException e) {

    }
  }
Example #27
0
 public static int scaled(int k) {
   return Math.round(k * RES_RATIO);
 }
Example #28
0
 /**
  * Draw one pixel at (x, y).
  *
  * @param x the x-coordinate of the pixel
  * @param y the y-coordinate of the pixel
  */
 private static void pixel(double x, double y) {
   offscreen.fillRect((int) Math.round(scaleX(x)), (int) Math.round(scaleY(y)), 1, 1);
 }
Example #29
0
  private ArrayList<RSTile> generatePath(Line[] lines) {
    double minStep = 5, maxStep = 10, wander = 3;
    if (lines.length < 2) return null;
    ArrayList<RSTile> path = new ArrayList<RSTile>();
    Line l1, l2 = lines[0];
    double distFromCenter = random(0, l2.getDistance() + 1);
    RSTile p = l2.translate((int) distFromCenter);
    distFromCenter = l2.getDistance() / 2 - distFromCenter;
    double centerXdist, centerYdist, line1Xdist, line1Ydist, line2Xdist, line2Ydist;
    double line1dist, line2dist, centerDist;
    double x, y;
    double distOnLine, last, cap1, cap2, move;
    double distFromCenterX1, distFromCenterY1, distFromCenterX2, distFromCenterY2;
    double force1, force2, slopeX, slopeY, slopeDist;
    boolean finished;
    int lastX = p.getX(), lastY = p.getY(), curX, curY;
    double dist, xdist, ydist;
    for (int i = 1; i < lines.length; i++) {
      l1 = l2;
      l2 = lines[i];
      centerXdist = l2.getCenterX() - l1.getCenterX();
      centerYdist = l2.getCenterY() - l1.getCenterY();
      centerDist = Math.sqrt(centerXdist * centerXdist + centerYdist * centerYdist);
      line1Xdist = l2.getX() - l1.getX();
      line1Ydist = l2.getY() - l1.getY();
      line2Xdist = l2.getX2() - l1.getX2();
      line2Ydist = l2.getY2() - l1.getY2();

      centerXdist /= centerDist;
      centerYdist /= centerDist;
      line1Xdist /= centerDist;
      line1Ydist /= centerDist;
      line2Xdist /= centerDist;
      line2Ydist /= centerDist;
      distOnLine = 0;
      last = 0;
      finished = false;
      while (!finished) {

        distOnLine += random(minStep, maxStep);
        if (distOnLine >= centerDist) {
          distOnLine = centerDist;
          finished = true;
        }
        x = centerXdist * distOnLine + l1.getCenterX();
        y = centerYdist * distOnLine + l1.getCenterY();

        distFromCenterX1 = x - (line1Xdist * distOnLine + l1.getX());
        distFromCenterY1 = y - (line1Ydist * distOnLine + l1.getY());

        distFromCenterX2 = x - (line2Xdist * distOnLine + l1.getX2());
        distFromCenterY2 = y - (line2Ydist * distOnLine + l1.getY2());

        slopeX = distFromCenterX2 - distFromCenterX1;
        slopeY = distFromCenterY2 - distFromCenterY1;
        slopeDist = Math.sqrt(slopeX * slopeX + slopeY * slopeY);
        slopeX /= slopeDist;
        slopeY /= slopeDist;

        line1dist =
            Math.sqrt(distFromCenterX1 * distFromCenterX1 + distFromCenterY1 * distFromCenterY1);
        line2dist =
            Math.sqrt(distFromCenterX2 * distFromCenterX2 + distFromCenterY2 * distFromCenterY2);

        move = (distOnLine - last) / maxStep * wander;

        force1 = line1dist + distFromCenter;
        force2 = line2dist - distFromCenter;

        cap1 = Math.min(move, force1);
        cap2 = Math.min(move, force2);

        if (force1 < 0) distFromCenter -= force1;
        else if (force2 < 0) distFromCenter += force2;
        else distFromCenter += random(-cap1, cap2);

        if (finished) {
          RSTile t = l2.translateFromCenter(distFromCenter);
          curX = t.getX();
          curY = t.getY();
        } else {
          curX =
              (int)
                  Math.round(distOnLine * centerXdist + l1.getCenterX() + distFromCenter * slopeX);
          curY =
              (int)
                  Math.round(distOnLine * centerYdist + l1.getCenterY() + distFromCenter * slopeY);
        }

        xdist = curX - lastX;
        ydist = curY - lastY;
        dist = Math.sqrt(xdist * xdist + ydist * ydist);
        xdist /= dist;
        ydist /= dist;
        for (int j = 0; j < dist; j++)
          path.add(
              new RSTile((int) Math.round(xdist * j + lastX), (int) Math.round(ydist * j + lastY)));

        last = distOnLine;
        lastX = curX;
        lastY = curY;
      }
    }
    return cutUp(path);
  }
Example #30
0
 public RSTile translateFromCenter(double length) {
   return new RSTile(
       (int) Math.round(centerX - (xdist / dist) * length),
       (int) Math.round(centerY - (ydist / dist) * length));
 }