2016-09-11 61 views
0

因此,當嘗試創建新遊戲並通過BufferedImage導入png時,JFrame變灰,並且我在其他類中繪製的各種對象消失。如果需要,我可以分享其他課程,但我不認爲這是不適用的原因。當使用BufferedImage時JFrame變灰灰色

public class Panel extends JPanel implements ActionListener, KeyListener { 
private Teeto game; 
private Player player; 
private Shrooms shroom; 
BufferedImage img; 

public Panel(Teeto game) { 
    setBackground(Color.WHITE); 
    this.game = game; 
    player = new Player(100, (game.HEIGHT/2) - 100, 200, KeyEvent.VK_UP, KeyEvent.VK_DOWN); 
    shroom = new Shrooms(100); 
    Timer timer = new Timer(5, this); 
    timer.start(); 
    addKeyListener(this); 
    setFocusable(true); 

    try { 
    img = ImageIO.read(new File("C:\\Users\\Patrick\\Desktop\\Teeto\\Yasuo.png")); 
    } catch (IOException e) {} 
} 

public void update() { 
    player.update(); 
    shroom.update(); 
    checkIntersection(); 
} 

public void checkIntersection() { 
    if (player.getBounds().intersects(shroom.getBounds())) { 
    player.health = player.health - 20; 
    } 
} 

public void actionPerformed(ActionEvent e) { 
    update(); 
    repaint(); 
} 

public void keyPressed(KeyEvent e) { 
    player.pressed(e.getKeyCode()); 
} 

public void keyReleased(KeyEvent e) { 
    player.released(e.getKeyCode()); 
} 

public void keyTyped(KeyEvent e) {} 

@Override 
public void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    player.paint(g); 
    shroom.paint(g); 
    g.drawImage(game.getPanel().img, 0, 0, null); 
} 
} 

回答

0

看來你的應用程序找不到文件「Yasuo.png」。確保它位於類文件所在的文件夾中。

+0

它確實但讓我嘗試指定路徑(是的沒有改變)。 –

+0

@JavaBeginner如果你的圖像作爲一個backgorund,你應該首先繪製它,然後把它放在它上面。它有幫助嗎? –

+0

不,不幸的是,它仍然只是一個灰色的沒有任何東西的Jframr。 –