TITLE:SynthでJInternalFrameを半透明にする

SynthでJInternalFrameを半透明にする

Posted by terai at 2008-12-01

概要

Synthを使ったLookAndFeelで、JInternalFrame を半透明にします。

  • &jnlp;
  • &jar;
  • &zip;

#screenshot

サンプルコード

try{
  UIManager.setLookAndFeel(
    "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
  SynthLookAndFeel.setStyleFactory(
    new MySynthStyleFactory(SynthLookAndFeel.getStyleFactory()));
}catch(Exception e) {
  e.printStackTrace();
}
class MySynthStyleFactory extends SynthStyleFactory {
  private SynthStyleFactory wrappedFactory;
  public MySynthStyleFactory(SynthStyleFactory factory) {
    this.wrappedFactory = factory;
  }
  public SynthStyle getStyle(JComponent c, Region id) {
    SynthStyle s = wrappedFactory.getStyle(c, id);
    //if(id==Region.INTERNAL_FRAME_TITLE_PANE||id==Region.INTERNAL_FRAME) {
    if(id==Region.INTERNAL_FRAME) {
      s = new TranslucentSynthSytle(s);
    }
    return s;
  }
}
class TranslucentSynthSytle extends SynthStyle {
  private final SynthStyle style;
  public TranslucentSynthSytle(SynthStyle s) {
    style = s;
  }
  public SynthPainter getPainter(final SynthContext context) {
    return new SynthPainter() {
      public void paintInternalFrameBackground(SynthContext context,
                           Graphics g, int x, int y, int w, int h) {
        g.setColor(new Color(100,200,100,100));
        g.fillRoundRect(x,y,w-1,h-1,15,15);
      }
    };
  }
  public boolean isOpaque(SynthContext context) {
    if(context.getRegion()==Region.INTERNAL_FRAME) {
      return false;
    }else{
      return style.isOpaque(context);
    }
  }
  public Color getColorForState(SynthContext context, ColorType type) {
    return null; //Color.RED;
  }
  public Font getFontForState(SynthContext context) {
    return null; //new Font("MONOSPACE", Font.ITALIC, 24);
  }
  //...

解説

上記のサンプルでは、SynthStyle#paintInternalFrameBackground、SynthStyle#isOpaqueなどのメソッドをオーバーライドしてJInternalFrame を半透明にします。

  • Synthを使ったLook&Feel
    • JDK 1.6.0_10 で追加された NimbusLookAndFeel や、GTKLookAndFeel など

      #screenshot(,screenshot1.png)

参考リンク

コメント