TITLE:JProgressBarにUIを設定してインジケータの色を変更
Posted by at 2012-10-08

JProgressBarにUIを設定してインジケータの色を変更

`JProgressBar`のインジケータの色を進行に応じてパレットから取得した色に変更します。
  • category: swing folder: GradientPalletProgressBar title: JProgressBarにUIを設定してインジケータの色を変更 tags: [JProgressBar, PixelGrabber, LinearGradientPaint] author: aterai pubdate: 2012-10-08T15:21:36+09:00 description: JProgressBarのインジケータの色を進行に応じてパレットから取得した色に変更します。 image: https://lh5.googleusercontent.com/-EjSzEK0Wc6g/UHJrTUTxG9I/AAAAAAAABT8/4AKSHxe6PNE/s800/GradientPalletProgressBar.png hreflang:
       href: https://java-swing-tips.blogspot.com/2015/05/dynamically-change-color-of.html
       lang: en

概要

JProgressBarのインジケータの色を進行に応じてパレットから取得した色に変更します。
GradientPalletProgressBar.png

サンプルコード

サンプルコード

#spandel
class GradientPalletProgressBarUI extends BasicProgressBarUI{
#spanend
#spanadd
class GradientPalletProgressBarUI extends BasicProgressBarUI {
#spanend
  private final int[] pallet;
  public GradientPalletProgressBarUI() {
  protected GradientPalletProgressBarUI() {
    super();
    this.pallet = makeGradientPallet();
  }
#spanadd

#spanend
  private static int[] makeGradientPallet() {
    BufferedImage image = new BufferedImage(100, 1, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2  = image.createGraphics();
    Point2D start  = new Point2D.Float(0f, 0f);
    Point2D end  = new Point2D.Float(99f, 0f);
    float[] dist   = {0.0f, 0.5f, 1.0f};
    Color[] colors = { Color.RED, Color.YELLOW, Color.GREEN };
    Point2D start  = new Point2D.Float();
    Point2D end    = new Point2D.Float(99, 0);
    float[] dist   = {0f, .5f, 1f};
    Color[] colors = {Color.RED, Color.YELLOW, Color.GREEN};
    g2.setPaint(new LinearGradientPaint(start, end, dist, colors));
    g2.fillRect(0, 0, 100, 1);
    g2.dispose();

    int width  = image.getWidth(null);
    int width = image.getWidth(null);
    int[] pallet = new int[width];
    PixelGrabber pg = new PixelGrabber(image, 0, 0, width, 1, pallet, 0, width);
    try{
    try {
      pg.grabPixels();
    }catch(Exception e) {
      e.printStackTrace();
    } catch (InterruptedException ex) {
      ex.printStackTrace();
    }
    return pallet;
  }
#spanadd

#spanend
  private static Color getColorFromPallet(int[] pallet, float x) {
    if(x < 0.0 || x > 1.0) {
    if (x < 0f || x > 1f) {
      throw new IllegalArgumentException("Parameter outside of expected range");
    }
    int i = (int)(pallet.length * x);
    int max = pallet.length-1;
    int index = i<0?0:i>max?max:i;
    int pix = pallet[index] & 0x00ffffff;
    return new Color(pix);
    int i = (int) (pallet.length * x);
    int max = pallet.length - 1;
    int index = Math.min(Math.max(i, 0), max);
    return new Color(pallet[index] & 0x00_FF_FF_FF);
    // translucent
    // int pix = pallet[index] & 0x00_FF_FF_FF | (0x64 << 24);
    // return new Color(pix), true);
  }
#spanadd

#spanend
  @Override public void paintDeterminate(Graphics g, JComponent c) {
    Insets b = progressBar.getInsets(); // area for border
    int barRectWidth  = progressBar.getWidth()  - (b.right + b.left);
    int barRectHeight = progressBar.getHeight() - (b.top + b.bottom);
    if(barRectWidth <= 0 || barRectHeight <= 0) {
    int barRectWidth  = progressBar.getWidth()  - b.right - b.left;
    int barRectHeight = progressBar.getHeight() - b.top   - b.bottom;
    if (barRectWidth <= 0 || barRectHeight <= 0) {
      return;
    }
    // int cellLength = getCellLength();
    // int cellSpacing = getCellSpacing();
    // amount of progress to draw
    int amountFull = getAmountFull(b, barRectWidth, barRectHeight);

    if(progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
      // draw the cells
      float x = amountFull / (float)barRectWidth;
    // draw the cells
    if (progressBar.getOrientation() == SwingConstants.HORIZONTAL) {
      float x = amountFull / (float) barRectWidth;
      g.setColor(getColorFromPallet(pallet, x));
      g.fillRect(b.left, b.top, amountFull, barRectHeight);
#spandel

#spanend
    }else{ // VERTICAL
      //...
    } else { // VERTICAL
      float y = amountFull / (float) barRectHeight;
      g.setColor(getColorFromPallet(pallet, y));
      g.fillRect(b.left, barRectHeight + b.bottom - amountFull, barRectWidth, amountFull);
    }
    // Deal with possible text painting
    if(progressBar.isStringPainted()) {
    if (progressBar.isStringPainted()) {
      paintString(g, b.left, b.top, barRectWidth, barRectHeight, amountFull, b);
    }
  }
}
View in GitHub: Java, Kotlin

解説

上記のサンプルでは高さ1pxの画像をパレットとして予め作成し、それから`JProgressBarの進捗に応じた色を取得して、BasicProgressBarUI#paintDeterminate(...)`内で使用することで、色の変更を行なっています。

解説

上記のサンプルでは高さ1pxの画像をパレットとして予め作成し、それからJProgressBarの進捗に応じた色を取得してBasicProgressBarUI#paintDeterminate(...)内で使用することで色の変更を行なっています。

参考リンク

参考リンク

コメント

コメント