Pastie now auto-senses if line-wrap is a bad or good idea. Feedback?
## mark a section (Learn more)
This paste will be private.
/////////////////////////////////////////////////////////////////////////// // // Pro24301 Write a program that simulates the Jeopardy contestant // push buttons. This program is to have three player // views and one Alex view. The players views must keep the // name of the player local as an identification mechanism. // The players view will have one button and the name of the // player. The Alex view will be one text area to display the // winner, for that question, and a reset button. // // The model is to contain a String name instance variable and // a boolean buzzed instance variable. The name holds the name // of the player the pressed the buzzer and the boolean variable // holds the buzzer state. Alex's view resets the buzzed to // false. The first players buzzers sets the buzzed variable // to true and places there name as the winnning buzzard. // // // +--------------+ +--------------+ +--------------+ // | _ = X | | _ = X | | _ = X | // +--------------+ +--------------+ +--------------+ // | | | | | | // | Player One | | Player Two | | Player Three | // | | | | | | // | +--------+ | | +--------+ | | +--------+ | // | | Buzzer | | | | Buzzer | | | | Buzzer | | // | +--------+ | | +--------+ | | +--------+ | // +--------------+ +--------------+ +--------------+ // // // +-------------------+ // | _ = X | // +-------------------+ // | | // | Alex | // | +---------------+ | // | | Player three | | // | | | | // | +---------------+ | // | | // | +---------+ | // | | Reset | | // | +---------+ | // +-------------------+ // // // // /////////////////////////////////////////////////////////////////////////// // // // // // package Buzzer; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; public class Pro24301 { public static void main(String[] args) { new Controller(); }} class Controller extends JPanel { GUIView viewA; GUIView viewB; GUIView viewC; GUIViewB alex; Model model; public Controller() { viewA = new GUIView("Player One"); viewB = new GUIView("Player Two"); viewC = new GUIView("Player Three"); alex = new GUIViewB(); model = new Model(); model.addObserver( viewA ); model.addObserver( viewB ); model.addObserver( viewC ); model.addObserver( alex ); viewA.setController(new ButtonController1()); viewB.setController(new ButtonController2()); viewC.setController(new ButtonController3()); alex.setController(new ResetController()); } class ButtonController1 implements ActionListener { public void actionPerformed(ActionEvent event) { if(model.buzzed == false) model.buzz(viewA.theName); model.sendNotification(); } } class ButtonController2 implements ActionListener { public void actionPerformed(ActionEvent event) { if(model.buzzed == false) model.buzz(viewB.theName); model.sendNotification(); } } class ButtonController3 implements ActionListener { public void actionPerformed(ActionEvent event) { if(model.buzzed == false) model.buzz(viewC.theName); model.sendNotification(); } } class ResetController implements ActionListener { public void actionPerformed(ActionEvent event) { model.reset(); model.sendNotification(); } } } class GUIView extends JFrame implements Observer { private JButton buzz; private JLabel name; protected String theName; public GUIView(String name) { setSize(200,100); theName = name; setLayout( new FlowLayout() ); { this.name = new JLabel( name ); add( this.name ); buzz = new JButton("Buzz In"); add( buzz ); } setLocation(new Random().nextInt(300)+100, new Random().nextInt(300)+100); setDefaultCloseOperation(EXIT_ON_CLOSE); setFocusable(true); setVisible(true); } public String getName() { return theName; } public void update(Observable obs, Object x){} public void setController(ActionListener listener) { buzz.addActionListener(listener); } } class GUIViewB extends JFrame implements Observer { private JButton reset; private JLabel name; private JTextField field; public GUIViewB() { setSize(200,100); setLayout( new FlowLayout() ); { this.name = new JLabel( "Alex"); add( name ); field = new JTextField(10); add( field ); reset = new JButton("Reset"); add( reset ); } setLocation(new Random().nextInt(300)+100, new Random().nextInt(300)+100); setDefaultCloseOperation(EXIT_ON_CLOSE); setFocusable(true); setVisible(true); } public void update(Observable obs, Object x) { field.setText(((Model)obs).name); } public void setController(ActionListener listener) { reset.addActionListener(listener); } } class Model extends Observable { protected boolean buzzed; protected String name; public void reset() { buzzed = false; name = ""; } public void buzz(String aName) { buzzed = true; name = aName; } public void sendNotification() { setChanged(); notifyObservers(); } }
From the Design Piracy series on my blog: