gpt4 book ai didi

Java - 在 paintComponent 中合成的圆角面板

转载 作者:搜寻专家 更新时间:2023-10-30 19:44:51 24 4
gpt4 key购买 nike

根据最初的问题(下方),我现在悬赏以下内容:

基于 AlphaComposite 的圆角解决方案。

  • 请使用JPanel 进行演示。
  • 角落必须完全透明。
  • 必须能支持JPG画图,但还是有圆角
  • 不得使用 setClip(或任何剪辑)
  • 必须有不错的表现

希望有人能快速掌握它,这看起来很容易。

如果有一个很好解释的原因说明为什么永远无法做到这一点,并且其他人同意,我也会奖励赏金。

这是我所想的示例图像(但使用AlphaComposite)enter image description here


原始问题

我一直在尝试找出一种使用合成来制作圆角的方法,非常类似于How to make a rounded corner image in Java。或 http://weblogs.java.net/blog/campbell/archive/2006/07/java_2d_tricker.html .

但是,我在没有中间 BufferedImage 的情况下进行的尝试不起作用 - 圆形目标合成显然不会影响源。我尝试过不同的东西,但没有任何效果。应该得到一个圆形的红色矩形,而不是我得到一个方形的。

所以,我有两个问题,真的:

1) 有没有办法让它工作?

2) 中间图像实际上会产生更好的性能吗?

中南合作:

测试面板TPanel

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JLabel;

public class TPanel extends JLabel {
int w = 300;
int h = 200;

public TPanel() {
setOpaque(false);
setPreferredSize(new Dimension(w, h));
setMaximumSize(new Dimension(w, h));
setMinimumSize(new Dimension(w, h));
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();

// Yellow is the clipped area.
g2d.setColor(Color.yellow);
g2d.fillRoundRect(0, 0, w, h, 20, 20);
g2d.setComposite(AlphaComposite.Src);

// Red simulates the image.
g2d.setColor(Color.red);
g2d.setComposite(AlphaComposite.SrcAtop);

g2d.fillRect(0, 0, w, h);
}
}

及其沙箱

import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.JFrame;

public class Sandbox {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setMinimumSize(new Dimension(800, 600));
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new FlowLayout());

TPanel pnl = new TPanel();
f.getContentPane().add(pnl);

f.setVisible(true);
}
}

最佳答案

关于您的性能问题,Java 2D Trickery 文章包含一个链接,指向 Chet Haase 对 Intermediate Images 用法的非常好的解释。 .

我认为以下摘自 O'Reilly 的 Java Foundation Classes in a Nutshell 的摘录可能对您有所帮助,以便您理解 AlphaComposite 行为以及为什么中间图像可能是使用的必要技术.

The AlphaComposite Compositing Rules

The SRC_OVER compositing rule draws a possibly translucent source color over the destination color. This is what we typically want to happen when we perform a graphics operation. But the AlphaComposite object actually allows colors to be combined according to seven other rules as well.

Before we consider the compositing rules in detail, there is an important point you need to understand. Colors displayed on the screen never have an alpha channel. If you can see a color, it is an opaque color. The precise color value may have been chosen based on a transparency calculation, but, once that color is chosen, the color resides in the memory of a video card somewhere and does not have an alpha value associated with it. In other words, with on-screen drawing, destination pixels always have alpha values of 1.0.

The situation is different when you are drawing into an off-screen image, however. As you'll see when we consider the Java 2D BufferedImage class later in this chapter, you can specify the desired color representation when you create an off-screen image. By default, a BufferedImage object represents an image as an array of RGB colors, but you can also create an image that is an array of ARGB colors. Such an image has alpha values associated with it, and when you draw into the images, the alpha values remain associated with the pixels you draw.

This distinction between on-screen and off-screen drawing is important because some of the compositing rules perform compositing based on the alpha values of the destination pixels, rather than the alpha values of the source pixels. With on-screen drawing, the destination pixels are always opaque (with alpha values of 1.0), but with off-screen drawing, this need not be the case. Thus, some of the compositing rules only are useful when you are drawing into off-screen images that have an alpha channel.

To overgeneralize a bit, we can say that when you are drawing on-screen, you typically stick with the default SRC_OVER compositing rule, use opaque colors, and vary the alpha value used by the AlphaComposite object. When working with off-screen images that have alpha channels, however, you can make use of other compositing rules. In this case, you typically use translucent colors and translucent images and an AlphaComposite object with an alpha value of 1.0.

关于Java - 在 paintComponent 中合成的圆角面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9291836/

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