0

This is a continuation from my last post Java: Animated Sprites on GridLayout. Thanks to a reply, it gave me an idea in where I just had to insert a loop in the trigger condition and call pi[i].repaint() in it. So far it works. Though I tried to integrate it to my game which composed of multiple sprites, it had no improvement in it. Without the animation, the sprites show on the grid with no problems. I inserted the animation loop in the GridFile class and it didn't show. I also tried to insert the animation loop in the MainFile, it showed irregular animations, kinda like a glitch. Can someone tell me where did I went wrong? Ideas are welcome.

MainFile class

public class MainFile {

JFrame mainWindow = new JFrame();

public JPanel gridPanel;

public MainFile() { 
    gridPanel= new GridFile();

    mainWindow.add(gridPanel,BorderLayout.CENTER);

    mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainWindow.setSize(700,700);
    mainWindow.setResizable(false);
    mainWindow.setVisible(true);

}

public static void main(String[]args){

    new MainFile();

}

}

GridFile class

public class GridFile extends JPanel{   

ImageIcon gameBackground = new ImageIcon(getClass().getResource("Assets\\GridBackground.png"));
Image gameImage; 
int[] pkmArray = new int[12];
int random = 0;
Pokemon[] pkm = new Pokemon[36];
JPanel[] pokeball = new JPanel[36];
int j = 0;

public GridFile(){

    setLayout(new GridLayout(6,6,6,6));
    setBorder(BorderFactory.createEmptyBorder(12,12,12,12));
    gameImage = gameBackground.getImage();      


    for(int i = 0;i < 36;i++){  
        do{
            random = (int)(Math.random() * 12 + 0);

            if(pkmArray[random] <= 3){      
                pokeball[i] = new Pokemon(random);
                pokeball[i].setOpaque(false);
                pokeball[i].setLayout(new BorderLayout());
                pkmArray[random]++;
            }
        }while(pkmArray[random] >= 4);

        add(pokeball[i],BorderLayout.CENTER);
    } 

    while(true){
        for(int i = 0; i < 36; i++){
            pokeball[i].repaint();
        }
    }

}



public void paintComponent(Graphics g){

    if(gameImage != null){
        g.drawImage(gameImage,0,0,getWidth(),getHeight(),this);   
    }   
}

}

Community
  • 1
  • 1
Rapharlo
  • 89
  • 6
  • pokeball[i] = new Pokemon(random); shouldnt it be something like: pokeball[i].add(new Pokemon(random)); and this: pokeball[i].repaint(); -> pkm[i].repaint(); ? – JohnnyAW Sep 19 '13 at 13:51
  • 2
    whats wrong with linked code by trashgood and answer by Andrew Thompson, sorry I can't catch any significant progress, or am I wrong, correct me .... – mKorbel Sep 19 '13 at 13:54
  • 1
    For better help sooner, post an [SSCCE](http://sscce.org/) (e.g. like I did on your [last question](http://stackoverflow.com/a/18882304/418556)). – Andrew Thompson Sep 19 '13 at 14:20
  • Okay so I reverted back to my trial program from my last post, I was able to animate all the sprites, but the requirements is that the panel with the grid and the frame should be on separate classes (e.g. Main class and GridTest class). Annoyingly my Main class, even when my frame called the GridTest class on the .add() method, it didn't display the GUI.`public static void main(String[] args) { JFrame f = new JFrame("Game sample"); f.add(new GridTest(),BorderLayout.CENTER); f.setSize(700,700); f.setVisible(true); }` – Rapharlo Sep 20 '13 at 14:11
  • Tip: Add @mKorbel (or whoever) to *notify* them of a new comment. As to the latest comment. So ..what is your latest code? Enter it as an edit to the question. (Note that my SSCCE had multiple classes.) – Andrew Thompson Sep 21 '13 at 12:10

1 Answers1

3
  1. Use a swing Timer for the repainting, and give a bit time between the frames for swing to do the painting work. There's no point trying to draw faster than what could be displayed anyway. If you have the animation loop in main(), the repaint manager will try to drop some of the repaint requests that appear close to each other, which can be the cause of the irregular animation you see.
  2. You should create and access swing components only in the event dispatch thread. You current approach is breaking the threading rules.

Addition: When the animation loop is where you have it now, the GridFile constructor never returns, which explains that you'll see nothing because the code never gets far enough to show the window.

kiheru
  • 6,588
  • 25
  • 31