开发者

Graphic problems in swt

I want to build a widget which will be capable to slide vertical(up and down) and horizontal (left and right). Of course the user will set the styles of this widget. The problem is that when I test the widget with horizontal sliding in right direction the text left some painted trace behind. With this method I paint the text:

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.drawTe开发者_运维技巧xt ( 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!" );
}

Could somebody tell me what is the problem with the trace mark?


First off, it looks like you're caching the gc variable in order to do painting in your own method. Whenever I've seen custom painting, it takes place in the paintControl(PaintEvent) method of a PaintListener added to the composite, and the gc is fetched from the PaintEvent on each iteration.

That aside, gc.drawText(...) simply adds text to the target area. Only the rectangle directly surrounding the text is filled with the background color.

Try filling in the background color before drawing the text. Here is an example of swt painting that fills in the background, and here is an example that goes an extra step and uses double-buffering to prevent flicker.


This is my solution of the problem. It's simple but it's work fine:

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!" );
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜