簡體   English   中英

swt中的圖形問題

[英]Graphic problems in 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 );
            }
            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!" );
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM