700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > java drawimage图片不完整 调了一天没调对 是drawImage问题 说是空指针

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

时间:2021-06-27 23:56:25

相关推荐

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

调了一天没调对,是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);

}

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。