Exemple #1
0
  public static ImageIcon getImageFromURL(String url) {
    String[] splitName = url.split("/");
    String name = "unknown";
    try {
      name =
          URLDecoder.decode(
              splitName[splitName.length - 1], Charset.defaultCharset().displayName());
    } catch (UnsupportedEncodingException uee) {
      uee.printStackTrace();
      logger.error("Error when decoding image URL", uee);
    }
    if (name.indexOf('?') != -1) {
      splitName = name.split("\\?");
      name = splitName[0];
    }

    // We get the extension for ImageIO
    splitName = url.split("\\.");
    String typeExt = splitName[splitName.length - 1];

    File tmpDir = new File(tmpImagePath);

    // We check if we have already the file in order to not download it twice
    for (File f : tmpDir.listFiles()) {
      if (f.getName().equals(name)) {
        ImageIcon ii = new ImageIcon(tmpImagePath + PropertyManager.sep + name);
        ii.setDescription(tmpImagePath + PropertyManager.sep + name);
        return ii;
      }
    }

    BufferedImage image = null;
    try {
      image = ImageIO.read(new URL(url));
      File fileImage = new File(tmpDir + PropertyManager.sep + name);
      ImageIO.write(image, typeExt, fileImage);
      logger.debug("Picture downloaded : " + fileImage.getName());
    } catch (MalformedURLException mue) {
      mue.printStackTrace();
    } catch (FileNotFoundException fnfe) {
      logger.debug("404 Error : " + url);
    } catch (IOException ioe) {
      ioe.printStackTrace();
      logger.error("Error when fetching : " + url, ioe);
      MessagePaneManager.showCheckErrorPane(
          LocaleManager.getInstance().getString("connection_error"));
    }

    if (image != null) {
      ImageIcon ii = new ImageIcon(image);
      ii.setDescription(tmpImagePath + PropertyManager.sep + name);
      return ii;
    }
    return new ImageIcon();
  }
 @Test
 public void TestGetCellAt() {
   System.out.println("getCellAt");
   Image image =
       Toolkit.getDefaultToolkit().createImage(getClass().getResource("resources/x-icon.gif"));
   ImageIcon icon = new ImageIcon(image);
   icon.setDescription("X");
   instance.setValueAt(icon, 0, 0);
   String expResult = "X";
   String result = instance.getCellAt(0, 0);
   assertEquals(expResult, result);
 }
  private void cargarAutomataGraphViz() {
    this.imgUrl = this.generateImageUrl();
    GraphViz gv = new GraphViz();

    if (gv.testGraphViz()) {
      if (this.imgUrl.compareTo("<NO>") == 0) {
        this.imageLabel.setText("<El Path al Directorio de imagen es incorrecto>");
        this.imageLabel.setFont(new Font("Verdana", Font.BOLD, 14));
        this.imageLabel.setForeground(Color.orange);
      } else {

        boolean dibujar = this.primeraVez; // variable que indica que debemos generar el dibujo

        ImageIcon i = null;

        if (dibujar) { // Usar las imágenes guardadas
          i = this.dibujarNuevo();
          this.imagenOriginal = new File(this.imgUrl);
          this.primeraVez = false;
        } else {
          if (this.fileList != null && ((this.fileList.size() - 1) < this.IndexActual)) {
            dibujar = true;
            i = this.dibujarNuevo();
            File f = new File(this.imgUrl);
            this.fileList.add(f);
            this.HayArchivos = true;
          } else if (this.fileList != null) {
            dibujar = false; // no generar, utilizar uno ya creado.
            File f = this.fileList.get(this.IndexActual);
            i = new ImageIcon(f.getAbsolutePath());
          }
        }

        if (i != null) {
          i.setDescription("Automata Generado");
          this.imageLabel.setIcon(i);
        }
      }
    } else {
      this.imageLabel.setText("<GraphViz no esta instalado>");
      this.imageLabel.setFont(new Font("Verdana", Font.BOLD, 14));
      this.imageLabel.setForeground(Color.red);
    }
    this.imageLabel.setHorizontalAlignment(JLabel.CENTER);
    this.imageLabel.setVisible(true);
  }
  /**
   * <code>createTexture</code> takes the current height map and the current loaded textures and
   * produces an <code>ImageIcon</code> which can be retrieved with a call to <code>getImageIcon
   * </code>.
   */
  public void createTexture(int textureSize) {
    BufferedImage img = new BufferedImage(textureSize, textureSize, BufferedImage.TYPE_INT_RGB);
    DataBufferInt data = (DataBufferInt) img.getRaster().getDataBuffer();
    int[] pixels = data.getData();

    // tempvalues for the color
    int red = 0;
    int green = 0;
    int blue = 0;

    int tlSize = textureList.size();
    int twidths[] = new int[tlSize];
    int theights[] = new int[tlSize];
    for (int i = 0; i < tlSize; i++) {
      BufferedImage tempImg = textureList.get(i).imageData;
      twidths[i] = tempImg.getWidth();
      theights[i] = tempImg.getHeight();
    }

    int scaledX;
    int scaledZ;

    float mapRatio = (float) size / (float) textureSize;

    // check each pixel of the heightmap
    BufferedImage tempImg;
    float scalar;
    for (int x = 0; x < textureSize; x++) {
      for (int z = 0; z < textureSize; z++) {
        // combine every texture for this pixel
        for (int i = 0; i < tlSize; i++) {
          tempImg = textureList.get(i).imageData;
          data = (DataBufferInt) tempImg.getRaster().getDataBuffer();
          pixels = data.getData();

          // We may have to tile the texture if the terrain is
          // larger than the texture.
          scaledX = x % twidths[i];
          scaledZ = z % theights[i];

          // Retrieve the amount of the color to use for this
          // texture.
          scalar = getTextureScale(interpolateHeight(x, z, mapRatio), i);
          red += scalar * ((pixels[scaledZ * twidths[i] + scaledX] & 0x00FF0000) >> 16);
          green += scalar * ((pixels[scaledZ * twidths[i] + scaledX] & 0x0000FF00) >> 8);
          blue += scalar * ((pixels[scaledZ * twidths[i] + scaledX] & 0x000000FF));
        }

        // set the color for the final texture.
        int rgb = red << 16 | green << 8 | blue;
        img.setRGB(x, textureSize - (z + 1), rgb);

        red = 0;
        green = 0;
        blue = 0;
      }
    }

    // create the new image from the data.
    proceduralTexture = new ImageIcon(img);
    proceduralTexture.setDescription("TerrainTexture");

    logger.fine("Created procedural texture successfully.");
  }