分类:
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);
}
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))) {
//如果child的bound和clipRect没有交叉则不用绘制。
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里,这样只有这个需要改变。
}