java混合主动和被动渲染

时间:2012-04-20 09:22:42

标签: java swing

我正在开发一个有两个JPanel的应用程序。第一个JPanel用作绘图板,第二个用作属性/设置面板。所以绘图板应该使用主动渲染,第二个应该是被动渲染。它们都被添加到JFrame中。

我一直在阅读关于java中的活动渲染,但我注意到JPanel不支持createBufferStrategy。这是否意味着我需要使用画布? canvas的问题是它不是一个容器,所以我无法添加组件。我也可以使用JFrame缓冲策略(但是由于标题,我必须修复位置偏移?)..我可以使用JPanel主动渲染但仍然有第二个JPanel使用被动渲染吗?

1 个答案:

答案 0 :(得分:1)

以下是组合“被动”Swing组件和活动动画的一些示例:

public static void main ( String[] args )
{
    JFrame frame = new JFrame ();

    JPanel view = new JPanel ( null );
    view.setPreferredSize ( new Dimension ( 500, 500 ) );
    frame.add ( view );

    JButton button1 = new JButton ( "Button 1" );
    button1.setBounds ( 10, 10, 100, 40 );
    button1.setOpaque ( false );
    view.add ( button1 );

    Animator animator = new Animator ();
    animator.setBounds ( 0, 0, 500, 500 );
    view.add ( animator );

    JButton button2 = new JButton ( "Button 2" );
    button2.setBounds ( 390, 450, 100, 40 );
    button2.setOpaque ( false );
    view.add ( button2 );

    frame.setResizable ( false );
    frame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
    frame.pack ();
    frame.setLocationRelativeTo ( null );
    frame.setVisible ( true );
}

public static class Animator extends JComponent
{
    private float angle = 0;

    public Animator ()
    {
        super ();
        setOpaque ( false );

        new Timer ( 1000 / 24, new ActionListener ()
        {
            public void actionPerformed ( ActionEvent e )
            {
                angle += 0.2f;
                if ( angle > 360 )
                {
                    angle = 0;
                }

                repaint ();
            }
        } ).start ();

        addMouseListener ( new MouseAdapter ()
        {
            //
        } );
    }

    protected void paintComponent ( Graphics g )
    {
        super.paintComponent ( g );

        Graphics2D g2d = ( Graphics2D ) g;
        g2d.setRenderingHint ( RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON );

        GeneralPath shape = getShape ();

        g2d.setPaint ( Color.BLACK );
        g2d.fill ( shape );
    }

    public boolean contains ( int x, int y )
    {
        return getShape ().contains ( x, y );
    }

    private GeneralPath getShape ()
    {
        GeneralPath gp = new GeneralPath ( GeneralPath.WIND_EVEN_ODD );
        gp.append ( new Rectangle2D.Double ( -250, 150, 1000, 200 ), false );

        AffineTransform at = new AffineTransform ();
        at.rotate ( angle * Math.PI / 90, 250, 250 );
        gp.transform ( at );
        return gp;
    }
}

正如您所看到的,黑色旋转区域不仅覆盖右下方的按钮,还会阻止带有条纹按钮部分的鼠标事件。这是因为覆盖了Animator的contains()方法:

public boolean contains ( int x, int y )
{
    return getShape ().contains ( x, y );
}

默认情况下,组件会捕获父级中整个边界的鼠标事件,但通过更改此方法,您可以按照自己喜欢的方式使用它。

也可以做很多优化,例如每次重绘到某个变量后保存形状,并在检查“包含”值时返回它。

无论如何希望这对你的问题至少有所帮助......