So I'm trying to animate(draw different png image file continuously) on JPanel but if I run this JPanel, there is only white screen. And I'm trying to draw BufferedImage on the JPanel
public void run() {
try {
while (true) {
if (GAME_STATE) {
..................
}
getContentPane().repaint();
getContentPane().revalidate();
Thread.sleep(GAME_SPEED);
}
} catch (Exception e) {
}
}
This is run method which calls repaint() and 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);
}
}
And this is a class that I try to draw image on
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();
}