加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java混合主动和被动渲染

发布时间:2020-12-15 02:33:28 所属栏目:Java 来源:网络整理
导读:我正在开发一个有两个JPanels的应用程序.第一个JPanel用作绘图板,第二个用作属性/设置面板.所以绘图板应该使用主动渲染,第二个应该是被动渲染.它们都被添加到JFrame中. 我一直在阅读关于java中的主动渲染,但我注意到JPanel不支持createBufferStrategy.这是否
我正在开发一个有两个JPanels的应用程序.第一个JPanel用作绘图板,第二个用作属性/设置面板.所以绘图板应该使用主动渲染,第二个应该是被动渲染.它们都被添加到JFrame中.

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

解决方法

以下是组合“被动”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,500,500 );
    view.add ( animator );

    JButton button2 = new JButton ( "Button 2" );
    button2.setBounds ( 390,450,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 );
}

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

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

无论如何希望这有助于你的问题至少…

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读