Swing/EaseInOut のバックアップの現在との差分(No.15)
- category: swing
folder: EaseInOut
title: JTextAreaをキャプションとして画像上にスライドイン
tags: [JTextArea, OverlayLayout, JLabel, MouseListener, Animation]
author: aterai
pubdate: 2011-05-09T15:30:53+09:00
description: 画像の上にJTextAreaをスライドインアニメーションで表示します。
image:
hreflang:
href: http://java-swing-tips.blogspot.com/2011/05/translucent-image-caption-using.html href: https://java-swing-tips.blogspot.com/2011/05/translucent-image-caption-using.html lang: en
概要
画像の上にJTextArea
をスライドインアニメーションで表示します。
Screenshot
Advertisement
サンプルコード
private int delay = 4;
private int count = 0;
#spanadd
#spanend
@Override public void mouseEntered(MouseEvent e) {
if (animator != null && animator.isRunning() ||
yy == textArea.getPreferredSize().height) return;
final double h = (double) textArea.getPreferredSize().height;
if (animator != null && animator.isRunning() || yy == textArea.getPreferredSize().height) {
return;
}
double h = (double) textArea.getPreferredSize().height;
animator = new Timer(delay, new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
double a = easeInOut(++count / h);
yy = (int) (.5 + a * h);
textArea.setBackground(new Color(0f, 0f, 0f, (float) (.6 * a)));
if (yy >= textArea.getPreferredSize().height) {
yy = textArea.getPreferredSize().height;
animator.stop();
}
revalidate();
repaint();
}
});
animator.start();
}
#spanadd
#spanend
@Override public void mouseExited(MouseEvent e) {
if (animator != null && animator.isRunning() ||
contains(e.getPoint()) && yy == textArea.getPreferredSize().height) return;
final double h = (double) textArea.getPreferredSize().height;
double h = (double) textArea.getPreferredSize().height;
animator = new Timer(delay, new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
double a = easeInOut(--count / h);
yy = (int) (.5 + a * h);
textArea.setBackground(new Color(0f, 0f, 0f, (float) (.6 * a)));
if (yy <= 0) {
yy = 0;
animator.stop();
}
revalidate();
repaint();
}
});
animator.start();
}
#spandel
//@see Math: EaseIn EaseOut, EaseInOut and Beziér Curves | Anima Entertainment GmbH
#spanend
#spandel
//http://www.anima-entertainment.de/math-easein-easeout-easeinout-and-bezier-curves
#spanend
#spanadd
#spanend
#spanadd
// @see Math: EaseIn EaseOut, EaseInOut and Beziér Curves | Anima Entertainment GmbH
#spanend
#spanadd
// http://www.anima-entertainment.de/math-easein-easeout-easeinout-and-bezier-curves
#spanend
public double easeInOut(double t) {
//range: 0.0 <= t <= 1.0
// range: 0.0 <= t <= 1.0
if (t < .5) {
return .5 * Math.pow(t * 2d, 3d);
} else {
return .5 * (Math.pow(t * 2d - 2d, 3d) + 2d);
}
}
View in GitHub: Java, Kotlin解説
上記のサンプルでは、JLabel
に画像を表示し、これにマウスカーソルが入った場合、JTextArea
がキャプションとしてease-in
, ease-out
でスライドインするようになっています。
上記のサンプルでは、JLabel
に画像を表示してその内部にマウスカーソルが入った場合JTextArea
がキャプションとしてease-in
, ease-out
でスライドインするよう設定しています。
-
OverlayLayout#layoutContainer
内で、JTextArea
のy
座標を変更してアニメーション -
OverlayLayout#layoutContainer
内でJTextArea
のy
座標を変更してアニメーション EaseInOut
の計算は、Math: EaseIn EaseOut, EaseInOut and Beziér Curves | Anima Entertainment GmbHを参考- マウスカーソルが
JLabel
(画像)内にあっても、そこにJTextArea
がスライドインした場合、mouseExited
が発生するので、注意-
JTextArea#contains(int x, int y)
が常にfalse
を返すようにすれば、上記の場合でもmouseExited
などは発生しないが、JTextArea
内の文字列が選択できなくなるので、このサンプルでは、JTextArea
にマウスイベントを親へ素通しするリスナーを追加している
-
- マウスカーソルが
JLabel
(画像)内にあってもそこにJTextArea
がスライドインした場合、新たにmouseExited
イベントが発生するので注意が必要-
JTextArea#contains(int x, int y)
が常にfalse
を返すようにすれば上記の場合でもmouseExited
イベントなどは発生しないがJTextArea
内の文字列が選択できなくなるので、このサンプルではJTextArea
にマウスイベントを親へ素通しするリスナーを追加している
-
JTextArea
の背景色はsetOpaque(false)
にして描画せず、別途JTextArea#paintComponent(...)
をオーバーライドしてアルファ成分をEaseInOut
した色で全体を塗りつぶしている
メモ: 累乗を
Math.pow(...)
の代わりにバイナリ法で
- 累乗を
Math.pow(...)
の代わりにバイナリ法で実行する場合のメモ
- 指数が整数ならMath.powは使うな - DoMshi
- 整数のべき乗 - rexpit's programming memo
- Integer Power Algorithm
- The Power Algorithm - How a programmer can save time rising a real number to the power of a positive integer number. The code is dot.net but it's easily adaptable to any other language.
public static double intpow(double x, int n) {
double aux = 1d;
if (n < 0) {
throw new IllegalArgumentException("n must be a positive integer");
}
for (; n > 0; x *= x, n >>>= 1) {
if ((n & 1) != 0) {
aux *= x;
}
}
return aux;
}
参考リンク
Math: EaseIn EaseOut, EaseInOut and Beziér Curves | Anima Entertainment GmbH- Slide In Image Captions | CSS-Tricks
- 指数関数を使ったお手軽イーズ・アウト - Radium Software
- Soft maximum 関数 - Radium Software
- イーズイン/アウトいろいろ - wonderfl build flash online
- 本の虫: なんでGCCはa*a*a*a*a*a を (a*a*a)*(a*a*a) に最適化できないの?っと