Summary

JWindowの透明度をTimerを使用して変更し、フェードアウトで非表示化するよう設定します。

Source Code Examples

JWindow window = new JWindow();
GraphicsConfiguration gc = window.getGraphicsConfiguration();
if (gc != null && gc.isTranslucencyCapable()) {
  window.setBackground(new Color(0x0, true));
}
AtomicInteger alpha = new AtomicInteger(100);
Timer animator = new Timer(50, null);
animator.addActionListener(e -> {
  int a = alpha.addAndGet(-10);
  if (a < 0) {
    window.dispose();
    animator.stop();
    log.append("JWindow.dispose()\n");
  } else {
    float opacity = a / 100f;
    window.setOpacity(opacity);
    log.append(String.format("JWindow.setOpacity(%f)%n", opacity));
  }
});
Shape shape = new RoundRectangle2D.Float(0f, 0f, 240f, 64f, 32f, 32f);
window.getContentPane().add(makePanel(shape, animator));
window.pack();
window.setLocationRelativeTo(c.getRootPane());
window.setVisible(true);
View in GitHub: Java, Kotlin

Explanation

  • JWindowの背景色にWindow#setBackground(...)new Color(0x0, true)を設定して透明化
    • JWindow#getContentPane()で取得したコンテンツペインに半透明の図形を描画するJPanelを追加
    • JPanelには背景のみ透明なアイコンと親JWindowを閉じるJButtonを追加
  • JButtonがクリックされたらTimerを起動してJWindow#setOpacity(float)でウィンドウの不透明性を設定

Reference

Comment