java 在 panel中画图 (线) 出现空指针错误。
publicclassYardextendsFrame{publicstaticfinalintROW=30;publicstaticfinalintCOLU=50;pu...
public class Yard extends Frame { public static final int ROW = 30; public static final int COLU = 50; public static final int SIZE = 10; Image offScreemImage = null; Panel pp = new Panel(); public static void main(String[] args) { new Yard().launch(); } public void launch(){ Panel pp = new Panel(); this.setLayout(null); pp.setBounds(5, 30, COLU*SIZE, ROW*SIZE); this.setBounds(100,100, COLU*SIZE+10, ROW*SIZE+30); this.add(pp,BorderLayout.SOUTH); this.setVisible(true); PaintTheard pt = new PaintTheard(); Thread t = new Thread(pt); t.start(); } public void paint(Graphics g){ Graphics p = pp.getGraphics(); //这里在 拿不到panel的 Graphics 。 Color c = p.getColor(); p.setColor(Color.GRAY); p.drawRect(0, 0, COLU*SIZE, ROW*SIZE); p.setColor(Color.BLUE); for(int i = 1;i<ROW;i++){ p.drawLine(0, i*SIZE, COLU*SIZE, i*SIZE); } for(int i = 1;i<COLU;i++){ p.drawLine(i*SIZE,0 , i*SIZE, ROW*SIZE); } p.setColor(c); } public void update(Graphics p){ if(offScreemImage == null){ return; } Image offImage = createImage(COLU*SIZE, ROW*SIZE); Graphics goffGraphics = offScreemImage.getGraphics(); paint(goffGraphics); p.drawImage(offScreemImage, 0, 0, null); } class PaintTheard implements Runnable{ public void run() { repaint(); try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } } }}
展开
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励10(财富值+成长值)+提问者悬赏5(财富值+成长值)
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询