首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

java Graphics对象是怎么创建的。他的构造方法是protected

2012-01-14 
java Graphics对象是如何创建的。他的构造方法是protected。public Graphics getGraphics() {if (peer insta

java Graphics对象是如何创建的。他的构造方法是protected。
public Graphics getGraphics() {
  if (peer instanceof LightweightPeer) {
  // This is for a lightweight component, need to
  // translate coordinate spaces and clip relative
  // to the parent.
  if (parent == null) return null;
  Graphics g = parent.getGraphics();
  if (g == null) return null;
  if (g instanceof ConstrainableGraphics) {
  ((ConstrainableGraphics) g).constrain(x, y, width, height);
  } else {
  g.translate(x,y);
  g.setClip(0, 0, width, height);
  }
  g.setFont(getFont());
  return g;
  } else {
  ComponentPeer peer = this.peer;
  return (peer != null) ? peer.getGraphics() : null;
  }
  }
Component没得调用Graphics的构造方法,但是他是如何创建这个对象的昵。

[解决办法]
你要想调用重写paint方法,然后参数是Graphics
你可以看到最后一句话peer.getGraphics(),这个东西是在sun包里面,不知道有没源码
你再看Graphics源码,有create无参数的重载方法,是抽象的,留给子类实现
随便找个子类看下

Java code
public Graphics create() {        DebugGraphics debugGraphics;        debugGraphics = new DebugGraphics();        debugGraphics.graphics = graphics.create();        debugGraphics.debugOptions = debugOptions;        debugGraphics.buffer = buffer;        return debugGraphics;    } 

热点排行