2006年12月13日星期三

在Figure中作画

郁闷!一个下午就耗在这个弱智问题上了,拖出去喂鸟!

Figure中画基本几何图形,如线、折线、曲线、椭圆(包括圆了)等的时候,不能直接通过固定坐标来设置几何图形中的点坐标,而应该根据Figure的bounds来计算:
public void paint(Graphics graphics) {
Rectangle r = bounds;
PointList pl = new PointList(4);
pl.addPoint(r.x + r.width / 2, r.y);
pl.addPoint(r.x, r.y + r.height / 2);
pl.addPoint(r.x + r.width / 2, r.y + r.height - 1);
pl.addPoint(r.x + r.width, r.y + r.height / 2);
graphics.drawPolygon(pl);
}
上例中画了一个菱形,这里需要注意的是菱形的坐标,可以看出并不是简单的假设graphics开始画的区域的原点就是(0,0),而是从父Figure的原点开始画的。

另外Figure中众多的paintXXX系列方法也各有专用,不要乱覆盖:
  • paint() - 绘制Figure的全部内容
  • paintFigure() - 一般用于绘制Figure的背景或者Figure中的几何图形
  • paintBorder() - 绘制Figure的边框
  • paintChildren() - 调用子Figure的paint()
  • paintClientArea() - 绘制在Border和Insets之内的区域,这个区域一般放置的是子Figure,所以会调用paintChildren()

没有评论: