I think I might just be doing something wrong so I decided to come here. I'm trying to write an "Aim Trainer" of sorts and there are no issues with it until one part.
In this part it checks if a button has been pressed. If it has, then it will remove the button from the JPanel and add some other stuff.
if(e.getSource() == startSixty) {
milliseconds = System.currentTimeMillis();
this.remove(startButton);
this.remove(beginning);
this.remove(startText);
this.remove(startSixty);
this.add(hitButton);
validate();
repaint();
endCheck();
After this I have a while loop (simplified slightly for viewing pleasure) which is checking to see if 10 seconds has passed, once ten seconds passes, the code displays scores of how you did and stuff.
public void endCheck() {
while (((System.currentTimeMillis() - milliseconds) / 1000) != 10) {
System.out.println("1");
if ((int) ((System.currentTimeMillis() - milliseconds) / 1000) == 10) {
this.remove(hitButton);
this.add(finishedHitCount);
this.add(finishedMissCount);
this.add(hitMissRatio);
this.add(hitTimeRatio);
validate();
repaint();
}
}
}
The while loop is working fine, the issue I'm having is that the code before it that removes stuff isn't working, almost like it's skipping that snippet of code.
Full Code (Might not be necessary but if you want it):
package TrainingButtons;
import java.awt.Color;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.DecimalFormat;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
public class GameFrame extends JFrame implements ActionListener, MouseListener{
JButton hitButton;
JButton endButton;
JButton startButton;
JButton startSixty;
Random random;
JLabel finishedHitCount;
JLabel finishedMissCount;
JLabel hitMissRatio;
JLabel totalTime;
JLabel hitTimeRatio;
JLabel beginning;
JLabel startText;
double hits;
double misses;
double milliseconds;
double endTime;
boolean found = true;
Main main;
GameFrame() {
random = new Random();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(Toolkit.getDefaultToolkit().getScreenSize());
this.setLayout(null);
this.setResizable(true);
addMouseListener(this);
//startText Design
startText = new JLabel("Quinn's Aim Trainer", SwingConstants.CENTER);
startText.setSize(getWidth(), 100);
startText.setFont(new Font("Apple Chancery", Font.BOLD, 75));
startText.setLocation(0, 75);
//beginning Design
beginning = new JLabel("Press Start To Begin", SwingConstants.CENTER);
beginning.setSize(getWidth(), 100);
beginning.setFont(new Font("Copperplate", Font.BOLD, 75));
beginning.setLocation(0, 225);
//startSixty Design
startSixty = new JButton();
startSixty.setSize(350, 100);
startSixty.setText("60 Second Round?");
startSixty.setFont(new Font("Copperplate", Font.BOLD, 30));
startSixty.setLocation((getWidth() - 650), (getHeight() / 2 - (startSixty.getHeight() / 2)));
startSixty.addActionListener(this);
startSixty.setOpaque(true);
startSixty.setBorderPainted(false);
startSixty.setBackground(Color.GREEN);
//hitButton Design
startButton = new JButton();
startButton.setSize(350,100);
startButton.setText("Infinite Round?");
startButton.setFont(new Font("Copperplate", Font.BOLD, 30));
startButton.setLocation(300, (getHeight() / 2 - (startButton.getHeight() / 2)));
startButton.addActionListener(this);
startButton.setOpaque(true);
startButton.setBorderPainted(false);
startButton.setBackground(Color.GREEN);
//hitButton Design
hitButton = new JButton();
hitButton.setSize(25,25);
hitButton.setLocation((getWidth() / 2 - (hitButton.getWidth() /2)), (getHeight() / 2 - (hitButton.getHeight() / 2)));
hitButton.addActionListener(this);
hitButton.setOpaque(true);
hitButton.setBorderPainted(false);
hitButton.setBackground(Color.GREEN);
//endButton Design
endButton = new JButton();
endButton.setSize(100, 50);
endButton.setText("END");
endButton.setLocation(((getWidth() /2) - 50), 0);
endButton.addActionListener(this);
endButton.setOpaque(true);
endButton.setBorderPainted(false);
endButton.setBackground(Color.RED);
this.add(startText);
this.add(startButton);
this.add(startSixty);
this.add(beginning);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == hitButton) {
hitButton.setLocation(random.nextInt(this.getWidth()) - 13, random.nextInt(this.getHeight()) - 13);
hits++;
} else if(e.getSource() == startButton) {
milliseconds = System.currentTimeMillis();
this.remove(startButton);
this.remove(beginning);
this.remove(startText);
this.remove(startSixty);
this.add(hitButton);
this.add(endButton);
validate();
repaint();
} else if(e.getSource() == endButton) {
//endTime = (milliseconds/1000);
endTime = (System.currentTimeMillis() - milliseconds) / 1000;
//finishedHitCount Design
finishedHitCount = new JLabel("Total Hits: " + (int) hits, SwingConstants.CENTER);
finishedHitCount.setSize(getWidth(), 100);
finishedHitCount.setFont(new Font("Copperplate", Font.BOLD, 75));
finishedHitCount.setLocation(0, 25);
//finishedMissCount Design
finishedMissCount = new JLabel("Total Misses: " + (int) misses, SwingConstants.CENTER);
finishedMissCount.setSize(getWidth(), 100);
finishedMissCount.setFont(new Font("Copperplate", Font.BOLD, 75));
finishedMissCount.setLocation(0, 150);
//hitMissRatio Design
DecimalFormat format = new DecimalFormat("0.00");
if (misses == 0) {
hitMissRatio = new JLabel("Hit Miss Ratio: " + (int) hits, SwingConstants.CENTER);
} else {
String formatted = format.format(hits/misses);
hitMissRatio = new JLabel("Hit Miss Ratio: " + formatted, SwingConstants.CENTER);
}
hitMissRatio.setSize(getWidth(), 100);
hitMissRatio.setFont(new Font("Copperplate", Font.BOLD, 75));
hitMissRatio.setLocation(0, 275);
//totalTime Design
totalTime = new JLabel("Total Time: " + endTime + " Seconds", SwingConstants.CENTER);
totalTime.setSize(getWidth(), 100);
totalTime.setFont(new Font("Copperplate", Font.BOLD, 75));
totalTime.setLocation(0, 400);
//hitTimeRatio Design
String formatted2 = format.format(hits/endTime);
hitTimeRatio = new JLabel("Hits Per Second: " + formatted2, SwingConstants.CENTER);
hitTimeRatio.setSize(getWidth(), 100);
hitTimeRatio.setFont(new Font("Copperplate", Font.BOLD, 75));
hitTimeRatio.setLocation(0, 525);
this.remove(hitButton);
this.remove(endButton);
this.add(finishedHitCount);
this.add(finishedMissCount);
this.add(hitMissRatio);
this.add(totalTime);
this.add(hitTimeRatio);
validate();
repaint();
} else if(e.getSource() == startSixty) {
System.out.println("3");
milliseconds = System.currentTimeMillis();
this.remove(startButton);
this.remove(beginning);
this.remove(startText);
this.remove(startSixty);
this.add(hitButton);
validate();
repaint();
System.out.println("4");
endCheck();
}
}
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
misses++;
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void endCheck() {
//do {
//for (int i = 0; i != 1;) {
while (((System.currentTimeMillis() - milliseconds) / 1000) != 10) {
System.out.println("1");
if ((int) ((System.currentTimeMillis() - milliseconds) / 1000) == 10) {
//finishedHitCount Design
finishedHitCount = new JLabel("Total Hits: " + (int) hits, SwingConstants.CENTER);
finishedHitCount.setSize(getWidth(), 100);
finishedHitCount.setFont(new Font("Copperplate", Font.BOLD, 75));
finishedHitCount.setLocation(0, 25);
//finishedMissCount Design
finishedMissCount = new JLabel("Total Misses: " + (int) misses, SwingConstants.CENTER);
finishedMissCount.setSize(getWidth(), 100);
finishedMissCount.setFont(new Font("Copperplate", Font.BOLD, 75));
finishedMissCount.setLocation(0, 150);
//hitMissRatio Design
DecimalFormat format = new DecimalFormat("0.00");
if (misses == 0) {
hitMissRatio = new JLabel("Hit Miss Ratio: " + (int) hits, SwingConstants.CENTER);
} else {
String formatted = format.format(hits/misses);
hitMissRatio = new JLabel("Hit Miss Ratio: " + formatted, SwingConstants.CENTER);
}
hitMissRatio.setSize(getWidth(), 100);
hitMissRatio.setFont(new Font("Copperplate", Font.BOLD, 75));
hitMissRatio.setLocation(0, 275);
//hitTimeRatio Design
String formatted2 = format.format(hits/endTime);
hitTimeRatio = new JLabel("Hits Per Second: " + formatted2, SwingConstants.CENTER);
hitTimeRatio.setSize(getWidth(), 100);
hitTimeRatio.setFont(new Font("Copperplate", Font.BOLD, 75));
hitTimeRatio.setLocation(0, 525);
this.remove(hitButton);
this.add(finishedHitCount);
this.add(finishedMissCount);
this.add(hitMissRatio);
this.add(hitTimeRatio);
validate();
repaint();
//i = 1;
}
System.out.println("2");
}
//}
//}while (!found);
}
}