0

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();
}
Uiyeob
  • 9
  • 2
  • 1
    You are supposed to override `paintComponent()`, not `paint()`. – RealSkeptic Jan 18 '17 at 13:32
  • Oh, thank you! But is there other problem? Because I tried to replace with paintComponent but it's still not working..... – Uiyeob Jan 18 '17 at 13:39
  • 1
    1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 3) `while (true) {` this is not the correct way to do animation within a Swing GUI. Instead use a Swing `Timer` to call `repaint();` 3) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders .. – Andrew Thompson Jan 18 '17 at 13:44
  • 2
    It's hard to tell as we don't know what is in your image. Try to reduce the program into a [mcve] (draw something simple instead of all the complicated stuff, etc.). Also please be aware of the Java language conventions. Variable and method names should be in lower camel case (`nameOfMethod`), and should not have underscores at all. – RealSkeptic Jan 18 '17 at 13:45
  • .. for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Jan 18 '17 at 13:48
  • Thank you guys, That was really startling admonition for me and it was helpful. I will be aware of name of varaiable and name of method next time and I will also try with Timer next time. – Uiyeob Jan 18 '17 at 14:07

0 Answers0