gpt4 book ai didi

java - 悬停时 JFrame 上的图像(JLabel)变亮

转载 作者:行者123 更新时间:2023-12-02 09:05:42 27 4
gpt4 key购买 nike

我在 JFrame 上有一个 JLabel 图像,我想在悬停时使其太亮,已经尝试了一些方法,但似乎没有任何效果,这是我当前的代码:

public class DivinationLogo extends JLabel {
private BufferedImage logo;
public DivinationLogo() {

super();
try {
logo = ImageIO.read(getClass().getResourceAsStream("/assets/images/logo.png"));
} catch (IOException e) {
e.printStackTrace();
}
// setIcon(new ImageIcon(logo));
setIconTextGap(0);
setBorder(null);
setText(null);

addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
Functions.openWebPage(Config.website);
}

@Override
public void mouseEntered(MouseEvent e) {
Functions.brightnessControl(logo, .3F);
}

@Override
public void mouseExited(MouseEvent e) {
Functions.brightnessControl(logo, 1.0F);
}
});
setBounds(-5, 1, 247, 106);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(logo, -5, 1, null);
}
}

函数.brightnessControl:

public static Image brightnessControl(Image image, float brightness) {
BufferedImage bi = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics bg = bi.getGraphics();

if (bi.getColorModel().hasAlpha()) {
System.out.println("Image has got an alpha channel");
}

bg.drawImage(image, 0, 0, null);
bg.dispose();
RescaleOp rescaleOp = new RescaleOp(brightness, 0, null);
rescaleOp.filter(bi, bi);
image = bi;
return bi;
}

目前,它没有做任何让我困惑的事情。如果有人知道这是如何实现的,请随时告诉我:)

最佳答案

好的,所以基本的想法是,当鼠标进入组件时,你想要生成一个“明亮”版本的组件(我知道,很明显,但请耐心等待)

问题是,您有一个 Graphics 上下文,并且需要一个图像:/

以下内容并没有过度优化,但无需花费大量时间添加 PropertyChange 支持,这将为您提供基本概念。

import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

class Main {

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

public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
JFrame frame = new JFrame();
frame.setLayout(new GridBagLayout());
JLabel label = new BrightLabel();
label.setIcon((new ImageIcon(ImageIO.read(Main.class.getResource("background.png")))));
frame.add(label);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
}

public class BrightLabel extends JLabel {

private MouseAdapter mouseHandler;
private boolean isHover = false;

@Override
public void addNotify() {
super.addNotify();
mouseHandler = new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
isHover = true;
repaint();
}

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

System.out.println("...");
addMouseListener(mouseHandler);
}

@Override
public void removeNotify() {
super.removeNotify();
if (mouseHandler != null) {
removeMouseListener(mouseHandler);
}
mouseHandler = null;
}

@Override
protected void paintComponent(Graphics g) {
if (!isHover) {
super.paintComponent(g);
return;
}

BufferedImage img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
super.paintComponent(g2d);
g2d.dispose();

Image bright = brightnessControl(img, 1.3f);
g.drawImage(bright, 0, 0, this);
}

public Image brightnessControl(Image image, float brightness) {
BufferedImage bi = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics bg = bi.getGraphics();

if (bi.getColorModel().hasAlpha()) {
System.out.println("Image has got an alpha channel");
}

bg.drawImage(image, 0, 0, null);
bg.dispose();
RescaleOp rescaleOp = new RescaleOp(brightness, 0, null);
rescaleOp.filter(bi, bi);
image = bi;
return bi;
}

}

}

本质上,当鼠标进入或存在组件时,我们设置 isHover 属性。然后我们调用 repaint 来触发新的绘制 channel 。

当组件被绘制时,如果isHovertrue,我们会稍微接管绘制过程,生成我们自己的缓冲区并将组件绘制到其中。这给了我们我们的基础图像。从那里我们使图像变亮并将其绘制到Graphics上下文

关于java - 悬停时 JFrame 上的图像(JLabel)变亮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59833361/

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