gpt4 book ai didi

处理透明度和图像的 Java Swing 图形故障

转载 作者:行者123 更新时间:2023-11-30 09:19:12 25 4
gpt4 key购买 nike

所以我有这个登录表单和一张“用户照片”。我试图做到这一点,当您将鼠标悬停在照片区域上时,会出现一个带有彩色背景的透明标签(以产生“选择照片”的效果)。它看起来像这样:

image1

一旦您将鼠标移开它,它就会恢复“取消选择”状态。

现在我的问题是,如果您先将鼠标悬停在登录按钮上,然后将鼠标移到照片上,则会出现“幽灵登录按钮”。它看起来像这样:

image2

我不知道为什么会这样。有人可以帮忙吗?相关代码如下:

package com.stats;

public class Stats extends JFrame implements Serializable {

private JLabel fader;

public Stats() {

try {
Image image = ImageIO.read(new File(System.getenv("APPDATA")
+ "\\Stats\\Renekton_Cleave.png"));
JLabel labelUserPhoto = new JLabel(new ImageIcon(image));
fader = new JLabel();
fader.setBounds(97, 44, 100, 100);
fader.setOpaque(true);
fader.setBackground(new Color(0, 0, 0, 0));
labelUserPhoto.setBounds(97, 44, 100, 100);
PicHandler ph = new PicHandler();
contentPane.add(fader);
contentPane.add(labelUserPhoto);
fader.addMouseMotionListener(ph);
} catch(Exception e) {
e.printStackTrace();
}
}

private class PicHandler implements MouseMotionListener {
public void mouseDragged(MouseEvent e) { }
public void mouseMoved(MouseEvent e) {
int x = e.getX();
int y = e.getY();

System.out.println("x: " + x + ", y: " + y);

if ((x > 16 && x < 80) && (y > 16 && y < 80)) {
if (!fader.isOpaque()) {
fader.setOpaque(true);
fader.setBackground(new Color(0, 0, 0, 40));
fader.repaint();
}
} else {
if (fader.isOpaque()) {
fader.setOpaque(false);
fader.repaint();
}
}
}
}

最佳答案

我可以看到您的示例存在许多问题,但最重要的是使用具有 alpha 值的颜色。

fader.setBackground(new Color(0, 0, 0, 40));

Swing 不能很好地渲染具有基于 alpha 的颜色的组件(在此上下文中)。通过使组件不透明然后将背景颜色设置为使用 alpha 值,您告诉 Swing 它不需要担心绘制组件下方的内容,这是不正确的...

Graphics 上下文也是一个共享资源,这意味着在您的组件之前绘制的任何内容仍然是“绘制”的,您需要在绘制之前清除 Graphics 上下文.

enter image description here enter image description here

这个例子使用了一个相当讨厌的技巧来完成它的工作。因为所有绘制都发生在 UI 委托(delegate)中,所以如果我们只是允许默认绘制链继续,我们将无法在图标下方进行渲染。相反,我们接管了“脏”细节的控制权并代表 parent 绘制背景。

如果我们简单地从 JPanel 扩展并自己绘制图像,这将更容易实现

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class FadingIcon {

public static void main(String[] args) {
new FadingIcon();
}

public FadingIcon() {
startUI();
}

public void startUI() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

BufferedImage img = null;
try {
img = ImageIO.read(new File("C:\\Users\\swhitehead\\Documents\\My Dropbox\\Ponies\\SmallPony.png"));
} catch (IOException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Testing");
frame.setLayout(new GridBagLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new FadingLabel(new ImageIcon(img)));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class FadingLabel extends JLabel {

private boolean mouseIn = false;
private MouseHandler mouseHandler;

public FadingLabel(Icon icon) {
super(icon);
setBackground(Color.RED);
super.setOpaque(false)(
}

@Override
public void setOpaque(boolean opaque) {
}

@Override
public final boolean isOpaque() {
return false;
}

protected MouseHandler getMouseHandler() {
if (mouseHandler == null) {
mouseHandler = new MouseHandler();
}
return mouseHandler;
}

@Override
public void addNotify() {
super.addNotify();
addMouseListener(getMouseHandler());
}

@Override
public void removeNotify() {
removeMouseListener(getMouseHandler());
super.removeNotify();
}

@Override
protected void paintComponent(Graphics g) {
if (mouseIn) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
g2d.setColor(getBackground());
g2d.fillRect(0, 0, getWidth(), getHeight());
g2d.dispose();
}
getUI().paint(g, this);
}

public class MouseHandler extends MouseAdapter {

@Override
public void mouseEntered(MouseEvent e) {
mouseIn = true;
repaint();
}

@Override
public void mouseExited(MouseEvent e) {
mouseIn = false;
repaint();
}

}

}

}

我还建议您花时间学习如何使用适当的布局管理器,它们会在以后为您省去很多麻烦

查看 A Visual Guide to Layout ManagersLaying Out Components Within a Container

关于处理透明度和图像的 Java Swing 图形故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18070120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com