我有一些代碼應該讓播放器變成動畫,走路,但由於某種原因,它不起作用。這是我的代碼:動畫JPanel上的.GIF圖像
import javax.swing.*;
import java.awt.*;
/**
* Created by evengultvedt on 14.02.14.
*/
import javax.swing.*;
import java.awt.*;
//The board class, which the drawing is on
class Board extends JPanel {
//The image of the player
private Image imgPlayer;
public Board() {
setPreferredSize(new Dimension(400, 400));
setBackground(Color.WHITE);
setVisible(true);
//getting the player.gif file
ImageIcon player = new ImageIcon("player.gif");
//and put in the imgPlayer variable
imgPlayer = player.getImage();
}
public void paintComponent(Graphics graphics) {
Graphics2D graphics2D = (Graphics2D) graphics;
//this doesn't work
graphics2D.drawImage(imgPlayer, 10, 10, 100, 100, null);
//this works
graphics2D.drawString("Test drawing", 120, 120);
}
}
//The JFrame to put the panel on
class AnimatePlayer extends JFrame{
public AnimatePlayer() {
Board board = new Board();
add(board);
setTitle("PlayerTestAnimation");
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(400, 400);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new AnimatePlayer();
}
});
}
}
的player.gif文件是在一個文件中的兩個圖片,並保存在同一目錄中的java文件。
任何幫助表示讚賞,謝謝。對於只發布代碼抱歉,但我不知道你需要什麼更多的信息。請問是否有什麼東西。
作爲一個說明調用'super.paintComponent方法()'在該重寫方法 – nachokk
第一線什麼不起作用?圖像不顯示? –
是的,它不顯示任何東西只是一個空框架 – XBullet123