package test;import java.awt.Graphics;import javax.swing.JFrame;public class TestGetGraphics{ public static void main(String[] args){ JFrame f = new JFrame("TestGetGraphics");
/** * Creates a graphics context for this component. This method will * return <code>null</code> if this component is currently not * displayable. * @return a graphics context for this component, or <code>null</code> * if it has none * @see #paint * @since JDK1.0 */ 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.class中此方法 peer是组件的peer,peer实现了组件的行为。 当组件被加入到容器中时peer被创设,且此时组件也是peer。 因此当peer为null时,也即Component的行为没有被实现之前,getGraphics方法是得不到对象的* The peer of the component. The peer implements the component's * behavior. The peer is set when the <code>Component</code> is * added to a container that also is a peer.
楼主【sure2003】截止到2008-07-28 09:14:15的历史汇总数据(不包括此帖):
发帖的总数量:275 发帖的总分数:11233 每贴平均分数:40
回帖的总数量:240 得分贴总数量:10 回帖的得分率:4%
结贴的总数量:274 结贴的总分数:11193
无满意结贴数:2 无满意结贴分:50
未结的帖子数:1 未结的总分数:40
结贴的百分比:99.64 % 结分的百分比:99.64 %
无满意结贴率:0.73 % 无满意结分率:0.45 %
值得尊敬
public static void main(String[] args){
JFrame f = new JFrame("TestGetGraphics");
f.setSize(800, 600);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Graphics g = f.getGraphics();
//while(true)
g.drawLine(0, 0, 500, 500);
}
}以上是代码.运行时可以看到一闪而过的一条斜线,那是因为我没有持续画图,一刷新就没有了
如果在drawLine前加个while(true)
你就可以看到那条斜线了在JFrame没有setVisible(true)的时候,getGraphics()的返回结果是null
* Creates a graphics context for this component. This method will
* return <code>null</code> if this component is currently not
* displayable.
* @return a graphics context for this component, or <code>null</code>
* if it has none
* @see #paint
* @since JDK1.0
*/
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.class中此方法
peer是组件的peer,peer实现了组件的行为。
当组件被加入到容器中时peer被创设,且此时组件也是peer。
因此当peer为null时,也即Component的行为没有被实现之前,getGraphics方法是得不到对象的* The peer of the component. The peer implements the component's
* behavior. The peer is set when the <code>Component</code> is
* added to a container that also is a peer.
你可以在上面画你要的图像。
可以看成是一个画画用的画架,
而Graphics g中的g可以看成是画笔。
当组件被加入到容器中时peer被创设,且此时组件也是peer。
因此当peer为null时,也即Component的行为没有被实现之前