0

So the problem I have is under the method gameplay(). When we ( I did not write this code myself so I may have some trouble answering certain things ) tried to update the ImageComponent to a different image, refresh the JPanel to show the two new Images, delay the code 1 second, and the show the next image, it did not work. When I run the code, what heppens is that the screen freezes for the intended delay, and then what should show up in the last iteration of the while loop appears. Basically, it skips from the starting 2 images to the last 2. Even when I took out repaint(); and revalidate(); it did the same thing, so I believe it is not refreshing at all. What should I do?

import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;  
import javax.swing.Timer;

public class Background {

private Pokemon user;
private Pokemon computer;
private final JFrame frame = new JFrame();
private final JLabel userMove = new JLabel("");
private final JLabel aiMove = new JLabel("");
private final JLabel hu = new JLabel("");
private final JLabel pch = new JLabel("");
private final JLabel moveFirst = new JLabel("");
private final JPanel mainPanel = new JPanel();
private JPanel backS = new JPanel( new FlowLayout(FlowLayout.LEFT, 0, 0)); //coderanch.com, written by David Bryon
private JComponent left;
private JComponent right;
private final long PERIOD = 500L; // Adjust to suit timing
private long lastTime = System.currentTimeMillis() - PERIOD;
Timer timer;

public void execute(int choice){
    frame.setSize( 1000, 500);
    backS.setSize(300, 1000);
    final JPanel buttonPanel = new JPanel();


    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);



    //user choice for a pokemon, takes number from selection screen
    if(choice == 1){
        user = new Kyogre();
        computer = aiRandom();
    }
    else if(choice == 2){
        user = new Groudon();
        computer = aiRandom();
    }
    else if(choice == 3){
        user = new Mewtwo();
        computer = aiRandom();
    }
    else if(choice == 4){
        user = new Arceus();
        computer = aiRandom();
    }
    else if(choice == 5){
        user = new Pikachu();
        computer = aiRandom();
    }
    else if(choice == 6){
        user = new Snorlax();
        computer = aiRandom();
    }



    //health bars
    JPanel healthBarUser = new JPanel();
    healthBarUser.setSize(500, 100);
    healthBarUser.setBackground(Color.GRAY);
    String h1 = "";
    for(int i = 0; i < user.getHealth(); i+=10){
        h1 += "|";
    }
    String h2 = "";
    for(int i = 0; i < computer.getHealth(); i+=10){
        h2 += "|";
    }
    /*try{
        Thread.sleep(100);
    }
    catch(Exception e)
    {
        System.out.println("Exception caught");
    }*/
    hu.setText("Your HP: " + h1 + user.getHealth() +"      ");
    healthBarUser.add(hu);

    JPanel healthBarPC = new JPanel();
    healthBarPC.setBackground(Color.GRAY);
    healthBarPC.setSize(500, 100);

    pch.setText("Computer's HP: " + h2+ computer.getHealth());
    healthBarUser.add(pch);
    left = user.leftSide();
    right = computer.rightSide();
    backS.add(left);
    backS.add(right);



    //move buttons
    JButton button1 = new JButton(user.accessMoves(0).getName());
    JButton button2 = new JButton(user.accessMoves(1).getName());
    JButton button3 = new JButton(user.accessMoves(2).getName());
    JButton button4 = new JButton(user.accessMoves(3).getName());


    userMove.setText( "You used: " );
    aiMove.setText( "Computer used: " + "                       ");


    moveFirst.setText("_____ attacked first");        

    //4 different button listeners
    class b1Listener implements ActionListener{
        public void actionPerformed(ActionEvent event){
            gamePlay(0);
            if(computer.getHealth() <=0){
                frame.remove(mainPanel);
                frame.setSize(1050, 500);
                gameEnd(0);
            }
            else if(user.getHealth() <= 0){
                frame.remove(mainPanel);
                frame.setSize(1050, 500);
                gameEnd(1);
            }

          }
       }

    class b2Listener implements ActionListener{
        public void actionPerformed(ActionEvent event){
            gamePlay(1);
            if(computer.getHealth() <=0){
                frame.remove(mainPanel);
                frame.setSize(1050, 500);
                gameEnd(0);
            }
            else if(user.getHealth() <= 0){
                frame.remove(mainPanel);
                frame.setSize(1050, 500);
                gameEnd(1);
            }
          }
       }

    class b3Listener implements ActionListener{
        public void actionPerformed(ActionEvent event){
            gamePlay(2);
            if(computer.getHealth() <=0){
                frame.remove(mainPanel);
                frame.setSize(1050, 500);
                gameEnd(0);
            }
            else if(user.getHealth() <= 0){
                frame.remove(mainPanel);
                frame.setSize(1050, 500);
                gameEnd(1);
            }
          }
       }

    class b4Listener implements ActionListener{
        public void actionPerformed(ActionEvent event){
            gamePlay(3);
            if(computer.getHealth() <=0){
                frame.remove(mainPanel);
                frame.setSize(1050, 500);
                gameEnd(0);
            }
            else if(user.getHealth() <= 0){
                frame.remove(mainPanel);
                frame.setSize(1050, 500);
                gameEnd(1);
            }
          }
       }

//adds liteners to buttons
    button1.addActionListener( new b1Listener() );
    button2.addActionListener( new b2Listener());
    button3.addActionListener( new b3Listener() );
    button4.addActionListener( new b4Listener() );



//adds all panels, text fields, etc to final pane
    mainPanel.add(healthBarUser);
    mainPanel.add(healthBarPC);
    mainPanel.add(backS);
    mainPanel.add(button1);
    mainPanel.add(button2);
    mainPanel.add(button3);
    mainPanel.add(button4);
    mainPanel.add(userMove);
    mainPanel.add(aiMove);
    mainPanel.add(moveFirst);
    mainPanel.setBackground(Color.GRAY);

    frame.add(mainPanel);
    frame.setVisible(true);

}

//returns user's pokemon
public Pokemon getUserPokemon(){
    return user;
}

//returns computer's pokemon
public Pokemon getAIPokemon(){
    return computer;
}

//chooses a random pokemon for the computer
private Pokemon aiRandom(){
    int s = (int)(Math.random() * 6 );

    if(s == 0)
        return new Kyogre();
    if(s == 1)
        return new Groudon();
    if(s == 2)
        return new Mewtwo();
    if(s == 3)
        return new Arceus();
    if(s == 4)
        return new Pikachu();
    if(s == 5)
        return new Snorlax();
    return null;
}

//generates a random number so we can choose an ai move
private int aiMove(){
    int i = (int)(Math.random() * 4);
    return i;
}

//creates pop-up window if somebody's health drops to 0
private void gameEnd(int i){
    String rt;
    if(i == 0)
        rt = "Congratulations! You won! " + computer.getClass().getName() + " fainted.                                       Would you like to play again?";
    else if(i == 1)
        rt = "Sorry. You lost to the computer. " + user.getClass().getName() + " fainted.  Would you like to play again?";
    else 
        rt = "You and the computer tied. Would you like to play again?";

    JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
    p.add(user.leftSide());
    p.add(computer.rightSide());

    JLabel  j1 = new JLabel(rt);
    ButtonGroup groupD = new ButtonGroup();
    final JRadioButton y = new JRadioButton("Yes"); //allows us to run program with f already selected
    final JRadioButton n = new JRadioButton("No", true);
    groupD.add(y);
    groupD.add(n);
    JButton button = new JButton("Select");
    class ConvertListener implements ActionListener{
        public void actionPerformed(ActionEvent event){
            SelectionScreen s = new SelectionScreen();
            if(y.isSelected()){
                s.execute();
                frame.setVisible(false);
                frame.dispose();
            }
            else if(n.isSelected()){
                frame.setVisible(false);
                frame.dispose();
            }
        }
    }
    ActionListener listener = new ConvertListener();
    button.addActionListener(listener);
    p.add(j1);
    p.add(y);
    p.add(n);
    p.add(button);

    frame.add(p);
    frame.setVisible(true);
}

//runs and updates health bar
private String healthBar(Pokemon x){
    String rt = "";
    for(int i = 0; i < x.getHealth(); i +=10 ){
        rt += "|";
    }
    return rt;
}

private void gamePlay(int us){

    String h1 = "";
    String h2 = "";
    int c;
    do{
        c = aiMove();
    }while(computer.accessMoves(c).getPP() <= 0);


    if(user.accessMoves(us).getPP() <= 0 ){
         JOptionPane.showMessageDialog(frame, "The move you selected has no PP left. Select another.");
         return;
    }
    long lastTime = System.currentTimeMillis();           
    if(user.getSpeed() >= computer.getSpeed()){  //if user's speed is quicker, computer gets attacked first
           computer.updateHealth(user, us);
           userMove.setText("You used: " + user.accessMoves(us).getName());
           user.accessMoves(us).lowerPP(); 
           h2 = healthBar(computer);                       //health bar is changed

           int usCount = 0;
           int compCount= 0;



           while(usCount < user.leftNumFramesMove(us)){
               long thisTime = System.currentTimeMillis();
               if ((thisTime - lastTime) >= PERIOD) {
               lastTime = thisTime;
             //  backS.remove(left);
             //  backS.remove(right);
               if(us == 0)
                   left = user.LeftSideMove0(usCount + 1);
               else if(us == 1)
                   left = user.LeftSideMove1(usCount + 1);
               else if(us == 2)
                   left = user.LeftSideMove2(usCount + 1);
               else
                   left = user.LeftSideMove3(usCount + 1);
               //backS.add(left);
              // backS.add(right);
               backS.revalidate();

               usCount += 1;
             }
             usCount += 1;
           }
           while(compCount < user.rightNumFramesMove(us)){
                 long thisTime = System.currentTimeMillis();
                if ((thisTime - lastTime) >= PERIOD) {
                lastTime = thisTime;
               if(us == 0)
                  left = user.LeftSideMove0(usCount);
               else if(us == 1)
                   left = user.LeftSideMove1(usCount);
               else if(us == 2)
                   left = user.LeftSideMove2(usCount);
               else
                   left = user.LeftSideMove3(usCount);

               if(us == 0)
                  right = user.RightSideMove0(compCount + 1, computer);
               else if(us == 1)
                  right = user.RightSideMove1(compCount + 1, computer);
               else if(us == 2)
                  right = user.RightSideMove2(compCount + 1, computer);
               else
                  right = user.RightSideMove3(compCount, computer);
              // backS.add(left);
               //backS.add(right);
               backS.revalidate();



               compCount += 1;
             }
           }

           pch.setText("Computer's HP: " + h2+ computer.getHealth());
           moveFirst.setText("You attacked first.");

           if(computer.getHealth() == 0){                  //if computer dies, user wins
               return;
            }

        /* backS.remove(left);
         backS.remove(right);
         left = new ImageComponent(user.getName() + "Left.jpg");
         right = new ImageComponent(computer.getName() + "Right.jpg");
         backS.add(left);
         backS.add(right);
         backS.repaint();*/
           user.updateHealth(computer, aiMove());
           aiMove.setText("Computer used: " + computer.accessMoves(c).getName() + "                       "); 
           computer.accessMoves(c).lowerPP();
           h1= healthBar(user);
           hu.setText("Your HP: " + h1 + user.getHealth() +"      ");
           if(user.getHealth() == 0){
               return;
            }
      }
      else {
         user.updateHealth(computer, aiMove());
         aiMove.setText("Computer used: " + computer.accessMoves(c).getName() + "                       "); 
         computer.accessMoves(c).lowerPP();
         h1= healthBar(user);
         hu.setText("Your HP: " + h1 + user.getHealth() +"      ");
         moveFirst.setText("Computer attacked first.");

         computer.updateHealth(user, us);
         userMove.setText("You used: " + user.accessMoves(us).getName());
         user.accessMoves(us).lowerPP();
         h2 = healthBar(computer);

        //creates pause between player move and computer move
           int usCount = 0;
           int compCount= 0;
           while(usCount < user.leftNumFramesMove(us)){
                 long thisTime = System.currentTimeMillis();
                if ((thisTime - lastTime) >= PERIOD) {
                lastTime = thisTime;
              // backS.remove(left);
               //backS.remove(right);
               if(us == 0)
                  left = user.LeftSideMove0(usCount + 1);
               else if(us == 1)
                   left = user.LeftSideMove1(usCount + 1);
               else if(us == 2)
                   left = user.LeftSideMove2(usCount + 1);
               else if(us == 3)
                   left = user.LeftSideMove3(usCount + 1);
               right = computer.rightSide();
             //  backS.add(left);
             //  backS.add(right);
               frame.revalidate();
               frame.repaint();
               backS.revalidate();
               backS.repaint();


               usCount += 1;
           }
           }
           usCount += 1;
           while(compCount < user.rightNumFramesMove(us)){
                 long thisTime = System.currentTimeMillis();
                if ((thisTime - lastTime) >= PERIOD) {
                lastTime = thisTime;
              // backS.remove(left);
             //  backS.remove(right);
                if(us == 0)
                  left = user.LeftSideMove0(usCount);
               else if(us == 1)
                  left = user.LeftSideMove1(usCount);
               else if(us == 2)
                  left = user.LeftSideMove2(usCount);
               else if(us == 3)
                  left = user.LeftSideMove3(usCount);

               if(us == 0)
                  right = user.RightSideMove0(compCount + 1, computer);
               else if(us == 1)
                  right = user.RightSideMove1(compCount + 1, computer);
               else if(us == 2)
                  right = user.RightSideMove2(compCount + 1, computer);
               else if(us == 3)
                  right = user.RightSideMove3(compCount + 1, computer);
              // backS.add(left);
              // backS.add(right);
               frame.revalidate();
               frame.repaint();
               backS.revalidate();
               backS.repaint();
               ;
               compCount += 1;
           }
          }


         pch.setText("Computer's HP: " + h2+ computer.getHealth());
         if(computer.getHealth() == 0){ 
               return;
         }
        /*backS.remove(left);
         backS.remove(right);
         left = new ImageComponent(user.getClass().getName() + "Left.jpg");
         right = new ImageComponent(computer.getClass().getName() + "Right.jpg");
         backS.add(left);
         backS.add(right);
         backS.repaint(); */
       }
}

}
Peter
  • 3
  • 2
  • 1
    1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). 2) For many components in one space, use a [`CardLayout`](http://docs.oracle.com/javase/7/docs/api/java/awt/CardLayout.html) as seen in this [short example](http://stackoverflow.com/a/5786005/418556). – Andrew Thompson Jun 17 '14 at 04:49

1 Answers1

1

Basically, you're block the Event Dispatching Thread, preventing it from updating the screen.

Until the gamePlay method exists (and the actionPerformed method exists), nothing will be painted to the screen.

Take a look at Concurrency in Swing for more details.

Generally, you should consider using a javax.swing.Timer to perform basic animation in Swing. You can use a Thread, but it just complicates the matter

Have a look at How to use Swing Timers for more details

Also, remember, Swing is not thread safe. If you need to update the UI, you must do it from within the context of the Event Dispatching Thread

Updated...

Okay, so this is really, really complicated.

Essentially, a javax.swing.Timer waits outside of the EDT for a specified period of time and then triggers an update in such away that the registered ActionListeners are notified within the context of the EDT.

What this means is, when you start a Timer, the code will continue running while the Timer waits...

For example...

Timer timer = new Timer(PERIOD, new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
        // Executed later...
    }
});
timer.start();
// Not waiting, continue execution...

This means, based on you code, you will either need to devise some kind of "execute after" chain, or setup your animation better, for example...

private void gamePlay(int us) {

    String h1 = "";
    String h2 = "";
    int c;
    do {
        c = aiMove();
    } while (computer.accessMoves(c).getPP() <= 0);

    if (user.accessMoves(us).getPP() <= 0) {
        JOptionPane.showMessageDialog(frame, "The move you selected has no PP left. Select another.");
        return;
    }
    long lastTime = System.currentTimeMillis();
    if (user.getSpeed() >= computer.getSpeed()) {  //if user's speed is quicker, computer gets attacked first
        computer.updateHealth(user, us);
        userMove.setText("You used: " + user.accessMoves(us).getName());
        user.accessMoves(us).lowerPP();
        h2 = healthBar(computer);                       //health bar is changed

        //......
        MoveAnimationHandler handler = new MoveAnimationHandler(this, us, user, computer, new Runnable() {
            @Override
            public void run() {
                pch.setText("Computer's HP: " + h2 + computer.getHealth());
                moveFirst.setText("You attacked first.");

                if (computer.getHealth() == 0) {                  //if computer dies, user wins
                    return;
                }

                /* backS.remove(left);
                 backS.remove(right);
                 left = new ImageComponent(user.getName() + "Left.jpg");
                 right = new ImageComponent(computer.getName() + "Right.jpg");
                 backS.add(left);
                 backS.add(right);
                 backS.repaint();*/
                user.updateHealth(computer, aiMove());
                aiMove.setText("Computer used: " + computer.accessMoves(c).getName() + "                       ");
                computer.accessMoves(c).lowerPP();
                h1 = healthBar(user);
                hu.setText("Your HP: " + h1 + user.getHealth() + "      ");
                if (user.getHealth() == 0) {
                    return;
                }
            }
        });

        Timer timer = new Timer(40, handler);
        timer.start();

And the MoveAnimationHandler....

public class MoveAnimationHandler implements ActionListener {

    private int usCount = 0;
    private int compCount = 0;
    private Pokemon user;
    private Pokemon computer;

    private int left;
    private int right;

    private int us;
    private JComponent parent;

    private Runnable whenDone;

    public MoveAnimationHandler(JComponent parent, int us, Pokemon user, Pokemon computer, Runnable whenDone) {
        this.parent = parent;
        this.user = user;
        this.computer = computer;
        left = user.leftSide();
        right = computer.rightSide();
        this.us = us;
        this.whenDone = whenDone;
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if (usCount < user.leftNumFramesMove(us) && compCount < user.rightNumFramesMove(us)) {

            if (usCount < user.leftNumFramesMove(us)) {
                if (us == 0) {
                    left = user.LeftSideMove0(usCount + 1);
                } else if (us == 1) {
                    left = user.LeftSideMove1(usCount + 1);
                } else if (us == 2) {
                    left = user.LeftSideMove2(usCount + 1);
                } else {
                    left = user.LeftSideMove3(usCount + 1);
                }
                usCount += 1;
            }

            if (compCount < user.rightNumFramesMove(us)) {
                if (us == 0) {
                    left = user.LeftSideMove0(usCount);
                } else if (us == 1) {
                    left = user.LeftSideMove1(usCount);
                } else if (us == 2) {
                    left = user.LeftSideMove2(usCount);
                } else {
                    left = user.LeftSideMove3(usCount);
                }

                if (us == 0) {
                    right = user.RightSideMove0(compCount + 1, computer);
                } else if (us == 1) {
                    right = user.RightSideMove1(compCount + 1, computer);
                } else if (us == 2) {
                    right = user.RightSideMove2(compCount + 1, computer);
                } else {
                    right = user.RightSideMove3(compCount, computer);
                }
                compCount += 1;
            }

            parent.repaint();

        } else {

            ((Timer) e.getSource()).stop();
            whenDone.run();

        }
    }
}

Now, it get worse, because your ActionListeners are reliant on the output, so you need some way to chain those calls...

class b1Listener implements ActionListener {
    public void actionPerformed(ActionEvent event) {
        gamePlay(0, new Runnable() {
            @Override
            public void run() {
                if (computer.getHealth() <= 0) {
                    frame.remove(mainPanel);
                    frame.setSize(1050, 500);
                    gameEnd(0, null);
                } else if (user.getHealth() <= 0) {
                    frame.remove(mainPanel);
                    frame.setSize(1050, 500);
                    gameEnd(1, null);
                }
            }
        });
    }
}

private void gamePlay(int us, final Runnable doAfter) {
    //...
    if (user.getSpeed() >= computer.getSpeed()) {  //if user's speed is quicker, computer gets attacked first
        //...
        MoveAnimationHandler handler = new MoveAnimationHandler(this, us, user, computer, new Runnable() {
            @Override
            public void run() {
                //...
                doAfter.run();
            }
        });

        Timer timer = new Timer(40, handler);

    } else {
        //...
    }
}

This is what I meant by "as you have the executes AFTER the animation is suppose to have completed"

And around about the start of the post I would seriously reconsider the entire design...

You should take a look at the Model-View-Controller pattern and Observer Pattern

This would allow you to separate the various elements of the game into isolated areas of responsibility, while providing much needed notification about updates to the various states in order to keep them in sync...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thanks, I have been trying to figure out how to use a swing timer, but I have had no success. I am currently finishing one year of AP Comp Sci in high school so I do not understand very much. Is there any way you could help explain what I should be doing to make a swing timer work for my code? – Peter Jun 17 '14 at 04:08
  • Not unless you can provid a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem, as too much code is missing – MadProgrammer Jun 17 '14 at 04:10
  • The problem is that there is about 15 .java files in the project. Should I show all the files included? – Peter Jun 17 '14 at 04:13
  • The larger problem is trying to determine when the animation has stopped and what should be done at the end of it, as you have the executes AFTER the animation is suppose to have completed – MadProgrammer Jun 17 '14 at 04:41
  • I'm sorry, I'm not sure what you mean by executes after the animation. Is it the code after the animation to set hp? – Peter Jun 17 '14 at 04:45
  • See updates as to why you've dug yourself into a hole – MadProgrammer Jun 17 '14 at 05:09
  • Hmm thanks, I didn't write this code and I myself don't understand it to well to fix it before our project is due (Thursday) but thanks for your help and I'll try to make the animation at least work! – Peter Jun 17 '14 at 05:25
  • @gakiloroth Swing LayoutManager hasn't notifier that automatically switching betweens views, you have to call revalidate() & repaint() or to use CardLayout directly – mKorbel Jun 17 '14 at 05:27
  • @mKorbel I've tried using revalidate and repaint but it didn't work. – Peter Jun 17 '14 at 05:28
  • @MadProgrammer IF i try to implement everything you showed me to do, without radically rewriting the code like you suggested, should the animation work? Either way, thank you for all your help! – Peter Jun 17 '14 at 05:32
  • It might, but it might also stop your programming from running in they way you think it should... – MadProgrammer Jun 17 '14 at 05:32
  • Hm, either way thank you for all your help! – Peter Jun 17 '14 at 05:37
  • I'm a little confused on where all this code should go, though.. – Peter Jun 17 '14 at 05:58
  • I'm not surprised, you're trying to retrofit a fix into code which can't support it. – MadProgrammer Jun 17 '14 at 06:07
  • I also get an error from this.whendone; – Peter Jun 17 '14 at 06:10
  • Sorry, should be `this.whenDone = whenDone;` – MadProgrammer Jun 17 '14 at 06:12
  • Thanks, I'm sorry that the code is so hard to fix and I really appreciate you helping me try to. – Peter Jun 17 '14 at 06:12
  • I also get an error from " MoveAnimationHandler handler = new MoveAnimationHandler(this, us, user, computer, new Runnable() ", It says the problem is with "this" - Background cannot be converted to JComponent – Peter Jun 17 '14 at 06:37
  • Try using `(JComponent)frame.getContentPane()` – MadProgrammer Jun 17 '14 at 06:38
  • Is the gameplay(int us, final Runnable doAfter ) a second method i make? – Peter Jun 17 '14 at 06:43
  • Yep. You to need control the order in which notifications occur (about when something stops animating) – MadProgrammer Jun 17 '14 at 06:44
  • Hm... I plugged everything In and it didn't work. I had to change some of the code around and make a final int version of c and us, while moving some of the code into the void run(); since they needed to be final but they had to change. – Peter Jun 17 '14 at 07:17
  • I believe the issue is that we need the first if (usCount < user.leftNumFramesMove(us)) part to run until usCount is = to user.leftNumFramesMove(us)) and then move onto the next part. Also, I'm having problems with ending the game after having to move the code – Peter Jun 17 '14 at 07:25
  • I thought I passed all that information into the `MoveAnimationHandler`, but then again, I didn't have access to runnable code – MadProgrammer Jun 17 '14 at 07:26
  • I think that the problem is that the if statement doesnt stop after the first one works, it goes to the second one. Would a return; make it stop after one set of if statements work? Also, I had to move some code around since they requested it be final and I couldn't make them so because they had to change. – Peter Jun 17 '14 at 07:29
  • It looks like the two movement steps rely on each other, this means that the provided code is wrong and you would need yet another chained element to move from the left animation to the right animation... – MadProgrammer Jun 17 '14 at 07:33
  • Hm. I will try to fix that myself later, but do you have a solution for the problem I had with thing being requested as finals? us and c were no problem, but when I had to move the other code into void run() the game no longer ended when hp reached 0 – Peter Jun 17 '14 at 07:37
  • I could send you the entire project to make communication easier if you still have time to help. – Peter Jun 17 '14 at 07:41
  • I'm not sure how to pm you, should I just upload it and post a link – Peter Jun 17 '14 at 07:52
  • http://www.speedyshare.com/PkX2D/PokemonGame.zip – Peter Jun 17 '14 at 08:17
  • So I've had a look and I'm posting my updates. There is a god aweful amount of chaining going on so it's likely not to make much sense...or less then it did before. Basically I set up a `GameState` class which simply holds key/value pairs of information which allows you to pass information between the various links of logic...it's a mess...https://dl.dropboxusercontent.com/u/8411344/PokemonGame.zip – MadProgrammer Jun 17 '14 at 12:35
  • (This is Richard)I am part of this person's group and downloaded the file. I first off want to thank you for putting the work you did into fixing our code. However, when the code is run the animations still don't work. We also saw that the button listeners were commented out after the first one, but we need all four listeners for the buttons. We were wondering if there is any way to do the animations without all the classes you included because as high school students, we really don't understand what's going on and how to fix any problems. Thanks. – Peter Jun 17 '14 at 20:01
  • I only implemented one ActionListern, I'm sure you can figure out the rest. I'm not sure what the animation is use pose to be doing, but when I run it, it tends to switch the panel positions. As near as I can tell, it does what the original code intended, as to whether that's what it should be doing, is another question... – MadProgrammer Jun 17 '14 at 20:32
  • Is there away to do the animation with out all the linkage, yes, but you'd need to change your entire program as it is structurally incompatible – MadProgrammer Jun 17 '14 at 20:35
  • We wanted the animations to show a fight scene. So if we select a move the screen on the left changes from one frame to the next, and then the right frame is supposed to change frame by frame after the left is finished doing its thing. So if I were to for example throw a ball, the ball would fly from the left to right in stop motion. Some moves required the left to be a certain picture after updating, and our old code tried to account for that – Peter Jun 17 '14 at 20:45
  • I only copied the code over to a chained execution to allow for the animation to work as it was coded, it was hard enough trying to get that to work :P – MadProgrammer Jun 17 '14 at 20:54
  • I updated the source, which should produce a result "closer" to what you are expecting. The zip file (https://dl.dropboxusercontent.com/u/8411344/PokemonSource.zip) contains the source files only. It occurs to me that I might be able to use a `SwingWorker` instead of all the "link" classes, but I've got quite a bit of work to get through today so it might be some time before I get to look at it – MadProgrammer Jun 17 '14 at 22:13
  • Okay, so I've scrambled back over the code and implemented a slightly less complicated solution using a `SwingWoker` instead of a bunch of chainable links. Your code is so tightly coupled it's difficult to make this work any cleaner. https://dl.dropboxusercontent.com/u/8411344/PokemonSource02.zip – MadProgrammer Jun 18 '14 at 04:12
  • Thank you so much, this is what we meant to do. The one last thing I'm trying to fix is to make it go back to the initial screen with no pokemon moves or anything once the move finishes animating. Where should I add this? – Peter Jun 18 '14 at 06:13
  • It's probably easier to add it to the end of the `actionPerformed` method, assuming that neither player has lost (add one last `else` to the `if-else` statements) – MadProgrammer Jun 18 '14 at 06:15
  • What should be added to the else statement as the revalidate repaint doesnt work? I couldn't use game state like in the FasterAnimation classes – Peter Jun 18 '14 at 06:40
  • I guess you need to determine the "default" frames for the left and right views, remove the current content, add these "default" frames (in the correct order) and `revaldiate` the view – MadProgrammer Jun 18 '14 at 06:42
  • What i used was left = user.leftSide(); right = computer.rightSide(); backS.removeAll(); backS.add(left, 0); backS.add(right, 1); backS.revalidate(); backS.repaint(); – Peter Jun 18 '14 at 06:47
  • Oh, I'm sorry, I put them in only one of the buttons and tested the wrong button. Yes, this does work, thank you so much for helping me and my group implement animation. When I get more time I'll be sure to try and understand exactly what we did wrong and how you managed to fix it. Thank you! – Peter Jun 18 '14 at 06:50