import java.awt.*;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.*;
public class planeinterface extends JFrame {
plane mp = null;
ImageIcon ple;
Image im;
public static void main(String args[]) {
planeinterface text = new planeinterface();
}
public planeinterface() {
mp = new plane();
ple = new ImageIcon("picture\\icon48x48.png");
Image im = ple.getImage();
this.add(mp);
this.setVisible(true);
this.setSize(300, 400);
}
public class plane extends JPanel {
;
int plane_x;
int plane_y;
public void paint(Graphics g) {
super.paint(g);
mp.setPlane_x(20);
mp.setPlane_y(20);
// g.draw3DRect(20, 30, 20, 30, rootPaneCheckingEnabled);
g.drawImage(im, getPlane_x(), getPlane_y(), 40, 40, mp);
}
public int getPlane_x() {
return plane_x;
}
public void setPlane_x(int plane_x) {
this.plane_x = plane_x;
}
public int getPlane_y() {
return plane_y;
}
public void setPlane_y(int plane_y) {
this.plane_y = plane_y;
}
}
}
------解决方案--------------------
public planeinterface() {
mp=new plane();
ple=new ImageIcon("picture\\icon48x48.png");
// Image im=ple.getImage();创建了局部对象,把字段隐藏了
im=ple.getImage();
this.add(mp);
setVisible(true);
this.setSize(300,400);
}
------解决方案--------------------
public void paint(Graphics g) {
super.paint(g);
mp.setPlane_x(20);
mp.setPlane_y(20);
// g.draw3DRect(20, 30, 20, 30, rootPaneCheckingEnabled);
g.drawImage(im, getPlane_x(), getPlane_y(), 40, 40, mp);
}
你这个值没赋上
------解决方案--------------------
定义im字段
Image im;
定义局部变量im,图片信息存在这个im中,而不是字段im
Image im=ple.getImage();
从im字段绘图,但im字段这是null,就显示不出图片了
g.drawImage(im,getPlane_x(),getPlane_y(),40,40,mp);
------解决方案--------------------
Image im = ple.getImage();图片被付给局部的im
g.drawImage(im, getPlane_x(), getPlane_y(), 40, 40, mp);这里im是null