• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:Separatorのグラデーション
#navi(../)
*Separatorのグラデーション [#m51f3588]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2004-03-29~
更新日:&lastmod;
---
category: swing
folder: Gradient
title: Separatorのグラデーション
tags: [JSeparator, GradientPaint, UIManager]
author: aterai
pubdate: 2004-03-29T04:50:14+09:00
description: GradientPaintを使ったグラデーションで、徐々に背景色に溶け込んでいくJSeparatorを作成します。
image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTNgUSWNnI/AAAAAAAAAa4/92SfYb-Z3zs/s800/Gradient.png
---
* 概要 [#summary]
`GradientPaint`を使ったグラデーションで、徐々に背景色に溶け込んでいく`JSeparator`を作成します。

#contents
#download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTNgUSWNnI/AAAAAAAAAa4/92SfYb-Z3zs/s800/Gradient.png)

**概要 [#r715b34a]
グラデーションするセパレータを作ってみます。
* サンプルコード [#sourcecode]
#code(link){{
class GradientSeparatorUI extends BasicSeparatorUI {
  private Color bgc, ssc, shc;

#screenshot
  public static ComponentUI createUI(JComponent c) {
    return new GradientSeparatorUI();
  }

**サンプルコード [#z73fe6d9]
 class GradientSeparator extends JComponent{
   public GradientSeparator() {
     super();
     setPreferredSize(new Dimension(100, 2));
     setBorder(null);
   }
   protected void paintComponent(Graphics g) {
     super.paintComponent(g);
     Graphics2D g2 = (Graphics2D)g;
     Insets ins = getInsets();
     g2.setPaint(new GradientPaint(0, 0,
                   SystemColor.controlShadow,
                   getWidth(), 0, SystemColor.control, false));
     g2.fillRect(0, 0, getWidth(), 1);
     g2.setPaint(new GradientPaint(0, 0,
                   SystemColor.controlLtHighlight,
                   getWidth(), 0, SystemColor.control, false));
     g2.fillRect(0, 1, getWidth(), 1);
   }
 }
  private void updateColors(JComponent j) {
    Color c = UIManager.getColor("Panel.background");
    bgc = c instanceof ColorUIResource ? c : j.getBackground();
    c = UIManager.getColor("Separator.shadow");
    ssc = c instanceof ColorUIResource ? c : j.getBackground().brighter();
    c = UIManager.getColor("Separator.highlight");
    shc = c instanceof ColorUIResource ? c : j.getBackground().darker();
  }

-&jnlp;
-&jar;
-&zip;
  @Override public void installUI(JComponent c) {
    super.installUI(c);
    updateColors(c);
  }

**解説 [#u88fd1d8]
上記のサンプルでは下のJSeparatorの描画にGradientPaintを使用し、グラデーションさせています。
  @Override public void paint(Graphics g, JComponent c) {
    if (c instanceof JSeparator) {
      Graphics2D g2 = (Graphics2D) g.create();
      Dimension s = c.getSize();
      JSeparator js = (JSeparator) c;
      if (js.getOrientation() == JSeparator.VERTICAL) {
        g2.setPaint(new GradientPaint(0, 0, ssc, 0, s.height, bgc, true));
        g2.fillRect(0, 0, 1, s.height);
        g2.setPaint(new GradientPaint(0, 0, shc, 0, s.height, bgc, true));
        g2.fillRect(1, 0, 1, s.height);
      } else {
        g2.setPaint(new GradientPaint(0, 0, ssc, s.width, 0, bgc, true));
        g2.fillRect(0, 0, s.width, 1);
        g2.setPaint(new GradientPaint(0, 0, shc, s.width, 0, bgc, true));
        g2.fillRect(0, 1, s.width, 1);
      }
      g2.dispose();
    }
  }
}
}}

JSeparatorにsetBorder()すると、セパレータが見えなくなる場合があるのですこし注意が必要です。
* 解説 [#explanation]
- 上、左:
-- デフォルトの`JSeparator`
- 下、右:
-- `BasicSeparatorUI`を継承する`GradientSeparatorUI`を`JSeparator`に設定して元色から親パネルの背景色へとグラデーションする`GradientPaint`で直線を描画
-- `JSeparator`に`Border`を設定すると`JSeparator`が表示されない

//**参考リンク
**コメント [#x0186bae]
* 参考リンク [#reference]
- [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/JSeparator.html JSeparator (Java Platform SE 8)]

* コメント [#comment]
#comment
- `JSeparator.VERTICAL`に対応。 -- &user(aterai); &new{2008-04-11 (金) 14:32:04};

#comment