- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
根据最初的问题(下方),我现在悬赏以下内容:
基于 AlphaComposite
的圆角解决方案。
JPanel
进行演示。 希望有人能快速掌握它,这看起来很容易。
如果有一个很好解释的原因说明为什么永远无法做到这一点,并且其他人同意,我也会奖励赏金。
这是我所想的示例图像(但使用AlphaComposite
)
原始问题
我一直在尝试找出一种使用合成来制作圆角的方法,非常类似于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/
我有一个包含各种子元素的网格,例如网格、Stackpanel、图像...是否可以以裁剪所有内容的方式将网格的角变圆?此外,根网格的大小可能会有所不同,因此无法进行硬编码。 编辑:经过大量搜索,我发现这
我的应用程序采用非常圆润的字体设计,我希望使用勾选框作为 UI 的一部分。带有完全圆形勾选“框”的设计看起来不太好。我正在寻找圆角的标准勾选框,就像您使用容器一样。关于如何实现这一目标的任何想法。我试
我的应用程序采用非常圆润的字体设计,我希望使用勾选框作为 UI 的一部分。带有完全圆形勾选“框”的设计看起来不太好。我正在寻找圆角的标准勾选框,就像您使用容器一样。关于如何实现这一目标的任何想法。我试
所以我应该显示一个时间表,其中每个项目都有一个带有圆角的背景图像...... 我不知道如何为我的 View (当前是 TextView,但我可以更改它)提供背景(这是必须重复的图案)并为其提供圆角..
我想让 JTextArea 有一个圆角,我做了这段代码: public BPosTxtArea() { super(); setOpaque(false); } @Override p
我有两个项目,我在其中创建了一组扩展 card 类的自定义 cards,然后将它们插入到 StaggeredView 中。 我的问题是,在其中一个项目中,卡片角会自动变圆,而在另一个项目中则不会! 此
我正在寻找一些 c++ 绘图图形库来为动态键盘键创建器创建带有抗锯齿选项的圆角。我已经测试过 OpenCV 和 Magick++ 函数,但结果不是很好。谁能帮我解决这个问题? 这是一个使用 Magic
有没有办法使 QLineEdit 小部件的角变圆?如果没有,是否有类似的小部件我可以这样做? 视觉意义: 已解决:(请参阅下面的更多信息) QLineEdit *lineEdit = ne
我正在尝试创建具有圆角和背景的矩形作为重复位图。我是这样写的,但是在角落里得到了位图。 有人能帮忙吗? 背景.xml 最
我有 UIVIew 的代码: override func drawRect(rect: CGRect) { let toolbarSize = CGFloat(UIDevice.current
我正在开发一个 Windows 窗体应用程序(C#、.NET 4.0、VS 2010),其中我有一个非常标准的 MainForm 和一个 ToolStrip(GripStyle:Hidden,Dock
我设法绘制了一个矩形 :-) 但我不知道如何绘制圆角矩形。 谁能帮我解决以下如何四舍五入矩形的代码? let canvas = UIGraphicsGetCurrentContext() rec =
我有以下 SVG: 我想获得类似 CSS 的 border-top-right-radius 和 border-top-bottom-radius 效果。 如何实现
这个问题在这里已经有了答案: How to make an ImageView with rounded corners? (58 个回答) 关闭去年。 我希望图像具有圆角。我实现了这个 xml 代码
我使用此类别并为我的 UITableView 创建大小相同的图像。有没有办法让图像也有圆角?谢谢! + (UIImage *)scale:(UIImage *)image toSize:(CGSize
我对 iPhone 屏幕设计没有太多经验,但我需要制作一个像这样的表格:(图片),我进行了调查,但没有找到任何东西。该表需要有一个圆角,用户将能够插入数据,在本例中是名字、姓氏等......现在我使用
创建具有自定义背景图像的 NSButton 的最佳方法是什么,它能够具有可变的宽度,而不会使角边框看起来被拉伸(stretch)?我知道有一些方便的方法可以使用 UIButton 执行此操作:http
我知道你可以使用 .cornerRadius()将 swiftUI View 的所有角都圆角,但有没有办法只圆角特定的角,例如顶部? 最佳答案 有两个选项,您可以使用 View与 Path ,或者您可
我想在我的游戏屏幕上添加一些黑色轮廓,使其看起来像圆角。 这是我想要达到的效果: 我认为使用着色器可能很容易创建这种效果,而不是在所有内容之上绘制一个巨大的位图。 有人可以帮助我使用此效果的 GLSL
我的 NSTextField 有圆角和白色背景颜色,但 View 本身似乎是矩形的。这意味着圆角和边界矩形之间的空间显示为透明。我该如何填写该区域才能使其不突出? 图片: 代码: let textFi
我是一名优秀的程序员,十分优秀!