TITLE:RGBImageFilterでアイコンの色調を変更

Posted by at 2006-08-21

RGBImageFilterでアイコンの色調を変更

RGBImageFilterで色調を変更したアイコンの用意し、評価用コンポーネントを作成します。

RatingLabel.png

サンプルコード

private final ImageProducer ip = orgIcon.getImage().getSource();
private ImageIcon makeStarImageIcon(float[] filter) {
  SelectedImageFilter sif = new SelectedImageFilter(filter);
  return new ImageIcon(
    createImage(new FilteredImageSource(ip, sif)));
}
private class SelectedImageFilter extends RGBImageFilter {
  private final float[] filter;
  public SelectedImageFilter(float[] filter) {
    this.filter = filter;
    canFilterIndexColorModel = false;
  }
  @Override public int filterRGB(int x, int y, int argb) {
    int r = (int)(((argb >> 16) & 0xff) * filter[0]);
    int g = (int)(((argb >>  8) & 0xff) * filter[1]);
    int b = (int)(((argb      ) & 0xff) * filter[2]);
    return (argb & 0xff000000) | (r<<16) | (g<<8) | (b);
  }
}
View in GitHub: Java, Kotlin

解説

上記のサンプルは、RGBImageFilterを使用して、一つのアイコンから複数の色の異なるアイコンを生成し、5段階の評価を行うコンポーネントを作成しています。クリックしたアイコンの位置が評価レベルになります。

参考リンク

コメント

  • 素晴しい!:)--
  • メモ: 一般的?にはRating Barと言うみたいです。Masuga Design » Unobtrusive AJAX Star Rating Bar -- aterai
  • アイコンを変更、アイコンの間隔を設定 -- aterai
  • スクリーンショットを更新 -- aterai