Java swt中的图形问题

Java swt中的图形问题,java,graphics,swt,Java,Graphics,Swt,我想构建一个能够垂直(上下)和水平(左右)滑动的小部件。当然,用户将设置此小部件的样式。 问题是,当我在右方向水平滑动的情况下测试小部件时,文本留下了一些绘制的痕迹。使用此方法,我绘制文本: private void paintText ( ) { if ( getText ( ) != null ) { Point point = gc.stringExtent ( getText ( ) ); if ( isHorizontalSlide )

我想构建一个能够垂直(上下)和水平(左右)滑动的小部件。当然,用户将设置此小部件的样式。 问题是,当我在右方向水平滑动的情况下测试小部件时,文本留下了一些绘制的痕迹。使用此方法,我绘制文本:

private void paintText ( )
{
    if ( getText ( ) != null )
    {
        Point point = gc.stringExtent ( getText ( ) );

        if ( isHorizontalSlide )
        {
            if ( getMode ( ) == LEFT_MODE )
            {
                if ( pX + point.x + 1 == rectangle.x )
                    pX = rectangle.width;
                gc.drawText ( getText ( ) , pX -- , pY );
            }
            else if ( getMode ( ) == RIGHT_MODE )
            {
                if ( pX == rectangle.width )
                    pX = rectangle.x - point.x + 1;
                gc.drawText ( getText ( ) , pX ++ , pY );
            }
            else
            {
                if ( pX + point.x + 1 == rectangle.x )
                    pX = rectangle.width;
                gc.drawText ( getText ( ) , pX -- , pY );
            }

        }
        else if ( isVerticalSlide )
        {

            if ( getMode ( ) == UP_MODE )
            {
                if ( pY + point.y + 1 == rectangle.y )
                    pY = rectangle.height;
                gc.drawText ( getText ( ) , pX , pY -- );
            }
            else if ( getMode ( ) == DOWN_MODE )
            {
                if ( pY == rectangle.height )
                    pY = rectangle.y - point.y + 1;
                gc.drawText ( getText ( ) , pX , pY ++ );
            }
            else
            {
                if ( pY + point.y + 1 == rectangle.y )
                    pY = rectangle.height;
                gc.drawText ( getText ( ) , pX , pY -- );
            }
        }
    }
    else
        throw new NullPointerException ( "The text cannot be null!" );
}

谁能告诉我跟踪标记有什么问题吗?

首先,看起来您正在缓存gc变量,以便在自己的方法中进行绘制。每当我看到自定义绘制时,它都会发生在添加到合成中的PaintListener的paintControl(PaintEvent)方法中,并且每次迭代都会从PaintEvent获取gc

除此之外,gc.drawText(…)只是将文本添加到目标区域。只有直接围绕文本的矩形才会填充背景色

在绘制文本之前,请尝试填充背景色。
是一个填充背景的swt绘画示例,是一个额外步骤并使用双缓冲防止闪烁的示例。

这是我的问题解决方案。这很简单,但效果很好:

private void paintText ( )
{
    if ( getText ( ) != null )
    {
        Point point = gc.stringExtent ( getText ( ) );

        if ( isHorizontalSlide )
        {
            if ( getMode ( ) == LEFT_MODE )
            {
                if ( pX + point.x + 1 >= rectangle.x )
                    pX = rectangle.width;
                gc.drawText ( getText ( ) , pX -- , pY );
            }
            else if ( getMode ( ) == RIGHT_MODE )
            {
                if ( pX >= rectangle.width )
                    pX = rectangle.x - point.x + 1;
                gc.drawText ( getText ( ) , pX ++ , pY );
                Color color = gc.getForeground ( );
                gc.setForeground ( getBackground ( ) );
                gc.drawLine ( pX - 1 , pY , pX - 1 , point.y );
                gc.setForeground ( color );
            }
            else
            {
                if ( pX + point.x + 1 >= rectangle.x )
                    pX = rectangle.width;
                gc.drawText ( getText ( ) , pX -- , pY );
            }

        }
        else if ( isVerticalSlide )
        {

            if ( getMode ( ) == UP_MODE )
            {
                if ( pY + point.y + 1 >= rectangle.y )
                    pY = rectangle.height;
                gc.drawText ( getText ( ) , pX , pY -- );
            }
            else if ( getMode ( ) == DOWN_MODE )
            {
                if ( pY >= rectangle.height )
                    pY = rectangle.y - point.y + 1;
                gc.drawText ( getText ( ) , pX , pY ++ );
            }
            else
            {
                if ( pY + point.y + 1 >= rectangle.y )
                    pY = rectangle.height;
                gc.drawText ( getText ( ) , pX , pY -- );
            }
        }
    }
    else
        throw new NullPointerException ( "The text cannot be null!" );
}