自定义边框外的组件绘画 [英] Component painting outside custom border

查看:141
本文介绍了自定义边框外的组件绘画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个自定义边框类中,我定义了一个 RoundRectangle2D 形状。此对象用于绘制边框。不幸的是,因为 JComponent paint 方法调用 paintComponent paintBorder 之前,将 Graphics 剪辑设置为 RoundRectangle2D 形状没有效果;即使我发出重绘。因此,组件将在其边框外绘制,这是可以理解的不可取的。

In this custom border class, I define a RoundRectangle2D shape. This object is used to paint the border. Unfortunately, since the paint method of a JComponent invokes paintComponent before paintBorder, setting the Graphics clip to the RoundRectangle2D shape has no effect; even if I issue a repaint. Therefore, the component will paint outside it's border, which is understandably undesirable.

所以,我想知道:如何让组件专门绘制 在自定义边框内?

So, I was wondering: how do I get the component to paint exclusively inside a custom border?

我考虑的一种方法是在<$ c中获取组件的 Border 对象$ c> paintComponent 方法。然后将此对象转换为适当的类,其中我定义将影响剪辑的参数。但这似乎不是一个声音设计。

One approach I considered was obtaining the component's Border object in the paintComponent method. And then casting this object to the appropriate class, wherein I define parameters that will influence the clip. But this didn't seem like a "sound" design.

编辑 -

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.border.AbstractBorder;

class JRoundedCornerBorder extends AbstractBorder 
{   
    private static final long serialVersionUID = 7644739936531926341L;
    private static final int THICKNESS = 2;

    JRoundedCornerBorder()
    {
        super();
    }

    @Override
    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) 
    {
        Graphics2D g2 = (Graphics2D)g.create();

        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        if(c.hasFocus())
        {
            g2.setColor(Color.BLUE);
        }
        else
        {
            g2.setColor(Color.BLACK);
        }
        g2.setStroke(new BasicStroke(THICKNESS, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
        g2.drawRoundRect(THICKNESS, THICKNESS, width - THICKNESS - 2, height - THICKNESS - 2, 20, 20);

        g2.dispose();
    }

    @Override
    public Insets getBorderInsets(Component c) 
    {
        return new Insets(THICKNESS, THICKNESS, THICKNESS, THICKNESS);
    }

    @Override
    public Insets getBorderInsets(Component c, Insets insets) 
    {
        insets.left = insets.top = insets.right = insets.bottom = THICKNESS;
        return insets;
    }

    public boolean isBorderOpaque() {
        return false;
    }

    public static void main(String[] args) 
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run() 
            {
                final JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new FlowLayout());

                // Add button with custom border
                final JButton button = new JButton("Hello");
                button.setBorder(new JRoundedCornerBorder());
                button.setBackground(Color.YELLOW);
                button.setPreferredSize(new Dimension(200, 200));
                frame.add(button);

                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

红色圆圈突出显示组件延伸到其边界之外的位置。

The red circles highlight where the component extends beyond it's border.

推荐答案

啊......最后我明白了(由于某种原因错过了roundBorder,我的错:-)按钮只是遵守它合同:它表示不透明,因此必须填写其完整区域,包括边界区域。所以设置一个剪辑(你可以在paintComponent中做)会违反合同:

ahh ... at last I got it (missed the roundBorder for some reason, my fault :-) The button is simply complying to its contract: it states being opaque, so must fill its complete area, including that of the border. So setting a clip (which you could do in paintComponent) would make violate the contract:

 // DO NOT - a opaque component violates its contract, as it will not fill
 // its complete area  
        @Override
        protected void paintComponent(Graphics g) {
            if (getBorder() instanceof JRoundedCornerBorder) {
                Shape borderShape = ((JRoundedCornerBorder) getBorder()).
                    getBorderShape(getWidth(), getHeight());
                g.setClip(borderShape);
            }
            super.paintComponent(g);
        }

丑陋但安全的是将自己报告为透明并接管背景画你自己:

Ugly but safe would be to report itself as transparent and take over the background painting yourself:

        @Override
        protected void paintComponent(Graphics g) {
            if (getBorder() instanceof JRoundedCornerBorder) {
                g.setColor(getBackground());
                Shape borderShape = ((JRoundedCornerBorder) getBorder())
                    .getBorderShape(getWidth(), getHeight());
                ((Graphics2D) g).fill(borderShape);
            }
            super.paintComponent(g);
        }

        @Override
        public boolean isContentAreaFilled() {
            if (getBorder() instanceof JRoundedCornerBorder) return false;
            return super.isContentAreaFilled();
        }

        // using
        button.setOpaque(false);

这篇关于自定义边框外的组件绘画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆