import javax.swing.*;import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;public class MyPuzzles extends Applet {
/**
*
*/
private static final long serialVersionUID = 1L;
private Image []pic;
private int last_x;
private int last_y;
private boolean able;
Font font;
public void init() {
resize(480,640);
setBackground(Color.orange);
pic = new Image[3];
pic[0] = getImage(getCodeBase(), "img/img1.jpg");
pic[1] = getImage(getCodeBase(), "img/img2.jpg");
pic[2] = getImage(getCodeBase(), "img/img3.jpg");
MediaTracker media = new MediaTracker(this);
for(int i =0; i < 3; i++) {
media.addImage(pic[i], 0);
}
try {
media.waitForID(0);
} catch (Exception e) {
System.out.println("fail to load the pictures.");
}
font = new Font("TimesRoman", Font.BOLD, 48);
}
public void paint(Graphics g) {
if(!able){
g.setColor(Color.red);
g.drawImage(pic[0], 25, 35, 130, 260, this);
g.drawImage(pic[1], 25, 310, 130, 260,this);
g.drawImage(pic[2], 170, 170, 130, 260 , this);
g.setFont(font);
g.drawString("Choose one", 275, 35);
}
}
public static void main(String[] args) {
JFrame f = new JFrame("Puzzles");
MyPuzzles puzzles = new MyPuzzles();
f.setSize(480,640);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(
puzzles, BorderLayout.CENTER);
puzzles.init();
puzzles.start();
f.setVisible(true);
}
}
代码运行的时候在pic[0] = getImage(getCodeBase(), "img/img1.jpg");出错,java.lang.NullPointerException是pic[0]的得到的是null引起的吗?Java异常
import java.awt.*;
import java.awt.event.*;public class MyPuzzles extends Applet {
/**
*
*/
private static final long serialVersionUID = 1L;
private Image []pic;
private int last_x;
private int last_y;
private boolean able;
Font font;
public void init() {
resize(480,640);
setBackground(Color.orange);
pic = new Image[3];
pic[0] = getImage(getCodeBase(), "img/img1.jpg");
pic[1] = getImage(getCodeBase(), "img/img2.jpg");
pic[2] = getImage(getCodeBase(), "img/img3.jpg");
MediaTracker media = new MediaTracker(this);
for(int i =0; i < 3; i++) {
media.addImage(pic[i], 0);
}
try {
media.waitForID(0);
} catch (Exception e) {
System.out.println("fail to load the pictures.");
}
font = new Font("TimesRoman", Font.BOLD, 48);
}
public void paint(Graphics g) {
if(!able){
g.setColor(Color.red);
g.drawImage(pic[0], 25, 35, 130, 260, this);
g.drawImage(pic[1], 25, 310, 130, 260,this);
g.drawImage(pic[2], 170, 170, 130, 260 , this);
g.setFont(font);
g.drawString("Choose one", 275, 35);
}
}
public static void main(String[] args) {
JFrame f = new JFrame("Puzzles");
MyPuzzles puzzles = new MyPuzzles();
f.setSize(480,640);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(
puzzles, BorderLayout.CENTER);
puzzles.init();
puzzles.start();
f.setVisible(true);
}
}
代码运行的时候在pic[0] = getImage(getCodeBase(), "img/img1.jpg");出错,java.lang.NullPointerException是pic[0]的得到的是null引起的吗?Java异常
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货