import java.awt.GridLayout; import javax.swing.JPanel; import javax.swing.JTextField; import java.util.Random; /** This provides GUI interfaces for Simultaneous @author Melvin Fitting @version April, 2003 @version Revised December, 2007 */ public class SimultaneousGUI implements QuestionGUI { private Simultaneous problem; private JTextField xAns, yAns; //for the student to enter answers public SimultaneousGUI(Random r) { problem = new Simultaneous(r); } public JPanel askQuestion() { JPanel p = new JPanel(); p.setLayout(new GridLayout(5,1)); JTextField question1, question2, question3; //question1 is for the word part of the problem //question2 and question3 display the two equations Object[] q = problem.getQuestion(); question1 = new JTextField("Find a simultaneous solution to:"); question1.setBackground(QUESTION); question2 = new JTextField(q[0] + "x + " + q[1] + "y = " + q[2]); question2.setBackground(QUESTION); question3 = new JTextField(q[3] + "x + " + q[4] + "y = " + q[5]); question3.setBackground(QUESTION); question1.setEditable(false); question2.setEditable(false); question3.setEditable(false); xAns = new JTextField("x value goes here"); xAns.setBackground(ANSWER_ENTRY); xAns.setEditable(true); yAns = new JTextField("y value goes here"); yAns.setBackground(ANSWER_ENTRY); yAns.setEditable(true); p.add(question1); p.add(question2); p.add(question3); p.add(xAns); p.add(yAns); return(p); } public void putAnswer() { String input = xAns.getText().trim(); double x = Double.parseDouble(input); input = yAns.getText().trim(); double y = Double.parseDouble(input); Object[] answer = {new Double(x), new Double(y)}; problem.putAnswer(answer); } public JPanel showAnswer() { JPanel p = new JPanel(); if (getScore() == 10) { JTextField result = new JTextField("Answered correctly."); result.setBackground(GOOD_ANSWER); result.setEditable(false); p.add(result); } else { Object[] answer = problem.getAnswer(); JTextField xAnswer = new JTextField("x = " + answer[0]); xAnswer.setBackground(BAD_ANSWER); xAnswer.setEditable(false); JTextField yAnswer = new JTextField(" y = " + answer[1]); yAnswer.setBackground(BAD_ANSWER); yAnswer.setEditable(false); p.setLayout(new GridLayout(2,1)); p.add(xAnswer); p.add(yAnswer); } return(p); } public int getScore() { return problem.getScore(); } }