2011-03-12 55 views
0

我想构建一个能够垂直(上下)和水平(左右)滑动的控件。当然,用户将设置这个小部件的样式。 问题是,当我用正确的方向水平滑动测试小部件时,文本留下了一些绘制的痕迹。用这种方法我画了文字: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!"); 
} 

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

回答

0

这是我解决问题的方法。这很简单,但它的工作很好:

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

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

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

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

+0

这将无法正常工作。我已经试过了。 – 2011-03-12 14:00:09