Noble Master Games
Noble Master Games
Navigation
Categories:

Search:

Meta:

Archives:
Dev. Blog
Forums
 

Noble Master Developer's Blog

August 31, 2010

Star Rating Panel for Java Swing

Filed under: Development — noblemaster @ 09:32
Star Rating

Star Rating

I couldn’t find a good star rating component for Java Swing, so I rolled my own. The star rating component displays the current current rating (yellow stars), as well as a “selected” rating (reddish stars). The “selected” rating can be changed by a user. If the “selected” rating is changed, all the listeners will be notified of the change.

Example (5 stars, 3.78 average rating, 3 stars initial selection):

  StarRater starRater = new StarRater(5, 3.61, 3);
  starRater.addStarListener(new StarRater.StarListener() {
    public void handleSelection(int selection) {
      // a new number of stars has been selected
      do something...
    }
  });
  add(starRater);   // add the component to the container...

The star rating component will initially be used to rate maps in Age of Conquest.

Here is the download for both source code & star images (Photoshop). Note that I included the star images as byte array in the source code for your convenience. You are obviously free to use your own as well. Please consider both source code as well as images in the Public Domain. If you would like to credit me for the work that’s great, otherwise, no problem either:

10 Comments »

  1. Cool, sounds good. Is the star rating for the visual map quality or the overall playability (balance and map strategies)? Or will there be a star rating for both?

    Comment by fodder — August 31, 2010 @ 19:53

  2. There will only be one rating. I figured, in the end it all comes down how fun the map is to play overall.

    Comment by noblemaster — August 31, 2010 @ 21:41

  3. Thank you! This is exactly what I was looking for.

    Comment by Christie — January 18, 2011 @ 10:46

  4. In order to support background color, you should add the following 3 lines to the start of the paintComponent method:

    g.setColor(getBackground());
    g.fillRect(0, 0, getWidth(), getHeight());
    g.setColor(getForeground());

    Comment by Han — March 13, 2012 @ 01:10

  5. Hi. How can i use my own images?

    Comment by Namiq — August 14, 2013 @ 17:14

  6. The images are currently encoded as byte array, so you won’t need to worry about loading them from the file system. However, if you like you can use your own images as well! There are 4 types of images:
    – star background: empty background
    – star foreground: indicates the current rating
    – star rollover: when the mouse is over the star
    – star selected: when the mouse button is pressed

    Simply delete the hex-images at the bottom of the file and use your own images instead. The are all in java.awt.Image format.

    Comment by noblemaster — August 15, 2013 @ 01:48

  7. Man thanks a lot, that is the component i was lookink for, thanks

    Comment by dairon — September 20, 2013 @ 07:30

  8. Hey friend, how did you convert the image to hex byte array? I mean..
    I would like to save the images as array to, but I donĀ“t know how to do that:(

    Comment by MaRio — August 6, 2014 @ 01:57

  9. To convert an image to a byte-array do the following:
    1. Load the image via File into a byte-array.
    2. Then use System.out.print(…) to print the single bytes out.
    3. Copy/Paste the output into your program.

    I hope that about explains it :)

    Comment by noblemaster — August 6, 2014 @ 02:08

  10. Oh I understand! Thank you and thanks for the component, it’s so great!

    Comment by MaRio — August 6, 2014 @ 03:12

Leave a comment