Ejemplo n.º 1
0
  /**
   * Sets one or more icons for the Display.
   *
   * <ul>
   *   <li>On Windows you should supply at least one 16x16 icon and one 32x32.
   *   <li>Linux (and similar platforms) expect one 32x32 icon.
   *   <li>Mac OS X should be supplied one 128x128 icon
   * </ul>
   *
   * The implementation will use the supplied ByteBuffers with image data in RGBA and perform any
   * conversions nescesarry for the specific platform.
   *
   * @param icons Array of icons in RGBA mode
   * @return number of icons used.
   */
  public int setIcon(ByteBuffer[] icons) {
    boolean done_small = false;
    boolean done_large = false;
    int used = 0;

    int small_icon_size = 16;
    int large_icon_size = 32;
    for (ByteBuffer icon : icons) {
      int size = icon.limit() / 4;

      if ((((int) Math.sqrt(size)) == small_icon_size) && (!done_small)) {
        long small_new_icon = createIcon(small_icon_size, small_icon_size, icon.asIntBuffer());
        sendMessage(hwnd, WM_SETICON, ICON_SMALL, small_new_icon);
        freeSmallIcon();
        small_icon = small_new_icon;
        used++;
        done_small = true;
      }
      if ((((int) Math.sqrt(size)) == large_icon_size) && (!done_large)) {
        long large_new_icon = createIcon(large_icon_size, large_icon_size, icon.asIntBuffer());
        sendMessage(hwnd, WM_SETICON, ICON_BIG, large_new_icon);
        freeLargeIcon();
        large_icon = large_new_icon;
        used++;
        done_large = true;

        // Problem: The taskbar icon won't update until Windows sends a WM_GETICON to our window
        // proc and we reply. But this method is usually called
        // on init and it might take a while before the next call to nUpdate (because of resources
        // being loaded, etc). So we wait for the next
        // WM_GETICON message (usually received about 100ms after WM_SETICON) to make sure the
        // taskbar icon has updated before we return to the user.
        // (We wouldn't need to do this if the event loop was running continuously on its own
        // thread.)
        iconsLoaded = false;

        // Track how long the wait takes and give up at 500ms, just in case.
        long time = System.nanoTime();
        long MAX_WAIT = 500L * 1000L * 1000L;
        while (true) {
          nUpdate();
          if (iconsLoaded || MAX_WAIT < System.nanoTime() - time) break;

          Thread.yield();
        }
      }
    }

    return used;
  }
  private void readPalette() {
    RandomAccessFile rIn = null;
    ByteBuffer buf = null;
    int i;
    try {
      if (paletteFile != null) {
        // see if the file exists, if not, set it to the default palette.
        File file = new File(paletteFile);

        numPaletteEntries = (int) (file.length() / 4);

        buf = ByteBuffer.allocate(numPaletteEntries * 4);

        rIn = new RandomAccessFile(paletteFile, "r");

        FileChannel inChannel = rIn.getChannel();

        inChannel.position(0);
        inChannel.read(buf);

        // Check the byte order.
        buf.order(ByteOrder.LITTLE_ENDIAN);

        buf.rewind();
        IntBuffer ib = buf.asIntBuffer();
        paletteData = new int[numPaletteEntries];
        ib.get(paletteData);
        ib = null;
      }

    } catch (Exception e) {
      System.err.println("Caught exception: " + e.toString());
      System.err.println(e.getStackTrace());
    } finally {
      if (rIn != null) {
        try {
          rIn.close();
        } catch (Exception e) {
        }
      }
    }
  }
Ejemplo n.º 3
0
  /**
   * Reads an image from an archived file and return it as ByteBuffer object.
   *
   * @author Mike Butler, Kiet Le
   */
  private ByteBuffer readImage(String filename, Dimension dim) {
    if (dim == null) dim = new Dimension(0, 0);
    ByteBuffer bytes = null;
    try {
      DataInputStream dis =
          new DataInputStream(getClass().getClassLoader().getResourceAsStream(filename));
      dim.width = dis.readInt();
      dim.height = dis.readInt();
      System.out.println("Creating buffer, width: " + dim.width + " height: " + dim.height);
      // byte[] buf = new byte[3 * dim.height * dim.width];
      bytes = BufferUtil.newByteBuffer(3 * dim.width * dim.height);
      for (int i = 0; i < bytes.capacity(); i++) {
        bytes.put(dis.readByte());
      }
      dis.close();

    } catch (Exception e) {
      e.printStackTrace();
    }
    bytes.rewind();
    return bytes;
  }
Ejemplo n.º 4
0
 public static byte[] getNBOPort(short p) {
   ByteBuffer buf = ByteBuffer.allocate(2);
   buf.putShort((short) p);
   buf.order(ByteOrder.BIG_ENDIAN);
   buf.rewind();
   byte[] rtn = new byte[2];
   buf.get(rtn);
   return rtn;
 }
Ejemplo n.º 5
0
  /**
   * Check if the first X characters of a byte stream match a String.
   *
   * @param data The byte array to process
   * @param pattern The String to match
   * @return True if the pattern was found, false otherwise
   */
  private static boolean bytesEqualsString(byte[] data, String pattern) {
    byte[] bytes = new byte[pattern.length()];
    Charset csets = Charset.forName("US-ASCII");
    boolean fin = false;
    int currChar = 0;

    // remove any CR and/or LF characters at the beginning of the article
    // data
    while (!fin) {
      if (currChar >= data.length) break;

      byte in = data[currChar];
      ByteBuffer bb = ByteBuffer.wrap(new byte[] {(byte) in});
      CharBuffer cb = csets.decode(bb);
      char c = cb.charAt(0);

      if (data.length > 0 && (c == '\n' || c == '\r')) currChar++;
      else fin = true;

      if (data.length == 0) fin = true;
    }

    // extract bytes (chars) to check from article data
    for (int i = 0; i < bytes.length && i < data.length; i++, currChar++) {
      byte in = data[currChar];
      bytes[i] = (byte) in;
    }

    // decode byte data to characters
    ByteBuffer bb = ByteBuffer.wrap(bytes);
    CharBuffer cb = csets.decode(bb);

    // compare these characters to the pattern String
    for (int i = 0; i < pattern.length(); i++) if (cb.charAt(i) != pattern.charAt(i)) return false;

    return true;
  }
Ejemplo n.º 6
0
 public static byte[] getNBOHost(String h) {
   if (h.equals(PEER)) return null;
   byte hostBytes[] = h.getBytes();
   int len = Array.getLength(hostBytes);
   ByteBuffer buf = ByteBuffer.allocate(len);
   buf.put(hostBytes);
   buf.order(ByteOrder.BIG_ENDIAN);
   buf.rewind();
   return (buf.array());
 }
Ejemplo n.º 7
0
  public void init(GLAutoDrawable drawable) {
    GL gl = drawable.getGL();

    float m[] = {
      0.0f, 1.0f, 0.0f, 0.0f, //
      0.0f, 0.0f, 1.0f, 0.0f, //
      1.0f, 0.0f, 0.0f, 0.0f, //
      0.0f, 0.0f, 0.0f, 1.0f
    };

    pixels = readImage("Data/leeds.bin", dim);
    System.out.println(pixels.toString());

    gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

    gl.glMatrixMode(GL.GL_COLOR);
    gl.glLoadMatrixf(m, 0);
    gl.glMatrixMode(GL.GL_MODELVIEW);
  }