java drawimage图片不完整,调了一天没调对,是drawImage问题,说是空指针

ca56232b3bbedf9a539d07f37fffb99a.gif

3144d8b7615c79d9f638db40d5689d26.gif

a218af6549b45ee526caf607ebff1358.gif

0f8df0e29816ae721419de940fb833d1.gif

调了一天没调对,是drawImage问题,说是空指针

错误:Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

at mapDraw.pathDraw(mapDraw.java:127)

是说图片空指针,但是,上面一样的用法却是可以画图的。

下面g.drawImage(car, i.x, i.y, x, y, null);这句话错了;

import java.awt.Color; public class mapDraw extends JPanel {  /**

*

*/

private static final long serialVersionUID = 1L;

private Path sp=new Path();

private Vector path = new Vector();

private boolean[][] map = new boolean[10][10];

private int xx;

private int yy;

//private int [][]b;

public mapDraw(int xx2, int yy2) {

// TODO Auto-generated constructor stub

xx=xx2;

yy=yy2;

}  public void setMap() {

int m, n;

int i;

int w, h;   for (m = 0; m <= 9; m++) {

for (n = 0; n <= 9; n++) {

map[m][n] = false; // 初始化,false表示路通;true表示障碍;,1表示最短路径    }

}

for (i = 0; i < 10; i++) // 设置10个障碍

{

w = (int) (Math.random() * 10);

h = (int) (Math.random() * 10);

//b[0][i]=w;

//b[1][i]=h;

map[w][h] = true; // 设置为障碍

if (map[0][0] == true)//因为(0,0)是起始点,不能设置成障碍。

map[0][0] = false;

}

}  public boolean[][] getMap() {

return map;

}  /**

* Create the panel.

*/

public void draw() {

Graphics g = getGraphics();

int m, n;

// 加载地图背景

Image image = new ImageIcon(getClass().getResource("/image/map2.jpg")).getImage();

g.drawImage(image, 0, 0, 450, 500, null);

// 加载地图网格

int x = this.getSize().width / 10;

int y = this.getSize().height / 10;   for (m = 0; m < 10; m++) {

for (n = 0; n < 10; n++) {

g.setColor(Color.black);

g.drawRect(x * m, y * n, x, y);

}

}

//加载小车

//Image car = new ImageIcon(getClass().getResource("/image/car.jpg")).getImage();

//g.drawImage(car, 0, 0, x, y, this);

// 随机添加加障碍

for (m = 1; m < 10; m++)           //不画边界

for (n = 0; n < 10; n++) {

if (map[m][n] == true) {

Image bar = new ImageIcon(getClass().getResource("/image/b0.jpg")).getImage();

g.drawImage(bar, x * m, y * n, x, y, null);

}

}

//画路径

/*for (m = 0; m < 10; m++)           //不画边界

for (n = 0; n < 10; n++) {

if (map[m][n] == false) {

Image car = new ImageIcon(getClass().getResource("/image/car.jpg")).getImage();

g.drawImage(car, x * m, y * n, x, y, this);

}

}

*/

}

public void pathDraw(){

//找路径,画路径

Graphics g = getGraphics();

Image car = new ImageIcon(getClass().getResource("/image/c.jpg")).getImage();

int x = this.getSize().width / 10;

int y = this.getSize().height / 10;

sp.setMap(map, 10, 10);

sp.calcPath(0, 0, xx, yy);

System.out.print(path.isEmpty());

path=sp.getPath();

for(Point i:path){

g.drawImage(car, i.x, i.y, x, y, null);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值