I'm having trouble with the GUI updating after I run the applet. I've tried different methods such as repaint() and revalidate() and am not sure if I'm doing it wrong or I'm missing something. Currently I have working to the point where the applet gets "painted" only once.
This applet is a math quiz game in which there is a math questions and the player has to answer it. If they get it right they gain 50 points but if they get it wrong they lose 100 points.
Since I can't get the questions or score to update on the applet itself, I just have them printed on to the console. You still have to type the answer in the TextField of the applet though.
Keep in mind that I am a novice programmer so the code may be messy. Thanks in advance!
This applet is a math quiz game in which there is a math questions and the player has to answer it. If they get it right they gain 50 points but if they get it wrong they lose 100 points.
Since I can't get the questions or score to update on the applet itself, I just have them printed on to the console. You still have to type the answer in the TextField of the applet though.
Keep in mind that I am a novice programmer so the code may be messy. Thanks in advance!
import java.applet.Applet; import java.awt.Button; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.util.Random; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JOptionPane; import javax.swing.JTextArea; /* * A simple math quiz that lets you test your math skills. */ public class NewIdeaMath extends Applet { int elo = 100; int input; String question = " "; Color black = new Color(255, 255, 255); Color white = new Color(0, 0, 0); int x = 0, y = 0, z = 0; int ans = 0; boolean ready = true; Font f = new Font("Dialog", Font.PLAIN, 50); Font g = new Font("Dialog", Font.PLAIN, 30); protected TextField userInput; protected TextField scoreField; protected TextField questionField; protected JTextArea textArea; //Creating stuff public void init () { ask(); } public void ask() { getELO(); repaint(); System.out.println(question); userInput = new TextField(50); scoreField = new TextField(9); questionField = new TextField(20); scoreField.setEditable(true); questionField.setEditable(true); scoreField.setFont(g); questionField.setFont(f); Button b = new Button("Check Answer"); this.add(questionField); this.add(scoreField); this.add(userInput); this.add(B)/>/>; questionField.setText(question); scoreField.setText("Points: "+elo); AreTheyReady atr = new AreTheyReady(userInput, elo, ans, ready); b.addActionListener(atr); input = atr.parsedAnswer; //checkAnswer(); ready = false; elo = atr.elo; } public void paint(Graphics p) { setSize(1000, 200); p.fillRect(0, 0, 1000, 800); p.setFont(f); p.setColor(Color.gray); //Sets the size of the canvas. p.drawLine(0, 700, 1000, 700); p.drawString("You have "+" points.", 25, 780); //Drawing Scoreboard p.setColor(Color.white); p.drawString(question, 200, 400); } public void getELO() { //---------0 - 950 ELO-------------------------------------------------------------------- if (elo <= 0) { System.out.println("You lose."); System.exit(0); } if (elo == 50) { int n [] = {1,2,3,4,5,6,7,8,9}; Random random = new Random(); x = (n[random.nextInt(n.length)]); y = (n[random.nextInt(n.length)]); question = "What is " + x + " + 0?"; ans = x; } if (elo == 100 || elo == 150) { int n [] = {1,2}; Random random = new Random(); x = (n[random.nextInt(n.length)]); y = (n[random.nextInt(n.length)]); question = "What is " + x + " + " + y + "?"; ans = x + y; } if (elo == 200 || elo == 250) { int n [] = {1,2,3,4,5,6}; Random random = new Random(); x = (n[random.nextInt(n.length)]); y = (n[random.nextInt(n.length)]); question = "What is " + x + " + " + y + "?"; ans = x + y; } if (elo == 300 || elo == 350) { int n [] = {1,2,3}; Random random = new Random(); x = (n[random.nextInt(n.length)]); y = (n[random.nextInt(n.length)]); question = "What is " + x + " - " + y + "?"; ans = x - y; } if (elo == 400 || elo == 450) { int n [] = {1,2,3,4,-5,-6,-7}; Random random = new Random(); x = (n[random.nextInt(n.length)]); y = (n[random.nextInt(n.length)]); question = "What is " + x + " + " + y + "?"; ans = x + y; } if (elo == 500 || elo == 550) { int n [] = {1,2,3,4,5,6,7,8,9}; Random random = new Random(); x = (n[random.nextInt(n.length)]); y = (n[random.nextInt(n.length)]); question = "What is sum of " + x + " and " + y + " multiplied by 2?"; ans = (x + y) * 2; } if (elo == 600 || elo == 650) { int n [] = {1,2,3,4,5,6,7,8,9}; Random random = new Random(); x = (n[random.nextInt(n.length)]); y = (n[random.nextInt(n.length)]); question = "What is the sum of " + x + " and " + y + " multiplied by 3?"; ans = (x + y) * 3; } if (elo == 700 || elo == 750) { int n [] = {1,2,3,4,5,6,7,8,9}; Random random = new Random(); x = (n[random.nextInt(n.length)]); y = (n[random.nextInt(n.length)]); question = "What is 2 * " + x + " + " + y + "?\n Remember order of operations!"; ans = x + x + y; } if (elo == 800 || elo == 850) { int n [] = {1,2,3,4,5,6,7,8,9}; Random random = new Random(); x = (n[random.nextInt(n.length)]); y = (n[random.nextInt(n.length)]); question = "What is 2 * " + x + " + 2 * " + y + "?\n Remember order of operations!"; ans = x + x + y + y; } if (elo == 900 || elo == 950) { int n [] = {1,2,3,4,5,6,7,8,9}; Random random = new Random(); x = (n[random.nextInt(n.length)]); y = (n[random.nextInt(n.length)]); z = (n[random.nextInt(n.length)]); question = "What is " + x + " + " + y + " + " + z + "?"; ans = x + y + z; } //---1000+ ELO-------------------------------------------------------------------------- if (elo == 1000 || elo == 1050) { int n [] = {1,2,3,4,5,6,7,8,9}; Random random = new Random(); x = (n[random.nextInt(n.length)]); y = (n[random.nextInt(n.length)]); z = (n[random.nextInt(n.length)]); question = "What is (" + x + " - " + y + " - " + z + ") * 3?"; ans = (x - y - z) * 3; } if (elo == 1100 || elo == 1150) { int n [] = {1,2,3,4,5,6,7,8,9}; Random random = new Random(); x = (n[random.nextInt(n.length)]); y = (n[random.nextInt(n.length)]); z = (n[random.nextInt(n.length)]); question = "What is (" + x + " * " + y + ") + (" + y + " * " + z + ")?"; ans = (x * y) + (y * z); } if (elo == 1200 || elo == 1250) { int n [] = {1,2,3,4,5,6,7,8,9}; Random random = new Random(); x = (n[random.nextInt(n.length)]); y = (n[random.nextInt(n.length)]); z = (n[random.nextInt(n.length)]); question = "What is (" + x + " * " + y + ") - (" + y + " * " + z + ")?"; ans = (x * y) - (y * z); } if (elo == 1300 || elo == 1350) { int n [] = {11,16,21,22,33,44,46,54,55,66,77,88,99}; Random random = new Random(); x = (n[random.nextInt(n.length)]); y = (n[random.nextInt(n.length)]); z = (n[random.nextInt(n.length)]); question = "What is " + x + " + " + y + "?"; ans = x + y; } if (elo == 1400 || elo == 1450) { int n [] = {13,16,21,22,33,44,46,54,55,66,77,88,99}; Random random = new Random(); x = (n[random.nextInt(n.length)]); y = (n[random.nextInt(n.length)]); z = (n[random.nextInt(n.length)]); question = "What is " + x + " - " + y + "?"; ans = x - y; } if (elo == 1500 || elo == 1550) { int n [] = {3,5,7,9,11,20,30,91}; Random random = new Random(); x = (n[random.nextInt(n.length)]); y = (n[random.nextInt(n.length)]); z = (n[random.nextInt(n.length)]); question = "What is " + x + " * " + y + "?"; ans = x * y; } if (elo == 1600 || elo == 1650) { int n [] = {11,16,21,22,33,44,46,54,55,66,77,88,99}; Random random = new Random(); x = (n[random.nextInt(n.length)]); y = (n[random.nextInt(n.length)]); z = (n[random.nextInt(n.length)]); question = "What is " + x + " + " + y + " + " + z + "?"; ans = x + y + z; } if (elo == 1700 || elo == 1700) { int n [] = {9,12,15,17,30,40,50,60,70,80,90}; Random random = new Random(); x = (n[random.nextInt(n.length)]); y = (n[random.nextInt(n.length)]); z = (n[random.nextInt(n.length)]); question = "What is 5 * " + x + " + 5 * " + y + "?\n Remember order of operations!"; ans = x + x + x + x + x + y + y + y + y + y; //5x + 5y } if (elo == 1800 || elo == 1800) { int n [] = {7,9,5,4,20,50,30,21,100}; Random random = new Random(); x = (n[random.nextInt(n.length)]); y = (n[random.nextInt(n.length)]); z = (n[random.nextInt(n.length)]); question = "What is 5 * " + x + " + 5 * " + y + " - 5 * " + z + "?\n Remember order of operations!"; ans = x + x + x + x + x + y + y + y + y + y - z - z - z - z - z; //5x + 5y - 5z } if (elo == 1900 || elo == 1950) { int n [] = {11,16,21,22,33,44,46,54,55,66,77,88,99}; Random random = new Random(); x = (n[random.nextInt(n.length)]); y = (n[random.nextInt(n.length)]); z = (n[random.nextInt(n.length)]); question = "What is " + x + " * " + y + "?"; ans = x * y; } if (elo == 2000) { int n [] = {11,16,21,22,33,44,46,54,55,66,77,88,99}; Random random = new Random(); x = (n[random.nextInt(n.length)]); y = (n[random.nextInt(n.length)]); z = (n[random.nextInt(n.length)]); question = "What is (" + x + " * " + y + " * " + z + ") + (" + x + " + " + y + " + " + z + ")?"; ans = (x * y * z) + (x + y + z); } System.out.println("X: " + x + " | Y: " + y + " | Answer: " + ans); } } class AreTheyReady implements ActionListener { private TextField userInput; int elo; int ans; int parsedAnswer; int run = 1; boolean ready; public NewIdeaMath a = new NewIdeaMath(); public AreTheyReady(TextField userInput, int elo, int ans, boolean ready) { this.userInput = userInput; this.elo = elo; this.ans = ans; this.ready = ready; } public void actionPerformed(ActionEvent ae) { String answerEntered = userInput.getText(); try { parsedAnswer = Integer.parseInt(answerEntered); //System.out.println("Parsed: " + parsedAnswer); //System.out.println("ans: " + ans); } catch(NumberFormatException e) { System.out.println("That isn't a number silly!"); } returnScore(); returnReady(); System.out.println("ELO: " + elo); //System.out.println("Answer: " + a.ans); userInput.setText(""); a.elo = elo; a.ask(); a.questionField.setText(a.question); a.scoreField.setText(""+a.elo); } public boolean returnReady() { //a.getELO(); a.revalidate(); ready = true; return ready; } public int returnScore() { if (run == 1) { if (parsedAnswer == ans) { elo += 50; System.out.println("You are correct."); } else { elo -= 100; System.out.println("You are wrong."); } run = run + 1; return elo; } else { if (parsedAnswer == a.ans) { elo += 50; System.out.println("You are correct."); } else { elo -= 100; System.out.println("You are wrong."); } return elo; } } }