简体   繁体   English

java混合主动和被动渲染

[英]java mixing active and passive rendering

im developing an application that has two JPanels. 我正在开发一个有两个JPanels的应用程序。 The first JPanel is used as a drawing board and the second is used as a property/settings panel. 第一个JPanel用作绘图板,第二个用作属性/设置面板。 So the drawing board should use active rendering and the second one passive. 所以绘图板应该使用主动渲染,第二个应该是被动渲染。 They are both added to a JFrame. 它们都被添加到JFrame中。

I've been reading about active rendering in java but i've noticed that JPanel doesnt support createBufferStrategy. 我一直在阅读关于java中的主动渲染,但我注意到JPanel不支持createBufferStrategy。 Does this mean i need to use a canvas? 这是否意味着我需要使用画布? The problem with canvas is that it is not a container so i can't add components to it. canvas的问题是它不是一个容器,所以我无法添加组件。 I could also use the JFrame buffered strategy (but then i would have to fix the position offset due to the title?).. Could i use a JPanel to actively render on but still have the second JPanel use passive rendering? 我也可以使用JFrame缓冲策略(但是由于标题,我必须修复位置偏移?)..我可以使用JPanel主动渲染但仍然有第二个JPanel使用被动渲染吗?

Here is some example of combining "passive" Swing components and active animation: 以下是组合“被动”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;
    }
}

As you can see the black rotated area not just overlays the bottom right button, but also blocks mouse events on the covered with stripe button's part. 正如您所看到的那样,黑色旋转区域不仅覆盖右下方按钮,还会阻挡带有条纹按钮部分的鼠标事件。 That happens due to overriden Animator's contains() method: 这是因为覆盖了Animator的contains()方法:

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

By default component catches mouse events over the whole bounds in the parent, but by changing this method you can play with that the way you like. 默认情况下,组件会捕获父项中整个边界的鼠标事件,但通过更改此方法,您可以按照自己喜欢的方式使用它。

Also there could be done a lot of optimizations, for eg saving the shape after each repaint to some variable and return it when checking "contains" value. 此外,还可以进行大量优化,例如在每次重绘到某个变量后保存形状,并在检查“包含”值时返回它。

Anyway hope this helps atleast a bit in your question... 无论如何希望这有助于你的问题至少...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM