Java 6 / Swing:在JScrollPane的ViewPort / ViewPort组件之上绘制

时间:2014-07-26 21:59:18

标签: java swing rendering jscrollpane paint

我想实现一个自定义的JScrollPane,它可以在它自己的ViewPort / ViewPort组件的顶部的客户区上绘制。这样做的挑战是在用户滚动ViewPort时保持自定义绘制代码重新绘制,换句话说,重新绘制ViewPort组件。

我的目标是与iOS(iPhone / iPad)的半透明滚动条具有相同的效果,这些滚动条呈现在可滚动列表(而不是经典滚动条)之上。

所以这只是一个绘画问题,而不是关于滚动的问题:)

我已经有了一些工作,看起来像这样(下面的代码只渲染JScrollPane上方的绿色测试行):

public class ScrollPaneTest
{
    private JFrame          frame;
    private TestScrollPane  scrollPane;
    private JTextArea       textArea;

    public static void main( String[] args ) throws Exception
    {
        UIManager.setLookAndFeel( "com.sun.java.swing.plaf.windows.WindowsLookAndFeel" );
        new ScrollpaneTest();
    }

    public ScrollPaneTest()
    {
        configureUi();

        SwingUtilities.invokeLater( new Runnable() {
            @Override public void run() {
                frame.setVisible( true );
            }
        } );
    }

    private void configureUi()
    {
        textArea = new JTextArea();
        textArea.setWrapStyleWord( true );
        textArea.setLineWrap( true );
        textArea.setOpaque( false );
        textArea.setText( "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor "
                + "invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo "
                + "duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor "
                + "sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor "
                + "invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et "
                + "justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum "
                + "dolor sit amet." );

        scrollPane = new TestScrollPane();
        scrollPane.setViewportView( textArea );
        scrollPane.setLocation( 50, 50 );
        scrollPane.setSize( 200, 200 );

        frame = new JFrame( getClass().getSimpleName() );
        frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
        frame.setLayout( null );
        frame.setSize( 340, 380 );
        frame.setLocationRelativeTo( null );

        frame.add( scrollPane );
    }
}

public class TestScrollPane extends JScrollPane
{
    public TestScrollPane()
    {
        setOpaque( true );
        setBackground( Color.WHITE );
    }

    @Override
    protected JViewport createViewport()
    {
        JViewport viewPort = super.createViewport();
        viewPort.setOpaque( false );

        return viewPort;
    }

    @Override
    protected void paintChildren( Graphics g )
    {
        super.paintChildren( g );

        Graphics2D g2d = (Graphics2D)g;
        g2d.setColor( Color.GREEN );
        g2d.drawLine( 0, 0, 100, 100 );
    }

}

运行代码,它看起来像这样:

enter image description here

这里的关键是将ViewPort和ViewPort组件设置为非不透明:

// From: ScrollPaneTest.configureUi()
textArea.setOpaque( false );

// From: TestScrollPane.paintChildren()
JViewport viewPort = super.createViewport();
viewPort.setOpaque( false );

如果不将它们设置为非透明,则不会重新绘制JScrollPane,并且ViewPort组件(JTextArea)会覆盖绿线

现在我的相当简单的问题是

有更好的方法吗?必须在两个组件上使用setOpaque( false)才感觉不对。

非常感谢你的帮助!

1 个答案:

答案 0 :(得分:1)

  

必须在两个组件上使用setOpaque(false)才感觉不对。

好吧,也许更轻松一些。您可以覆盖JViewport的paintComponent()方法,然后您只需要在文本区域使用setOpaque(false),而不是视口:)

import java.awt.*;
import javax.swing.*;

public class ScrollPaneTest5
{
    private JFrame          frame;
    private JScrollPane  scrollPane;
    private JTextArea       textArea;

    public static void main( String[] args ) throws Exception
    {
        UIManager.setLookAndFeel( "com.sun.java.swing.plaf.windows.WindowsLookAndFeel" );
        new ScrollPaneTest5();
    }

    public ScrollPaneTest5()
    {
        configureUi();

        SwingUtilities.invokeLater( new Runnable() {
            @Override public void run() {
                frame.setVisible( true );
            }
        } );
    }

    private void configureUi()
    {
        textArea = new JTextArea();
        textArea.setWrapStyleWord( true );
        textArea.setLineWrap( true );
//        textArea.setOpaque( false );
        textArea.setText( "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor "
                + "invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo "
                + "duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor "
                + "sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor "
                + "invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et "
                + "justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum "
                + "dolor sit amet." );

        scrollPane = new JScrollPane();
//        scrollPane.setViewportView( textArea );
        scrollPane.setLocation( 50, 50 );
        scrollPane.setSize( 200, 200 );

        frame = new JFrame( getClass().getSimpleName() );
        frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
        frame.setLayout( null );
        frame.setSize( 340, 380 );
        frame.setLocationRelativeTo( null );

        frame.add( scrollPane );



        JViewport viewport = new JViewport()
        {
            public void paintComponent(Graphics g)
            {
                super.paintComponent(g);

                g.setColor( Color.BLUE );
                g.drawArc( 100, 100, 80, 80, 0, 360);
            }
        };

        textArea.setOpaque( false );
        viewport.setView(textArea);
        scrollPane.setViewport( viewport );
    }
}

否则,我认为你需要使用JLayer类。有关示例,请参阅How to Decorate Components with the JLayer Class上的Swing教程中的部分。