Skip to content

Gravatar with JavaFX

Gravatar is easy way to put global recognized avatar images into any Internet application. Gravatar would stands for globally recognized avatar.

Below,  the Java class that I got from the Gravatar Java reference. Here is a static class called md5 that applies a MD5Sum algorithm over a string. Is a little complex code but all behavior keeps encapsulated and who uses it don’t need to know how it works. Just gives a string and receives a encrypted string. Those two codes are also a good example of how calling Java classes inside a JavaFX code.

package gravatarexample;
 
import java.security.MessageDigest;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
 
public class MD5 {
   public static String toHex(String message) {
      try {
         MessageDigest md = MessageDigest.getInstance("MD5");
         byte array[] = md.digest(message.getBytes("CP1252"));
         StringBuffer sb = new StringBuffer();
         for (int i = 0; i < array.length; ++i) {
            sb.append(Integer.toHexString((array[i]&0xFF)|0x100).substring(1, 3));
         }
         return sb.toString();
      } catch (NoSuchAlgorithmException e) {
      } catch (UnsupportedEncodingException e) {
      }
      return null;
   }
}

As a Java class in the same package, any JavaFX (or Java) code can call it without any problem. Just to keep the code more clear I’m importing it explicitly. Is this example I also create some Swing interface to give user the option to put his mail, adjust the image size and get a output direct link or html image tag.

package gravatarexample;
 
import gravatarexample.MD5;
import javafx.ext.swing.SwingButton;
import javafx.ext.swing.SwingSlider;
import javafx.ext.swing.SwingTextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.scene.Scene;
import javafx.stage.Stage;
 
var mail = "Email";
var key = "";
 
function gravatalize(mail:String, size: Integer): String {
   return "http://www.gravatar.com/avatar/{MD5.toHex(mail)}?s={size}"
}
 
var inputtxt = SwingTextField {
   columns: 20
   text: mail
}
 
var slider = SwingSlider {
   minimum: 10
   maximum: 128
   value: 100
   vertical: false
}
 
var button = SwingButton {
   text: "Get Gravatar"
   action: function() {
      key = gravatalize(inputtxt.text, slider.value);
      directoutput.text = key;
      htmloutput.text = "<img src="{key}" alt="\&quot;gravatar\&quot;" />";
      photo.image = Image {
         backgroundLoading: true,
         url: key};
   }
}
 
var photo:ImageView = ImageView {
   image: null
}
 
var directoutput = SwingTextField {
   columns: 20
   text: "direct link image"
 
}
 
var htmloutput = SwingTextField {
   columns: 20
   text: "html tag image"
}
 
Stage {
   title: "Gravatar"
   width: 300
   height: 340
   scene: Scene {
      content: [
         VBox {
            spacing: 10
            content: [inputtxt, slider, button, directoutput, htmloutput, photo]
         },
      ]
   }
}

The string itself is assembled in the gravatalize function. You give a mail and it’s returns a Gravatar direct link to the image. There’s many cool ways to use together Gravatar and a JavaFX Internet application.

Published inenglish

28 Comments

  1. Hi,

    really nice, would be helpful for some social networking application.

  2. […] by apokalyptik Silveira Neto write in to let us know that he has written an article covering how to use gravatar with JavaFX.  Thanks for the info, Silveira, I’m sure many people will find this to be very […]

  3. […] Neto write in to let us know that he has written an article covering how to use gravatar with JavaFX.  Thanks for the info, Silveira, I’m sure many people will find this to be very […]

  4. Actually I’m confusing about this. May you write in detail Gravatar with JavaFX what for, how it work, and what benefit with it? Sorry to trouble you. Thanks for the info.

  5. @iskandarX With JavaFX you can create a broad kind of dynamic animated internet applications. You can mix JavaFX and Gravatar to create applications where you can easily get a image avatar of your user and user friends, making it more social. That’s the benefit of Gravatar, not JavaFX. This example I wrote show you how to get this benefit using JavaFX.

  6. so what exactly would the diffrence be , it seems complicated using html

  7. Cool, it’s totally above my level of understanding. Way to go guys, only a PhD could understand this code.

  8. Great article! Thanks for this sharing..

  9. Thanks for the detailed tutorial Silveira..

  10. Thanks for the great information, it was very helpful.

  11. Great article! Thanks for this sharing…

  12. Yes! its work like a a carm, thanks for your great shareing…

  13. ANTONIO JOSE DOMINGUEZ M. ANTONIO JOSE DOMINGUEZ M.

    MI CORDIAL SALUDO, DESDE VENEZUELA.

  14. Very good article! thank you to the author for it! In it interesting and useful information it is possible often times re-read it! I will advise to read it all friends. It will be very useful at writing of the article . Very much thankful you. :) good

  15. I used google translate because my English so bad. Hard to understand this topic, but I think this great article

  16. ticle! thank you to the author for it! In it interesting and useful information it is possible often times re-read it! that are ypunged responsed moured the lokilles

  17. Hard to understand this topic

  18. seen your comment thanked of your lakasted nice to very muchedishes slamped of youred

  19. secret thanked of your madamer nice to very muched setringed on the rempet of your lasted

    • Sorry @wondermommy!Gravaatr is a global avaatr system… can you change the email address you use for this site, so it’s different from your other ones?If not, you’re probably best off not uploading an avaatr for now… sorry!

  20. more youred thanked of your lasted tecrnet you slepped of your the ambledder nice to very muchedishes

  21. ticle! thank you to the author for it! In it interesting and useful information it is possible often times re-read it! that are ypunged responsed moured the lokilles

Leave a Reply to Biswajit Cancel reply

Your email address will not be published. Required fields are marked *