本文介绍了Java - 在paintComponent中使用合成的圆角面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从最初的问题(下面),我现在提供以下奖励:

From the original question (below), I am now offering a bounty for the following:

一个 AlphaComposite 基于圆角的解决方案。

An AlphaComposite based solution for rounded corners.


  • 请演示 JPanel

  • 角落必须完全透明。

  • 必须能够支持JPG绘画,但仍有圆角

  • 不得使用setClip(或任何剪辑)

  • 必须有不错的表现

  • Please demonstrate with a JPanel.
  • Corners must be completely transparent.
  • Must be able to support JPG painting, but still have rounded corners
  • Must not use setClip (or any clipping)
  • Must have decent performance

希望有人快速选择这个,看起来很容易。

Hopefully someone picks this up quick, it seems easy.

如果有一个充分解释的原因,为什么永远不会这样做,其他人同意,我也会奖励赏金。

I will also award the bounty if there is a well-explained reason why this can never be done, that others agree with.

这是我想到的一个示例图像(但使用 AlphaComposite

Here is a sample image of what I have in mind (but usingAlphaComposite)

原始问题

我一直试图找出一种使用合成来做圆角的方法,非常类似于或。

I've been trying to figure out a way to do rounded corners using compositing, very similar to How to make a rounded corner image in Java or http://weblogs.java.net/blog/campbell/archive/2006/07/java_2d_tricker.html.

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

However, my attempts without an intermediate BufferedImage don't work - the rounded destination composite apparently doesn't affect the source. I've tried different things but nothing works. Should be getting a rounded red rectangle, instead I'm getting a square one.

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

So, I have two questions, really:

1)有没有办法使这项工作?

1) Is there a way to make this work?

2)中间图像是否会产生更好的效果?

2) Will an intermediate image actually generate better performance?

SSCCE:

测试小组 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关于使用。

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.

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.

这篇关于Java - 在paintComponent中使用合成的圆角面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-20 05:19