frame.setIconImage()

I was curious as to how to change the frame icon of an applet upon runtime. I couldn't find an example of how to do it in Processing, so here is an example sketch.

PGraphics pg;    

color background = 0;
color foreground = color(255);


void setup()
{
  size(300, 300);
  pg = createGraphics(width, height);

}

void draw()
{
  background(0);
  frame.setTitle("Framerate: " + frameRate);    //I can't code... getting 5-15 fps on my system...
  float fC = (float) frameCount;
  background = color( 255* (sin( fC*0.01)*0.5+0.5), 255* (sin( fC*0.01 + TWO_PI*0.3333)*0.5+0.5), 255* (sin( fC*0.01 + TWO_PI*0.6666)*0.5+0.5));
  foreground = color( 255-255* (sin( fC*0.01)*0.5+0.5), 255-255* (sin( fC*0.01 + TWO_PI*0.3333)*0.5+0.5), 255-255* (sin( fC*0.01 + TWO_PI*0.6666)*0.5+0.5));

  //Creating some pixels:
  pg.loadPixels();
  for (int i = 0; i < pg.width; i++)
  {
    for (int j = 0; j < pg.height; j++)
    {
      int dist = (int) (pow(i-mouseX, 2) + pow(j-mouseY, 2));
      //Abuse negative colour values:
      color colour = color(((background >> 16) & 0xFF)-((foreground>>16)&0xFF)*dist*0.01*(sin(dist*0.01+frameCount*0.01)*0.5+0.5), 
      ((background >> 8) & 0xFF)-((foreground>>8)&0xFF)*dist*0.01*(sin(dist*0.01+TWO_PI*0.3333+frameCount*0.01)*0.5+0.5), 
      (background  & 0xFF)-((foreground)&0xFF)*dist*0.01*(sin(dist*0.01+TWO_PI*0.6666+frameCount*0.01)*0.5+0.5));
      pg.pixels[i+width*j] = colour;    //set the pixels for the PGraphics

    }
  }
  pg.updatePixels();

  image(pg, 0, 0);    //Render the PGraphics to the PApplet
  frame.setIconImage(pg.image);    //Render the PGraphics to the Windows icon, PGraphics.image is a java.awt.Image field which is what the setIconImage method needs.
}

Comments

  • "I couldn't find an example of how to do it in Processing"
    Really?
    https://www.google.com/search?as_sitesearch=processing.org&as_q=frame.setIconImage

  • Your Google skills are better than mine :P

  • edited September 2014

    Admittedly, that's easier to search once you know the name of the function... :-P
    I pointed out just to show some previous work, posting it again isn't a problem, it increases the chances to find out for other people.

    Note that the search was made from processing.org, it automatically restricts the scope to its site.

  • Indeed, I did not do that. But in my search I did encounter the first result of your Google search but discarded it as outdated since the thread was more than seven years old. But it also has this thread: http://processing.org/discourse/beta/num_1234945669.html and the method described there still works! It uses the image field of a PGraphics object. I didn't realise a PGraphics has this field (despite me going through the PGraphics source for a couple days for another project - embarrassing! - but in my defense PGraphics is a very large class with lots of fields and methods, I did search for a method which returns an Image though... )

    So you can use this Image field directly instead of creating a java.awt.bufferedImage. It increases the frame rate as well. Updated the opening post.

  • edited September 2014

    It uses the image field of a PGraphics object. I didn't realize a PGraphics had this field...

    The Image image field isn't PGraphics's but its super class PImage is in PGraphics indeed!

    https://github.com/processing/processing/blob/master/core/src/processing/core/PImage.java#L28

    https://github.com/processing/processing/blob/master/core/src/processing/core/PGraphics.java#L507

    /**
     * Java AWT Image object associated with this renderer. For the 1.0 version
     * of P2D and P3D, this was be associated with their MemoryImageSource.
     * For PGraphicsJava2D, it will be the offscreen drawing buffer.
     */
    
    public Image image;
    

    To make the quest harder, it's not initialized in any of those, but within some of the PGraphics's subclasses like the PGraphicsJava2D as a new BufferedImage! 8-}

    ... but discarded it as outdated since the thread was more than 7 years old.

    Well, Java's AWT is that old or more! :-)) In the end, it's a Java's feature, not Processing's! :P

  • Well, Java's AWT is that old or more! ) In the end, it's a Java's feature, not Processing's!

    But the PGraphics implementation is different since 2007... or not?

    The Image image field isn't PGraphics's but its super class PImage!

    Are you sure of that? I can't find a java.awt.image.Image field anywhere inside the PImage source code or javadoc! There is a getImage() method which is deprecated and there is a PImage constructor which takes an Image as an argument but that's it. Your link only points to the import of java.awt.image.*.

  • edited September 2014

    Are you sure of that?

    Oops! I guess after much search and multiple browser tabs opened, I've got it mixed up! 8-}

    But the PGraphics implementation is different since 2007... or not?

    As long they keep on relying on Java's AWT, those internal things gonna be the same eternally! >:)

Sign In or Register to comment.