package exercise.engine; /** * The QuestionModuleUI is the basic Module display for Text Question or Number Question. * This class displays the current question and accepts answers given by the user. */ import javax.swing.*; import javax.swing.border.LineBorder; import java.awt.*; public class QuestionModuleUI extends JPanel { /** * This is the Constructor method. It inializes the display placing the question and * answer Fields appropriately. */ //18 pixels the height of line final static int SPACE = 10; final static int QUEST_TEXT_AREA_X = 15; final static int QUEST_TEXT_AREA_Y = 15; final static int QUEST_TEXT_AREA_HEIGHT = 70; final static int QUEST_TEXT_AREA_WIDTH = 402; final static int BACKGROUND_RED = 255; final static int BACKGROUND_GREEN = 255; final static int BACKGROUND_BLUE = 204; final static int TEXT_RED = 0; final static int TEXT_GREEN = 102; final static int TEXT_BLUE = 102; final static int JBUTTON_X = -3; final static int JBUTTON_Y = 100; final static int HINT_TEXT_AREA_X = 28; final static int HINT_TEXT_AREA_Y = 106; final static int HINT_TEXT_AREA_HEIGHT = 67; final static int HINT_TEXT_AREA_WIDTH = 390; final static int LABEL_X = 15; final static int LABEL_Y = 190; final static int FIELD_X = 35; final static int FIELD_Y = 188; final static int FIELD_WIDTH = 200; final static int FIELD_HEIGHT = 20; final static int PIXEL_GAP = 15; protected JTextArea questTextArea; protected JButton hintButton; protected JTextArea hintTextArea; protected JTextField answerField; protected JLabel answerLabel; protected QuestionContents questionContents; protected QuestionModel questModel; protected ImageIcon hintDown; protected ImageIcon hintOff; protected ImageIcon hintRoll; protected ImageIcon hintUp; QuestionModuleUI( JButton hintButton, ImageIcon hintDown, ImageIcon hintOff, ImageIcon hintRoll, ImageIcon hintUp, QuestionContents questionContents ) { this.questionContents = questionContents; this.hintButton = hintButton; this.hintDown = hintDown; this.hintOff = hintOff; this.hintDown = hintRoll; this.hintDown = hintUp; questModel = new QuestionModel (); questModel.setQuestion ( questionContents.getQuestion() ); //Set the Size of the Module setSize ( 432, 223 ); //Set the Layout Manager setLayout ( new BulletinLayout () ); //Set the Background Color of the Module setBackground ( new Color ( BACKGROUND_RED, BACKGROUND_GREEN, BACKGROUND_BLUE ) ); //Initialize the Question to be displayed questTextArea = new JTextArea(); questTextArea.setEditable ( false ); questTextArea.setLineWrap ( true ); questTextArea.setWrapStyleWord ( true ); questTextArea.setText ( questModel.getQuestion() ); questTextArea.setFont ( new Font("SansSerif", Font.PLAIN, 12)); questTextArea.setForeground( new Color ( TEXT_RED, TEXT_GREEN, TEXT_BLUE ) ); questTextArea.setBackground ( new Color ( BACKGROUND_RED, BACKGROUND_GREEN, BACKGROUND_BLUE ) ); //Add the Question to the module add ( questTextArea ); questTextArea.setBounds ( QUEST_TEXT_AREA_X, QUEST_TEXT_AREA_Y, QUEST_TEXT_AREA_WIDTH, QUEST_TEXT_AREA_HEIGHT); //Setup the hint button hintButton.setRolloverIcon ( hintRoll ); hintButton.setPressedIcon ( hintDown ); hintButton.setDisabledIcon ( hintOff ); hintButton.setIcon( hintUp) ; hintButton.setRolloverEnabled ( true ); //Takes border focus and filled painting for nice clean look of button. hintButton.setContentAreaFilled( false ); hintButton.setFocusPainted ( false ); hintButton.setBorderPainted ( false ); //add the hint button add ( hintButton ); hintButton.setLocation ( JBUTTON_X, JBUTTON_Y ); //Initiaize the Hint Area to be displayed hintTextArea = new JTextArea(); hintTextArea.setLineWrap ( true ); hintTextArea.setWrapStyleWord ( true ); hintTextArea.setFont ( new Font("SansSerif", Font.PLAIN, 12)); hintTextArea.setForeground( new Color ( TEXT_RED, TEXT_GREEN, TEXT_BLUE ) ); hintTextArea.setText ( "Please click on the ? button for a hint.\nPlease click on the ? button for a hint.\nPlease click on the ? button for a hint." ); hintTextArea.setBorder ( new LineBorder ( new Color ( TEXT_RED, TEXT_GREEN, TEXT_BLUE ) ) ); //add the hint text area add( hintTextArea ); hintTextArea.setBounds ( HINT_TEXT_AREA_X, HINT_TEXT_AREA_Y, HINT_TEXT_AREA_WIDTH, HINT_TEXT_AREA_HEIGHT ); //Initialize the AnswerField to be Displayed answerLabel = new JLabel ( "A:" ); answerLabel.setFont ( new Font("SansSerif", Font.PLAIN, 12)); answerLabel.setForeground( new Color ( TEXT_RED, TEXT_GREEN, TEXT_BLUE ) ); answerField = new JTextField (); answerField.setPreferredSize ( new Dimension ( FIELD_WIDTH, FIELD_HEIGHT ) ); //Add the AnswerField to the Module add ( answerLabel ); answerLabel.setLocation ( LABEL_X, LABEL_Y ); add ( answerField ); answerField.setLocation ( FIELD_X, FIELD_Y ); } public String getUserAnswer() { return answerField.getText(); } }