그래서 나는 JApplet에를 확장하고 그것의 생성자에서 내가 이후에 상자를 그릴 것하는 JPanel을합니다,하지만 난로 getGraphics를 수행하려고 할 때 null를 돌려 클래스 보드가 :/로 getGraphics 반환 널
JPanel panel;
public Board(int x, int y, int wolfNumber, int hareNumber){
this.x=x;
this.y=y;
wolvesCoords = new int[wolfNumber][2];
haresCoords = new int[hareNumber][2];
panel = new JPanel();
panel.setVisible(true);
add(panel);
}
public synchronized void write(int xx, int yy, Color c){
int width=panel.getWidth()/x;
int height=panel.getHeight()/y;
Graphics g = panel.getGraphics();
System.out.println(g);
g.setColor(c);
g.drawRect(xx*width, yy*height, width, height);
g.fillRect(xx*width, yy*height, width, height);
}
public void paint(Graphics g)
{
super.paint(g);
}
을
g가 null 일 때 g.setColor (c) 라인에서 nullpointerexception을 제공합니다.
이'write' 메소드는 무엇입니까? 'Graphics' 객체로'paint' 메소드에서 드로잉 코드를 호출하지 않는 이유는 무엇입니까? – durron597
칠판에 도료를 씁니다.g가 null이고 이것이 현재의 문제이기 때문에 저는 페인트를 호출하지 않습니다. – user3369008
아니요,'paint'를 직접 호출하지 마십시오. Java는 자동으로'paint'를 호출 할 것입니다. http://www.oracle.com/technetwork/java/painting-140037.html – durron597