Swing/RippleBorder のバックアップの現在との差分(No.4)
TITLE:Borderのアニメーション
Borderのアニメーション
編集者:Terai Atsuhiro~
作成日:2006-05-29
更新日:2021-06-25 (金) 22:29:08
概要
Timer
を使って、波紋風のアニメーションを描画するBorder
を作成します。
Screenshot
Advertisement
概要
Borderの描画をアニメーションさせます。サンプルコード
#spanend
#spanadd
class RippleBorder extends EmptyBorder {
#spanend
private final Timer animator;
private final JComponent comp;
private float count = 1f;
#spandel
#screenshot
#spanend
public RippleBorder(JComponent c, int width) {
super(width, width, width, width);
this.comp = c;
animator = new Timer(80, new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
comp.repaint();
count += .9f;
}
});
comp.addMouseListener(new MouseAdapter() {
@Override public void mouseEntered(MouseEvent e) {
comp.setForeground(Color.RED);
animator.start();
}
#spandel
**サンプルコード [#c89c1907]
#spanend
class RippleBorder extends EmptyBorder {
private final javax.swing.Timer animator;
private final JComponent comp;
public RippleBorder(JComponent c, int width) {
super(width, width, width, width);
this.comp = c;
animator = new javax.swing.Timer(80, new ActionListener() {
public void actionPerformed(ActionEvent e) {
comp.repaint();
count+=0.9f;
}
});
comp.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
comp.setForeground(Color.red);
startAnimation();
}
public void mouseExited(MouseEvent e) {
comp.setForeground(Color.black);
stopAnimation();
}
});
}
boolean startflag = false;
boolean stopflag = false;
public void startAnimation() {
startflag = true;
stopflag = false;
animator.start();
}
public void stopAnimation() {
stopflag = true;
}
private float count = 1.0f;
public void paintBorder(Component c, Graphics g,
int x, int y, int w, int h) {
if(!startflag) return;
Graphics2D g2 = (Graphics2D)g;
g2.setColor(Color.white);
float a = 1.0f/count;
if(0.12f-a>1.0e-2) a = 0.0f;
g2.setComposite(
AlphaComposite.getInstance(AlphaComposite.SRC_OVER,a));
Insets i = getBorderInsets();
int xx = i.left-(int)count;
int yy = i.top-(int)count;
int ww = i.left+i.right-(int)(count*2.0f);
int hh = i.top+i.bottom-(int)(count*2.0f);
g2.setStroke(new BasicStroke(count*1.2f));
g2.drawRoundRect(xx, yy, w-ww, h-hh, 10, 10);
if(xx<0) {
count = 1.0f;
if(stopflag) {
animator.stop();
startflag = false;
stopflag = false;
return;
}
}
}
}
@Override public void mouseExited(MouseEvent e) {
comp.setForeground(Color.BLACK);
}
});
}
-&jnlp;
-&jar;
-&zip;
@Override public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
if (!animator.isRunning()) {
super.paintBorder(c, g, x, y, w, h);
return;
}
Graphics2D g2 = (Graphics2D) g;
g2.setPaint(Color.WHITE);
float a = 1f / count;
if (.12f - a > 1.0e-2) {
a = 0f;
}
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, a));
Insets i = getBorderInsets();
int xx = i.left - (int) count;
int yy = i.top - (int) count;
int ww = i.left + i.right - (int) (count * 2f);
int hh = i.top + i.bottom - (int) (count * 2f);
g2.setStroke(new BasicStroke(count * 1.2f));
g2.drawRoundRect(xx, yy, w - ww, h - hh, 10, 10);
if (xx < 0 && animator.isRunning()) {
count = 1f;
animator.stop();
}
}
#spanadd
}
#spanend
#spanadd
View in GitHub: Java, Kotlin解説
コンポーネント上にカーソルがきた場合、Borderをアニメーションさせることで、波紋状の効果を描画しています。解説
対象コンポーネント上にマウスカーソルが乗ったことをマウスリスナーで検知し、Border
をアニメーションさせることで波紋状の効果を描画しています。