Chinaunix首页 | 论坛 | 博客
  • 博客访问: 508009
  • 博文数量: 95
  • 博客积分: 5168
  • 博客等级: 大校
  • 技术积分: 1271
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-28 23:31
文章分类

全部博文(95)

文章存档

2013年(2)

2012年(3)

2011年(1)

2010年(8)

2009年(81)

分类:

2009-10-13 00:28:39

-----------------------------------------------------------
本文系本站原创,欢迎转载!

转载请注明出处:http://sjj0412.cublog.cn/

-----------------------------------------------------------

gef中,Figure是基本图形单位.

其绘图函数是paint

Class Figure

public void paint(Graphics graphics) {

    if (getLocalBackgroundColor() != null)

       graphics.setBackgroundColor(getLocalBackgroundColor());

    if (getLocalForegroundColor() != null)

       graphics.setForegroundColor(getLocalForegroundColor());

    if (font != null)

       graphics.setFont(font);

 

    graphics.pushState();

    try {

       paintFigure(graphics);

             //绘制当前这个figure的内容

       graphics.restoreState();

       paintClientArea(graphics);

        //主要就是paint children.

       paintBorder(graphics);

//paint边框

    } finally {

       graphics.popState();

    }

}

绘制本身

protected void paintFigure(Graphics graphics) {

    if (isOpaque())

       graphics.fillRectangle(getBounds());

    if (getBorder() instanceof AbstractBackground)

       ((AbstractBackground) getBorder()).paintBackground(this, graphics, NO_INSETS);

}

 绘制客户端----绘制children

protected void paintClientArea(Graphics graphics) {

    if (children.isEmpty())

       return;

 

    boolean optimizeClip = getBorder() == null || getBorder().isOpaque();

 

    if (useLocalCoordinates()) {

       graphics.translate(

           getBounds().x + getInsets().left,

           getBounds().y + getInsets().top);

       if (!optimizeClip)

           graphics.clipRect(getClientArea(PRIVATE_RECT));

       graphics.pushState();

       paintChildren(graphics);

       graphics.popState();

       graphics.restoreState();

    } else {

       if (optimizeClip)

           paintChildren(graphics);

       else {

           graphics.clipRect(getClientArea(PRIVATE_RECT));

            //设置剪切域,作用是在剪切域中的绘制才会有效。

           graphics.pushState();

            //将画笔Font,大小等信息保存

           paintChildren(graphics);

            //绘制子figure.

           graphics.popState();

           graphics.restoreState();

       }

    }

}

 

protected void paintChildren(Graphics graphics) {

    IFigure child;

 

    Rectangle clip = Rectangle.SINGLETON;

    for (int i = 0; i < children.size(); i++) {

       child = (IFigure)children.get(i);

       if (child.isVisible() && child.intersects(graphics.getClip(clip))) {

//如果childboundclipRect没有交叉则不用绘制。

           graphics.clipRect(child.getBounds());

           //添加子figure的剪切域

           child.paint(graphics);

//

           graphics.restoreState();

       }

    }

}

 

绘制边框

protected void paintBorder(Graphics graphics) {

    if (getBorder() != null)

       getBorder().paint(this, graphics, NO_INSETS);

}

    

我们假设是LineBorder:

 

Clas LineBorder:

 

public void paint(IFigure figure, Graphics graphics, Insets insets) {

    tempRect.setBounds(getPaintRectangle(figure, insets));

    if (getWidth() % 2 == 1) {

       tempRect.width--;

       tempRect.height--;

    }

    tempRect.shrink(getWidth() / 2, getWidth() / 2);

//收缩线的1/2宽度.

    graphics.setLineWidth(getWidth());

    if (getColor() != null)

       graphics.setForegroundColor(getColor());

    graphics.drawRectangle(tempRect);

}

protected static final Rectangle getPaintRectangle(IFigure figure, Insets insets) {

    tempRect.setBounds(figure.getBounds());

    return tempRect.crop(insets);

}

public Rectangle crop(Insets insets) {

    if (insets == null)

       return this;

    x += insets.left;

    y += insets.top;

    width -= (insets.getWidth());

    height -= (insets.getHeight());

    return this;

}

public void repaint(int x, int y, int w, int h) {

    if (isVisible())

       getUpdateManager().addDirtyRegion(this, x, y, w, h);

//这个会重新调用this.paint,并将x, y, w, h加到insest里,这样只有这个需要改变。

}

 

阅读(1771) | 评论(0) | 转发(0) |
0

上一篇:gef中figure查找分析

下一篇:gef绘制机制(2)

给主人留下些什么吧!~~