Java 如何向JLabel添加淡入/淡出效果
您好,我正在尝试创建一个Java游戏,需要在我的标签上添加一些效果。我有以下问题Java 如何向JLabel添加淡入/淡出效果,java,swing,jlabel,Java,Swing,Jlabel,您好,我正在尝试创建一个Java游戏,需要在我的标签上添加一些效果。我有以下问题 如何向我的标签添加淡入/淡出效果 我有一个JLabel,但我需要它的形状,可能是矩形或云形状。我怎样才能做到 您可以使用AlphaComposite更改组件的透明度级别 现在,巧妙地使用一个计时器,你可以使标签淡入淡出,或者简单地控制你喜欢的不透明度 public class TestFadeLabel { public static void main(String[] args) {
您可以使用
AlphaComposite
更改组件的透明度级别
现在,巧妙地使用一个计时器,你可以使标签淡入淡出,或者简单地控制你喜欢的不透明度
public class TestFadeLabel {
public static void main(String[] args) {
new TestFadeLabel();
}
public TestFadeLabel() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new MainPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class MainPane extends JPanel {
private float direction = -0.05f;
private FadeLabel label = new FadeLabel();
public MainPane() {
setLayout(new BorderLayout());
JLabel background = new JLabel();
background.setLayout(new GridBagLayout());
try {
background.setIcon(new ImageIcon(ImageIO.read(getClass().getResource("/Storm.jpg"))));
} catch (Exception e) {
e.printStackTrace();
}
add(background);
label = new FadeLabel();
background.add(label);
Timer timer = new Timer(100, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
float alpha = label.getAlpha();
alpha += direction;
if (alpha < 0) {
alpha = 0;
direction = 0.05f;
} else if (alpha > 1) {
alpha = 1;
direction = -0.05f;
}
label.setAlpha(alpha);
}
});
timer.setRepeats(true);
timer.setCoalesce(true);
timer.start();
}
}
public class FadeLabel extends JLabel {
private float alpha;
private BufferedImage background;
public FadeLabel() {
try {
background = ImageIO.read(getClass().getResource("/Cloud.png"));
} catch (Exception e) {
}
setText("Hide and go seek");
setHorizontalAlignment(CENTER);
setVerticalAlignment(CENTER);
setAlpha(1f);
}
public void setAlpha(float value) {
if (alpha != value) {
float old = alpha;
alpha = value;
firePropertyChange("alpha", old, alpha);
repaint();
}
}
public float getAlpha() {
return alpha;
}
@Override
public Dimension getPreferredSize() {
return background == null ? super.getPreferredSize() : new Dimension(background.getWidth(), background.getHeight());
}
@Override
public void paint(Graphics g) {
// This is one of the few times I would directly override paint
// This makes sure that the entire paint chain is now using
// the alpha composite, including borders and child components
Graphics2D g2d = (Graphics2D) g.create();
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getAlpha()));
super.paint(g2d);
g2d.dispose();
}
@Override
protected void paintComponent(Graphics g) {
// This is one of the few times that doing this before the super call
// will work...
if (background != null) {
int x = (getWidth() - background.getWidth()) / 2;
int y = (getHeight() - background.getHeight()) / 2;
g.drawImage(background, x, y, this);
}
super.paintComponent(g);
}
}
}
公共类TestFadeLabel{
公共静态void main(字符串[]args){
新的TestFadeLabel();
}
公共TestFadeLabel(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}捕获(ClassNotFoundException ex){
}catch(实例化异常){
}捕获(非法访问例外){
}捕获(无支持的LookandFeelexception ex){
}
JFrame=新JFrame(“测试”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(新的BorderLayout());
frame.add(新主窗格());
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
公共类主窗格扩展了JPanel{
专用浮动方向=-0.05f;
专用FadeLabel标签=新的FadeLabel();
公共主窗格(){
setLayout(新的BorderLayout());
JLabel background=新的JLabel();
setLayout(新的GridBagLayout());
试一试{
setIcon(新的ImageIcon(ImageIO.read(getClass().getResource(“/Storm.jpg”)));
}捕获(例外e){
e、 printStackTrace();
}
添加(背景);
标签=新的FadeLabel();
背景。添加(标签);
计时器计时器=新计时器(100,新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
float alpha=label.getAlpha();
α+=方向;
如果(α<0){
α=0;
方向=0.05f;
}否则,如果(alpha>1){
α=1;
方向=-0.05f;
}
标签:setAlpha(alpha);
}
});
timer.setRepeats(真);
timer.setCoalesce(真);
timer.start();
}
}
公共类FadeLabel扩展了JLabel{
私人浮动阿尔法;
私有缓冲图像背景;
公共FadeLabel(){
试一试{
background=ImageIO.read(getClass().getResource(“/Cloud.png”);
}捕获(例外e){
}
setText(“隐藏和搜索”);
设置水平对齐(中心);
设置垂直对齐(中心);
setAlpha(1f);
}
公共void setAlpha(浮动值){
如果(alpha!=值){
浮动旧=阿尔法;
α=值;
火属性更改(“alpha”,旧,alpha);
重新油漆();
}
}
公共浮点getAlpha(){
返回α;
}
@凌驾
公共维度getPreferredSize(){
返回background==null?super.getPreferredSize():新维度(background.getWidth(),background.getHeight());
}
@凌驾
公共空间涂料(图g){
//这是为数不多的几次我会直接覆盖油漆
//这样可以确保整个绘制链现在正在使用
//alpha组合,包括边框和子组件
Graphics2D g2d=(Graphics2D)g.create();
setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,getAlpha());
超级油漆(g2d);
g2d.dispose();
}
@凌驾
受保护组件(图形g){
//这是为数不多的几次在超级呼叫之前这样做
//将工作。。。
如果(背景!=null){
intx=(getWidth()-background.getWidth())/2;
int y=(getHeight()-background.getHeight())/2;
g、 drawImage(背景,x,y,此);
}
超级组件(g);
}
}
}
我看不到它的边框。也许背景颜色是一样的。我会检查一下,试试这个还是别的。