Image floor=new ImageIcon("image/floor.jpg").getImage();
int floorwidth,floorheigth;
Image chara;
Graphics draw_chara;
public void floor(){
chara = createImage(250,250);
draw_chara = chara.getGraphics();//41
floorwidth=floor.getWidth(this);
floorheigth = floor.getHeight(this);
for(int i=0;i<250;i+=floorwidth)
for(int j=0;j<250;j+=floorwidth)
draw_chara.drawImage(floor, j, i, j+floorwidth, i+floorwidth, 0,0,floorwidth,floorwidth,this);
repaint();
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(chara, 0, 0, this);
}
public Migong(){
this.floor();
}###########################
提示错误:
Exception in thread "main" java.lang.NullPointerException
at migong.Migong.floor(Migong.java:41)
int floorwidth,floorheigth;
Image chara;
Graphics draw_chara;
public void floor(){
chara = createImage(250,250);
draw_chara = chara.getGraphics();//41
floorwidth=floor.getWidth(this);
floorheigth = floor.getHeight(this);
for(int i=0;i<250;i+=floorwidth)
for(int j=0;j<250;j+=floorwidth)
draw_chara.drawImage(floor, j, i, j+floorwidth, i+floorwidth, 0,0,floorwidth,floorwidth,this);
repaint();
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(chara, 0, 0, this);
}
public Migong(){
this.floor();
}###########################
提示错误:
Exception in thread "main" java.lang.NullPointerException
at migong.Migong.floor(Migong.java:41)
import java.awt.*;
class SWingConsole{
public static void run(final JFrame f,final int width,final int height){;
f.setTitle("window");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(width, height);
f.setLocation(400,200);
f.setVisible(true);
f.setResizable(false);
}
}/**
*
* @author 木偶
*/
class Jie_mian extends JFrame{
public Jie_mian(){
add(new Migong());
}
}
public class Migong extends JPanel{ /**
* @param args the command line arguments
*/
Image floor=new ImageIcon("image/floor.jpg").getImage();
int floorwidth,floorheigth;
Image chara;
Graphics draw_chara;
public void floor(){
chara = createImage(250,250);
draw_chara = chara.getGraphics();
floorwidth=floor.getWidth(this);
floorheigth = floor.getHeight(this);
for(int i=0;i<250;i+=floorwidth)
for(int j=0;j<250;j+=floorwidth)
draw_chara.drawImage(floor, j, i, j+floorwidth, i+floorwidth, 0,0,floorwidth,floorwidth,this);
repaint();
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(chara, 0, 0, this);
}
public Migong(){
this.floor();
}
public static void main(String[] args) {
// TODO code application logic here
SWingConsole.run(new Jie_mian(), 250, 250);
}}
at migong.Migong.floor(Migong.java:41)
at migong.Migong.<init>(Migong.java:55)
at migong.Jie_mian.<init>(Migong.java:27)
at migong.Migong.main(Migong.java:59)
chara = createImage(250, 250);
//此处报错了、空指向异常 //说明上一句返回的对象为null)
draw_chara = chara.getGraphics();
// ...
}
如果把chara = createImage(250,250);
draw_chara = chara.getGraphics();
floorwidth=floor.getWidth(this);
floorheigth = floor.getHeight(this);
for(int i=0;i<250;i+=floorwidth)
for(int j=0;j<250;j+=floorwidth)
draw_chara.drawImage(floor, j, i, j+floorwidth, i+floorwidth, 0,0,floorwidth,floorwidth,this);
加到 paintComponent中便可以运行
public Image createImage(int width, int height) {
ComponentPeer peer = this.peer;
if (peer instanceof LightweightPeer) {
if (parent != null) { return parent.createImage(width, height); }
else { return null;}
} else {
return (peer != null) ? peer.createImage(width, height) : null;
}
}
//返回了null。估计是覆盖了父类的某个方法,但是没有初始化一些东西。
//不过还是别纠结这个问题了、现在基本都不用AWT SWING开发了。你那个方法能解决问题的话就先这样子嘛。
应该是某个变量没有开辟空间
at migong.Migong.floor(Migong.java:41)
at migong.Migong.<init>(Migong.java:55)
at migong.Jie_mian.<init>(Migong.java:27)
at migong.Migong.main(Migong.java:59)
按照提示的行号去看看哪个变量没有开辟空间
//个人观点,你参考参考。