且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:1970-01-01 07:56:48

随着关于你的性能问题,Java 2D Trickery文章包含了一个关于Chet Haase关于使用中级图像

With respect to your performance concerns the Java 2D Trickery article contains a link to a very good explanation by Chet Haase on the usage of Intermediate Images.

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

I think the following excerpt from O'Reilly's Java Foundation Classes in a Nutshell could be helpful to you in order to make sense of the AlphaComposite behaviour and why intermediate images may be the necessary technique to use.


AlphaComposite合成规则

SRC_OVER合成规则在目标颜色上绘制可能半透明的源
颜色。这是我们在执行图形操作时通常想要的
。但是AlphaComposite
对象实际上允许根据其他七个
规则组合颜色。

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.

在我们详细考虑合成规则之前,你需要了解一个
重点。屏幕上显示的颜色
永远不会有alpha通道。如果你能看到颜色,那就是不透明的
颜色。可以根据
透明度计算选择精确的颜色值,但是,一旦选择了该颜色,颜色
就会驻留在某处的视频卡内存中,并且没有
与之相关的alpha值。换句话说,使用屏幕上的
绘图,目标像素的alpha值始终为1.0。

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.

当您绘制一个关闭时,情况会有所不同屏幕
图像,但是。正如您将在本章后面考虑Java 2D
BufferedImage类时所看到的那样,您可以在创建屏幕外图像时指定所需的
颜色表示。默认情况下,
BufferedImage对象将图像表示为RGB颜色数组,
但您也可以创建一个ARGB颜色数组的图像。这样的
图像具有与之关联的alpha值,当你在图像中绘制
时,alpha值仍然与你b $ b绘制的像素相关联。

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.

屏幕上和屏幕外绘图之间的区别很重要
,因为某些合成规则根据目标像素的
alpha值执行合成,而不是alpha值
源像素。通过屏幕绘制,目标像素
始终是不透明的(alpha值为1.0),但是对于屏幕外的
绘图,情况并非如此。因此,一些合成
规则仅在您绘制
具有Alpha通道的屏幕外图像时才有用。

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.

过度概括有点,我们可以说当你在屏幕上绘制
时,你通常坚持使用默认的SRC_OVER合成
规则,使用不透明的颜色,并改变
AlphaComposite使用的alpha值宾语。但是,在处理具有
alpha通道的屏幕外图像时,您可以使用其他合成规则。
在这种情况下,您通常使用半透明颜色和半透明的
图像以及Alpha值为1.0的AlphaComposite对象。

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.