所以我试图在JPanel上设置动画(连续绘制不同的png图像文件)但是如果我运行这个JPanel,则只有白屏。 我试图在JPanel上绘制BufferedImage
public void run() {
try {
while (true) {
if (GAME_STATE) {
..................
}
getContentPane().repaint();
getContentPane().revalidate();
Thread.sleep(GAME_SPEED);
}
} catch (Exception e) {
}
}
这是run方法,它调用repaint()和revalidate()
public class Panel_Game extends JPanel {
private static final long serialVersionUID = 1L;
Panel_Game() {
// set a preferred size for the custom panel.
this.setPreferredSize(new Dimension(f_width, f_height + 100));
this.setVisible(true);
this.setLayout(null);
this.setDoubleBuffered(true);
this.setFocusable(true);
this.requestFocus(true);
}
@Override
public void paint(Graphics g) {
buffImage = createImage(f_width, f_height + 100);
buffg = buffImage.getGraphics();
getContentPane().revalidate();
super.paintComponent(g);
Draw_Background();
Draw_Player();
Draw_Weapon();
Draw_Enemy();
Draw_EnemyWeapon();
Draw_Item();
.............
g.drawImage(buffImage, 0, 0, this);
}
}
这是我尝试在
上绘制图像的类public void Game_InterFace() {
JLabel Label_Menu_Board = new JLabel();
JLabel Label_AP_Board = new JLabel();
JLabel Label_Save_Board = new JLabel();
JLabel Label_Load_Board = new JLabel();
Panel_Game panel_Game = new Panel_Game();
panel_Game.addKeyListener(this);
.............................................
.............................................
panel_Game.add(btn_Menu);
panel_Game.add(btn_AP);
panel_Game.add(label_Status);
getContentPane().add(panel_Game);
getContentPane().repaint();
}
.......
public Game_main() {
start();
setLocation(0, 0);
setUndecorated(true);
setSize(f_width, f_height);
setResizable(false);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = getContentPane();
contentPane.setLayout(null);
Game_Home();
}