有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java看不到图片,只是显示了黑色背景

 public class Board extends JPanel implements ActionListener {    
 private Player player;
 private Timer timer;
 public ArrayList<Enemy> enemyList;
 public ArrayList<Bullet> bulletList;
 private Random randomNumber;
 public int WIDTH;
 public int HEIGHT;
 public Image plane;
 public Image enemyimage;
 private int x,y;
 private final int DELAY = 25;
 private final int B_WIDTH = 300;
 private final int B_HEIGHT = 300;
 private final int ICRAFT_X = 40;
 private final int ICRAFT_Y = 60;



public Board(){
     initBoard();   
}
public void initBoard(){
    addKeyListener(new TAdapter());
    setBackground(Color.BLACK);
    setFocusable(true);
    setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT));
    player = new Player();
    loadImage();
    timer = new Timer(DELAY, this);
    timer.start();
}

public void loadImage(){
    ImageIcon ii = new ImageIcon("src/resources/planeimage.png");
    plane = ii.getImage();
}



@Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        doDrawing(g);
    }

public void doDrawing(Graphics g){
    Graphics2D g2d = (Graphics2D) g;
    
    g2d.drawImage(player.getImage(),player.getX(),player.getY(),this);
    
    ArrayList<Bullet> bulletList = player.getBullets();
    
    for (Bullet bullet : bulletList){
        g2d.drawImage(bullet.getImage(),bullet.getX(),bullet.getY(),this);
    }
}



@Override
    public void actionPerformed(ActionEvent e){
        updateBullets();
        updatePlayer();
        repaint();
    }


public void updateBullets() {
    
    for (int i = 0; i < bulletList.size(); i++) {

        Bullet bullet = bulletList.get(i);

        if (bullet.isVisible()) {

            bullet.move();
        } else {

            bulletList.remove(i);
        }
    }
}

public void updatePlayer(){
    player.move();
}

    private class TAdapter extends KeyAdapter {

    @Override
    public void keyReleased(KeyEvent e) {
        player.keyReleased(e);
    }

    @Override
    public void keyPressed(KeyEvent e) {
        player.keyPressed(e);
    }
    }

这是扩展车辆类的玩家类。玩家构造函数在board类中被调用,我想,问题在于他们的

public class Player extends Vehicle{
 public ArrayList<Bullet> bulletList;

 public Player(){
     super();
     initPlayer();
 }



 public void initPlayer(){
     bulletList = new ArrayList<>();
     loadImage("src/resources/planeimage.png");
     getImageDimensions();
 }





@Override
 public void move(){
     x += dx;
     y += dy;
 }




public ArrayList<Bullet> getBullets(){
     return bulletList;
 }


public void keyPressed(KeyEvent e) {

    int key = e.getKeyCode();

    if (key == KeyEvent.VK_SPACE) {
        fire();
    }

    if (key == KeyEvent.VK_LEFT) {
        dx = -1;
    }

    if (key == KeyEvent.VK_RIGHT) {
        dx = 1;
    }

    if (key == KeyEvent.VK_UP) {
        dy = -1;
    }

    if (key == KeyEvent.VK_DOWN) {
        dy = 1;
    }
}



@Override
     public void fire() {
        bulletList.add(new Bullet(x + w, y + h / 2));
    }
 
 public void keyReleased(KeyEvent e){
    int key = e.getKeyCode();

    if (key == KeyEvent.VK_LEFT) {
        dx = 0;
    }

    if (key == KeyEvent.VK_RIGHT) {
        dx = 0;
    }

    if (key == KeyEvent.VK_UP) {
        dy = 0;
    }

    if (key == KeyEvent.VK_DOWN) {
        dy = 0;
    }
 }
}


public class Application extends JFrame {
 public Application() {

    initUI();
}

private void initUI() {

    add(new Board());

    setSize(1024, 768);

    setTitle("Application");
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}    

public static void main(String[] args) {
       EventQueue.invokeLater(() -> {
          
               Application ex = new Application(); 
               ex.setVisible(true);
    });
}

}

应用程序类是主类。Board类拥有所有代码。当我运行程序时,只显示黑色背景,而不是图像。为什么会这样,我做错了什么。多谢各位


共 (0) 个答案