Java 具有图像淡出奇怪效果的Jlabel
希望这个问题能够更多地强调Jlabel(swing)的淡出效应 当然,是的。。。我已经遵循了一些指南和来自的一些答案,但我的答案有点不同。这不仅仅是JLabel中的一个文本,还有我放在上面的一个图像 我继续往下看。然而,它给了我一种淡出的效果。但是发生了可怕的事情白色背景。 如何解决这个问题? 我在这里共享界面。。。 这是尚未发生淡出的早期阶段。虽然 这是我提到的不想要的结果 老实说,我用三叉戟库制作动画; 因此,每当用户单击图像时,它都会执行此代码Java 具有图像淡出奇怪效果的Jlabel,java,image,animation,jlabel,fadeout,Java,Image,Animation,Jlabel,Fadeout,希望这个问题能够更多地强调Jlabel(swing)的淡出效应 当然,是的。。。我已经遵循了一些指南和来自的一些答案,但我的答案有点不同。这不仅仅是JLabel中的一个文本,还有我放在上面的一个图像 我继续往下看。然而,它给了我一种淡出的效果。但是发生了可怕的事情白色背景。 如何解决这个问题? 我在这里共享界面。。。 这是尚未发生淡出的早期阶段。虽然 这是我提到的不想要的结果 老实说,我用三叉戟库制作动画; 因此,每当用户单击图像时,它都会执行此代码 Timeline tm = new Time
Timeline tm = new Timeline(jll_btnProceed);
tm.addPropertyToInterpolate("intensity", 1.0f, 0.0f);
tm.setDuration(1000);
tm.play();
而且。。。JLabel本身,我用这个源代码覆盖它
/**
*
* @author Gumuruh
*/
public class JLabelFader extends JLabel {
private float intensity = 1.0f;
public void setIntensity(float intensity) {
this.intensity = intensity;
this.setOpaque(false);
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
final Composite oldComposite = g2.getComposite();
g2.setComposite(AlphaComposite.SrcOver);
final Color c = getBackground();
final Color color = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (255 * (1.0f - intensity)));
g2.setColor(color);
g2.fillRect(0, 0, getWidth(), getHeight());
g2.setComposite(oldComposite);
}
}
private BufferedImage buttonImage = null;
public void paint(Graphics g) {
// Create an image for the button graphics if necessary
if (buttonImage == null || buttonImage.getWidth() != getWidth()
|| buttonImage.getHeight() != getHeight()) {
buttonImage = getGraphicsConfiguration().
createCompatibleImage(getWidth(), getHeight());
}
Graphics gButton = buttonImage.getGraphics();
gButton.setClip(g.getClip());
// Have the superclass render the button for us
super.paint(gButton);
// Make the graphics object sent to this paint() method translucent
Graphics2D g2d = (Graphics2D) g;
AlphaComposite newComposite =
AlphaComposite.getInstance(AlphaComposite.SRC_OVER, intensity);
g2d.setComposite(newComposite);
// Copy the button's image to the destination graphics, translucently
g2d.drawImage(buttonImage, 0, 0, null);
}
我的手和我的头都停不下来,让这个问题得到真正的解决。因此,我试图跟进Java肮脏富客户端电子书中的一些示例,然后应用下面给出的源代码,但首先我需要对上面编写的受保护的void paint(图g)方法进行注释,并简单地添加此源代码
/**
*
* @author Gumuruh
*/
public class JLabelFader extends JLabel {
private float intensity = 1.0f;
public void setIntensity(float intensity) {
this.intensity = intensity;
this.setOpaque(false);
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
final Composite oldComposite = g2.getComposite();
g2.setComposite(AlphaComposite.SrcOver);
final Color c = getBackground();
final Color color = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (255 * (1.0f - intensity)));
g2.setColor(color);
g2.fillRect(0, 0, getWidth(), getHeight());
g2.setComposite(oldComposite);
}
}
private BufferedImage buttonImage = null;
public void paint(Graphics g) {
// Create an image for the button graphics if necessary
if (buttonImage == null || buttonImage.getWidth() != getWidth()
|| buttonImage.getHeight() != getHeight()) {
buttonImage = getGraphicsConfiguration().
createCompatibleImage(getWidth(), getHeight());
}
Graphics gButton = buttonImage.getGraphics();
gButton.setClip(g.getClip());
// Have the superclass render the button for us
super.paint(gButton);
// Make the graphics object sent to this paint() method translucent
Graphics2D g2d = (Graphics2D) g;
AlphaComposite newComposite =
AlphaComposite.getInstance(AlphaComposite.SRC_OVER, intensity);
g2d.setComposite(newComposite);
// Copy the button's image to the destination graphics, translucently
g2d.drawImage(buttonImage, 0, 0, null);
}
在最后。。。给我很好的淡出效果。但一开始,它给了我第二个可怕的效果,那就是第一个渲染的黑色背景。真不敢相信我??好的,给你。这是
如果有人告诉我;
“您的PNG不透明”!”。
请不要那样说。因为,我是跟着这个,
现在,我头晕目眩,但我的心笑了,无法控制。天啊。哎呀
新的故事开始了。。。
*从此处和下方更新*
嗯,非常感谢我们的朋友打电话来MKorbel,
从…起提供了一个关于Swing JButton淡出效果的清晰示例,我尝试在我的JLabel中实现它,violaaa。。。!!
它起作用了。
让我们为MKorbel热烈鼓掌:D
那么不管怎样,我怎样才能修复早期的代码呢?非常简单,只需注释Paint()方法,然后再次使用paintComponent()方法,它应该被下面的新源代码覆盖
@Override
public void paintComponent(java.awt.Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, intensity));
if (rectangularLAF && isBackgroundSet()) {
Color c = getBackground();
g2.setColor(c);
g.fillRect(0, 0, getWidth(), getHeight());
}
super.paintComponent(g2);
g2.dispose();
}
现在,JLabel很容易使用其强度变量进行更改。
淡出淡入完成
好的现在一切看起来都是“好的”。但是等一下,这里又发生了奇怪的事情。你注意到了吗?六羟甲基三聚氰胺六甲醚。。。。
我试图在JLabel中加入鼠标事件(悬停),我们覆盖了前面讨论的paintComponent()方法
myJLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
从逻辑上讲,它应该在鼠标悬停时更改光标。但是,另一种奇怪的效果出现了。(非常抱歉,但这仍然是主要案件的继续)。现在奇怪的效果是,当我们将鼠标悬停在Jlabel上时,鼠标光标无法更改。它仍然无法更改光标似乎是影响光标反应方式的paintComponent()方法。这是真的吗?queston->如果您试图设置/更改Alpha半透明或无实体透明/三叉戟库更改Alpha半透明,mKorbel,会发生什么?哪一个?实际上,我正在通过“强度”变量更改颜色--->旨在提供alpha颜色。请检查我的线程,只有当这将如您预期的那样工作时,您才能应用Trident libs。嘿,mKorbel。。。!我今天才意识到,没关系。你保存了我上面的主题。还有一些不必要的影响。如果我尝试实现您的代码(paintComponents)算法,光标似乎无法再应用了…:(这是非常学术化的一步,现在你有两个选择,编辑这个线程或开始一个新的,但运行的代码显示你尝试了什么…)。。。。