/* * @(#)ChoiceApplet.java 0.0.0 99/09/02 * * Copyright (c) 1999 by Willie Wheeler. All rights reserved. */ package exercise.choice; import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; import javax.swing.border.*; import exercise.*; import widget.*; /** * An applet to allow the student to do multiple choice exercises, submit * them for evaluation, and receive post-evaluation feedback. * * @version 0.0.0 09/02/99 * @author Willie Wheeler */ public class ChoiceApplet extends JApplet implements ActionListener { // Applet parameters. protected static final String EXERCISE_ID_PARAM = "exercise_id"; // Action commands. protected static final String SUBMIT_CMD = "submit"; // Resource names. protected static final String SUBMIT_RESOURCE = "images/submit_exercise.gif"; // Resources. protected static ImageIcon submitIcon; static { Class cl = ChoiceApplet.class; submitIcon = new ImageIcon(cl.getResource(SUBMIT_RESOURCE)); } // Colors. protected static final Color BORDER_COLOR = new Color(51, 51, 102); protected static final Color INSTRUCTIONS_COLOR = new Color(51, 102, 153); protected static final Color CHOICES_COLOR = new Color(255, 255, 204); // Other stuff. protected String exerciseID; protected ChoiceExercise exercise; protected ChoicePane choicePane; // Architecturally, the evaluator belongs on the server, not on the // client. (It's easier for the students to cheat if the evaluator sits // on the client.) But since we can't run servlets from CMU Online, and // since we have to use CMU Online, we have to put the evaluator here. protected static ChoiceEvaluator evaluator = new ChoiceEvaluator(); public void init() { try { // Load an exercise. exerciseID = getParameter(EXERCISE_ID_PARAM); exercise = ChoiceExercise.loadExercise(exerciseID); // Install the exercise workbench. widget.ConcretePLAF.install(); // hack Container cp = getContentPane(); cp.setBackground(BORDER_COLOR); cp.setLayout(new GridBagLayout()); // Install the instruction panel. addComponent(cp, buildInstructionPanel(), 0, 0, 1, 1, new Insets(2, 2, 0, 2), 1.0, 0.0, GridBagConstraints.BOTH, GridBagConstraints.CENTER); // Install the choice panel. int type = exercise.getType(); String[] choices = exercise.getChoices(); choicePane = new ChoicePane(type, choices); choicePane.setOpaque(true); choicePane.setBackground(CHOICES_COLOR); addComponent(cp, choicePane, 0, 1, 1, 1, new Insets(2, 2, 2, 2), 1.0, 1.0, GridBagConstraints.BOTH, GridBagConstraints.CENTER); } catch (IOException e) { String msg = "Choice.Applet.init(): Failed to load exercise."; System.err.println(msg); } } protected JPanel buildInstructionPanel() { JPanel panel = new JPanel(); panel.setOpaque(true); panel.setBackground(INSTRUCTIONS_COLOR); panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS)); panel.setBorder(new EmptyBorder(10, 10, 10, 10)); // Add the instructions. String instr = "INSTRUCTIONS: " + exercise.getInstructions(); JTextArea area = new JTextArea(instr, 4, 30); Dimension d = area.getPreferredScrollableViewportSize(); Insets in = area.getInsets(); d.width += in.left + in.right; d.height += in.top + in.bottom; area.setPreferredSize(d); area.setOpaque(false); area.setForeground(Color.white); area.setEditable(false); area.setLineWrap(true); area.setWrapStyleWord(true); panel.add(area); // Add a strut. panel.add(Box.createHorizontalStrut(10)); // Add the submit button. JButton submitButton = new JButton("Submit", submitIcon) { public float getAlignmentX() { return Component.CENTER_ALIGNMENT; } }; submitButton.setHorizontalTextPosition(JButton.CENTER); submitButton.setVerticalTextPosition(JButton.BOTTOM); submitButton.setActionCommand(SUBMIT_CMD); submitButton.addActionListener(this); panel.add(submitButton); // Return the panel. return panel; } /** * Adapted from O'Reilly's Java AWT Reference, p. 276, */ public static void addComponent(Container container, Component component, int gridx, int gridy, int gridwidth, int gridheight, Insets insets, double weightx, double weighty, int fill, int anchor) { GridBagLayout gbl = (GridBagLayout)container.getLayout(); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = gridx; gbc.gridy = gridy; gbc.gridwidth = gridwidth; gbc.gridheight = gridheight; gbc.insets = insets; gbc.weightx = weightx; gbc.weighty = weighty; gbc.fill = fill; gbc.anchor = anchor; gbl.setConstraints(component, gbc); container.add(component); } public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); if (cmd == SUBMIT_CMD) { doSubmit(); } } protected void doSubmit() { // Build the student's submission. int numChoices = choicePane.getNumChoices(); boolean[] selected = new boolean[numChoices]; for (int i = 0; i < numChoices; i++) { selected[i] = choicePane.isSelected(i); } ChoiceSubmission subn = new ChoiceSubmission( null, exercise.getExerciseID(), null, selected); // Submit it to the evaluator. ChoiceEvaluation eval = evaluator.evaluateSubmission(subn); // Present feedback to the student. String msg = (eval.studentPassed() ? "Correct!" : "Incorrect."); JOptionPane.showMessageDialog(this, msg); } }