gpt4 book ai didi

java - 在 jframe 中的模糊背景上创建一个透明矩形

转载 作者:行者123 更新时间:2023-12-02 05:54:37 24 4
gpt4 key购买 nike

我在模糊背景上创建透明矩形时遇到问题。我正在尝试在glasspane上执行此任务。这是我的代码片段。

void createBlur() {
alpha = 1.0f;
JRootPane root = SwingUtilities.getRootPane(jf);
blurBuffer = GraphicsUtilities.createCompatibleImage(jf.getWidth(), jf.getHeight());
Graphics2D g2d = blurBuffer.createGraphics();
root.paint(g2d);
g2d.dispose();

backBuffer = blurBuffer;
blurBuffer = GraphicsUtilities.createThumbnailFast(blurBuffer, jf.getWidth() / 2);
blurBuffer = new GaussianBlurFilter(5).filter(blurBuffer, null);
}

其中,backBuffer和blurBuffer是BufferedImage的对象&jf = JFrame,alpha用于不透明度。上述方法很好地创建了模糊效果。

这是在面板上创建透明矩形的代码

protected void paintComponent(Graphics g) {
int x = 34;
int y = 34;
int w = getWidth() - 68;
int h = getHeight() - 68;
int arc = 30;

//Graphics2D g2 = currentGraphics.createGraphics();
//g2.drawImage(currentGraphics, 0, 0, null);
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

g2.setColor(new Color(0, 0, 0, 220));
g2.fillRoundRect(x, y, w, h, arc, arc);

g2.setStroke(new BasicStroke(1f));
g2.setColor(Color.WHITE);
g2.drawRoundRect(x, y, w, h, arc, arc);

g2.dispose();
}

现在我遇到的问题是如何同时绘制模糊效果和透明矩形。我没有在这里发布完整的代码,如果有人想在这里看到代码link .

这是所需的示例输出图像。提前致谢。

enter image description here

最佳答案

我正在尝试弄清楚您的代码...

您无法调用 super.paintComponent...如果您不小心,这可能会导致您遇到同样严重的问题。一般经验法则,只需调用它即可;)

修改Graphics上下文的状态时要小心,例如......

g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

将影响在此之后绘制的所有组件,并可能导致一些您没有预料到的有趣的图形故障...

jf.getGlassPane().setVisible(false);
glassPanel = new GlassPanel();
jf.getGlassPane().setVisible(true);

似乎毫无意义,因为使用 jf.setGlassPane(glassPanel); 设置的组件仍然是调用 jf.getGlassPane().setVisible(true) 时可见的组件;。这也意味着 GlassPane 组件从未被使用过...

paintComponent 中检查 isVisible 是没有意义的,因为 Swing 足够聪明,知道不要绘制不可见的组件...

现在,说了这么多......

如果您想在 BlurPanel 上绘制,您可以...在绘制 blurBuffer 后绘制内容,因此您可以在其上绘制或添加BlurPanel Pane 上的另一个组件,其中包含您要应用的绘图逻辑...

这是该概念的一个基本示例。这会在玻璃板上添加另一个面板,根据我的需要绘制面板的自定义框架。

此示例使用个人库代码,仅作为概念示例,而不是完全可运行的示例。

Before After

import core.ui.GlowEffectFactory;
import core.ui.GraphicsUtilities;
import core.util.ByteFormatter;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

public class TransparentTest {

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

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

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

private BufferedImage background;
private BlurredGlassPane blurredGlassPane;

private InfoPane infoPane;

public TestPane() {
try {
background = ImageIO.read(new File("get your own image"));
} catch (IOException ex) {
ex.printStackTrace();
}

blurredGlassPane = new BlurredGlassPane();
blurredGlassPane.setLayout(new GridBagLayout());
infoPane = new InfoPane();
infoPane.setFile(new File("get your own image"));
blurredGlassPane.add(infoPane);

JButton click = new JButton("Click");
click.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Window win = SwingUtilities.getWindowAncestor(TestPane.this);
if (win instanceof JFrame) {
JFrame frame = (JFrame) win;
frame.setGlassPane(blurredGlassPane);
blurredGlassPane.setVisible(true);
}
}
});

setLayout(new GridBagLayout());
add(click);
}

@Override
public Dimension getPreferredSize() {
return background == null ? new Dimension(200, 200) : new Dimension(background.getWidth(), background.getHeight());
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (background != null) {
Graphics2D g2d = (Graphics2D) g.create();
int x = (getWidth() - background.getWidth()) / 2;
int y = (getHeight() - background.getHeight()) / 2;
g2d.drawImage(background, x, y, this);
g2d.dispose();
}
}
}

public static class InfoPane extends JPanel {

protected static final int RADIUS = 20;
protected static final int FRAME = 4;
protected static final int INSET = RADIUS + FRAME;
protected static final DateFormat DATE_FORMAT = DateFormat.getDateTimeInstance();

private JLabel name;
private JLabel path;
private JLabel length;
private JLabel lastModified;
private JLabel canExecute;
private JLabel canRead;
private JLabel canWrite;
private JLabel isDirectory;
private JLabel isHidden;

public InfoPane() {
setBorder(new EmptyBorder(INSET, INSET, INSET, INSET));
setOpaque(false);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.anchor = GridBagConstraints.WEST;
gbc.gridwidth = GridBagConstraints.REMAINDER;

name = createLabel(Font.BOLD, 48);
add(name, gbc);

gbc.gridy++;
path = createLabel();
add(path, gbc);

gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 2;
gbc.anchor = GridBagConstraints.WEST;

length = createLabel();
lastModified = createLabel();
add(createLabel("Size: "), gbc);

gbc.gridx++;
gbc.insets = new Insets(0, 0, 0, 10);
add(length, gbc);

gbc.insets = new Insets(0, 0, 0, 0);
gbc.gridx++;
add(createLabel("Last Modified: "), gbc);

gbc.gridx++;
add(lastModified, gbc);
}

public JLabel createLabel(String text) {

JLabel label = new JLabel(text);
label.setForeground(Color.WHITE);
return label;

}

public JLabel createLabel() {

return createLabel("");

}

public JLabel createLabel(int style, float size) {

JLabel label = createLabel();
label.setFont(label.getFont().deriveFont(style, size));
return label;
}

public void setFile(File file) {

name.setText(file.getName());
try {
path.setText(file.getParentFile().getCanonicalPath());
} catch (IOException ex) {
ex.printStackTrace();
}
length.setText(ByteFormatter.format(file.length()));
lastModified.setText(DATE_FORMAT.format(new Date(file.lastModified())));
file.canExecute();
file.canRead();
file.canWrite();
file.isDirectory();
file.isHidden();

}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
GraphicsUtilities.applyQualityRenderingHints(g2d);
int width = getWidth() - 1;
int height = getHeight() - 1;
int buffer = FRAME / 2;
RoundRectangle2D base = new RoundRectangle2D.Double(buffer, buffer, width - FRAME, height - FRAME, RADIUS, RADIUS);
g2d.setColor(new Color(0, 0, 0, 128));
g2d.fill(base);
g2d.setColor(Color.WHITE);
g2d.setStroke(new BasicStroke(FRAME, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g2d.draw(base);
g2d.dispose();
}

}

public class BlurredGlassPane extends JPanel {

private BufferedImage background;

@Override
public void setVisible(boolean visible) {
if (visible) {
Container parent = SwingUtilities.getAncestorOfClass(JRootPane.class, this);
if (parent != null) {
JRootPane rootPane = (JRootPane) parent;

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

background = GlowEffectFactory.generateBlur(img, 40);
}
}
super.setVisible(visible);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(background, 0, 0, this);
}

}

}

关于java - 在 jframe 中的模糊背景上创建一个透明矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23215415/

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