/*
在一个图上画一个线
有关Frame中getGraphics()方法的使用问题
*/import java.awt.*;
import java.awt.event.*;
public class DrawLine
{
Frame f= new Frame("IT资讯交流网");
public static void main(String [] args)
{
new DrawLine().init();
}
public void init()
{
f.setSize(300,300);
f.setVisible(true);
f.addMouseListener(new MouseAdapter()
{
int orgX;
int orgY;
public void mousePressed(MouseEvent e)
{
orgX=e.getX();
orgY=e.getY();
}
public void mouseReleased(MouseEvent e)
{
f.getGraphics().setColor(Color.red);//25 行
f.getGraphics().drawLine(orgX,orgY,e.getX(),e.getY()); //26行
}
}
);
}
}
/*
张孝祥说: 25行和26行的f.getGraphics()返回的不是同一个Graphics对象
请问为什么返回的不是同一个对象啊????请诸位指教
*/
在一个图上画一个线
有关Frame中getGraphics()方法的使用问题
*/import java.awt.*;
import java.awt.event.*;
public class DrawLine
{
Frame f= new Frame("IT资讯交流网");
public static void main(String [] args)
{
new DrawLine().init();
}
public void init()
{
f.setSize(300,300);
f.setVisible(true);
f.addMouseListener(new MouseAdapter()
{
int orgX;
int orgY;
public void mousePressed(MouseEvent e)
{
orgX=e.getX();
orgY=e.getY();
}
public void mouseReleased(MouseEvent e)
{
f.getGraphics().setColor(Color.red);//25 行
f.getGraphics().drawLine(orgX,orgY,e.getX(),e.getY()); //26行
}
}
);
}
}
/*
张孝祥说: 25行和26行的f.getGraphics()返回的不是同一个Graphics对象
请问为什么返回的不是同一个对象啊????请诸位指教
*/
/*
在一个图上画一个线
有关Frame中getGraphics()方法的使用问题
*/import java.awt.*;
import java.awt.event.*;
public class DrawLine
{
Frame f= new Frame("IT资讯交流网");
public static void main(String [] args)
{
new DrawLine().init();
}
public void init()
{
f.setSize(300,300);
f.setVisible(true);
f.addMouseListener(new MouseAdapter()
{
int orgX;
int orgY;
public void mousePressed(MouseEvent e)
{
orgX=e.getX();
orgY=e.getY();
}
public void mouseReleased(MouseEvent e)
{
f.getGraphics().setColor(Color.red);//25 行
f.getGraphics().drawLine(orgX,orgY,e.getX(),e.getY()); //26行
}
}
);
}
}
/*
张孝祥说: 25行和26行的f.getGraphics()返回的不是同一个Graphics对象
请问为什么返回的不是同一个对象啊????请诸位指教
*/
getGraphics
public Graphics getGraphics()
Creates a graphics context for this component. This method will return null if this component is currently not displayable.
注意他说的是创建,可能是这个原因吧(不知道对不对)
刚刚我在3个不同的相邻的位置打印了2个f.getGraphics()的hashcode,确实是不一样的