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(); */
}
}
}