Graphics和Graphics2D都是抽象函数,
我们来看一段代码:
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D)g ;
Rectangle2D rect = new Rectangle2D.Double(100, 100, 100, 100);
g2.draw(line);
}
在这里,抽象类对象g2直接调用了抽象函数draw(),我当时就怪了,在这里,抽象类并没有实现抽象函数,怎么就实现了功能?
我开始以为应该是传入参数g的时候是实现了东西,于是,查了下:protected Graphics() {
}
protected Graphics2D() {
}
发现构造器中也是空的!那么,既然没有实现函数,怎么就能实现函数的功能???到底是那个神奇的一步啊,我菜鸟,不明白,求解惑!!!
我们来看一段代码:
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D)g ;
Rectangle2D rect = new Rectangle2D.Double(100, 100, 100, 100);
g2.draw(line);
}
在这里,抽象类对象g2直接调用了抽象函数draw(),我当时就怪了,在这里,抽象类并没有实现抽象函数,怎么就实现了功能?
我开始以为应该是传入参数g的时候是实现了东西,于是,查了下:protected Graphics() {
}
protected Graphics2D() {
}
发现构造器中也是空的!那么,既然没有实现函数,怎么就能实现函数的功能???到底是那个神奇的一步啊,我菜鸟,不明白,求解惑!!!
public abstract class Father {
public Father () {}
abstract public method();
}public class Child extends Father {
@Override
public method() {
}
}
首先,Graphics的确是个类
其实,传参的时候的确实现了 Graphics 。但我的问题在于,没理解何处实现!
因为我自己无法实现它,我实现的只能是传入一个null !
sorry,前几天有事,现在才回复,我这里没有代码,只是看java源码想到的,如果非要有个什么代码,
基本上就是:
protected void paintComponent(Graphics g)
{
super.paint(g);
g.Method();//这种形式
}
这种形式而已 ... 我思考不通!
你这个很有意思,是它的一个实现的子类 ! 不过,就算没有Graphics2D , 也是可以g.Method()的,我思考不通在此处!
我的理解是, 传入了一个 (Graphics) new DebugGraphics() ;这是一个我自己的思路,但这解释不了,是何处传入一个 被实现的Graphics,
从而使得paintComponent()有效并被调用与绘制面板图像 !
赋给Graphics2D 类型的参数g2,因为Graphics2D是继承Graphics类的,那么肯定会覆写Graphics类的draw()方法,所以g2可以调用draw()方法,这是我的个人观点!
恕我直言,我真心不知道PathGraphics、PeerGraphics、ProxyGraphics2D、SunGraphics2D。
我的JDK1.6文档没有,我查http://docs.oracle.com/javase/7/docs/api/index.html,
也不存在你的sun包,更别说sun.java2D.SunGraphics2D等类了 。 能顺便解释下原因不? 谢谢 ...
按你的网址是看到东西了,但自己找不到diaplayCode.html