Java swing实现一闪一闪的效果,其实思路只是建立一个线程,在线程中不断地切换你想要显示的图片以此达到闪烁的效果。以下附上主要的代码。
class TwinkledThread extends Thread{
public boolean twinkledFlag=false;
private long currentTimeMillis;
public TwinkledThread(long currentTimeMillis) {
this.currentTimeMillis=currentTimeMillis;
}
@Override
public void run() {
while(!twinkledFlag){
try {
markstationImg= markstation;
Thread.sleep(100);
// 闪动消息的提示图片
markstationImg=null;
Thread.sleep(100);
//此处控制闪烁的时长
if(System.currentTimeMillis()-currentTimeMillis>2000){
twinkledFlag=true;
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class MyLabel extends JLabel{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2=(Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
if(twinkledBs!=null){
if(markstationImg!=null){
g2.drawImage(markstationImg.getImage(),
twinkledBs.getX(),twinkledBs.getY(), this
);
}
}
}
}