Java Source Code: jadex.examples.blackjack.player.HumanPlayerInterface


   1: package jadex.examples.blackjack.player;
   2: 
   3: import javax.swing.*;
   4: import java.awt.*;
   5: import java.awt.event.*;
   6: import java.beans.PropertyChangeListener;
   7: 
   8: import jadex.runtime.AgentEvent;
   9: import jadex.runtime.IAgentListener;
  10: import jadex.runtime.IExternalAccess;
  11: import jadex.util.SGUI;
  12: import jadex.util.SimplePropertyChangeSupport;
  13: import jadex.examples.blackjack.*;
  14: import jadex.examples.blackjack.gui.*;
  15: 
  16: /**
  17:  *  The human player interface for playing blackjack.
  18:  */
  19: public class HumanPlayerInterface extends GameStateFrame
  20:	  {
  21:    //-------- attributes --------
  22:
  23:    /** The property change support. */
  24:    protected SimplePropertyChangeSupport pcs;
  25:
  26:    //-------- constructors --------
  27:
  28:    /**
  29:     *  Create a new human player interface.
  30:     */
  31:    public HumanPlayerInterface(Player player, GameState gamestate, final IExternalAccess agent)
  32:	      {
  33:        super(gamestate, null);
  34:        setControlPanel(new HumanPlayerControlPanel(player, gamestate));
  35:        pcs = new SimplePropertyChangeSupport(this);
  36:
  37:        // Kill agent on exit.
  38:        addWindowListener(new WindowAdapter()
  39:	          {
  40:            public void windowClosing(WindowEvent e)
  41:	              {
  42:                agent.killAgent();
  43:            }
  44:        });
  45:
  46:        // Close window on agent death.
  47:        agent.addAgentListener(new IAgentListener()
  48:	          {
  49:            public void agentTerminating(AgentEvent e)
  50:	              {
  51:                SwingUtilities.invokeLater(new Runnable()
  52:	                  {
  53:                    public void run()
  54:	                      {
  55:                        HumanPlayerInterface.this.dispose();
  56:                    }
  57:                });
  58:            }
  59:        }, false);
  60:
  61:        this.setTitle("Human player: "+player.getName());
  62:        this.pack();
  63:        this.setLocation(SGUI.calculateMiddlePosition(this));
  64:        this.setVisible(true);
  65:    }
  66:
  67:    //-------- property methods --------
  68:
  69:    /**
  70:     *  Add a PropertyChangeListener to the listener list.
  71:     *  The listener is registered for all properties.
  72:     *  @param listener  The PropertyChangeListener to be added.
  73:     */
  74:    public void addPropertyChangeListener(PropertyChangeListener listener)
  75:	      {
  76:        pcs.addPropertyChangeListener(listener);
  77:    }
  78:
  79:    /**
  80:     *  Remove a PropertyChangeListener from the listener list.
  81:     *  This removes a PropertyChangeListener that was registered
  82:     *  for all properties.
  83:     *  @param listener  The PropertyChangeListener to be removed.
  84:     */
  85:    public void removePropertyChangeListener(PropertyChangeListener listener)
  86:	      {
  87:        pcs.removePropertyChangeListener(listener);
  88:    }
  89:
  90:
  91:    /**
  92:     *  The control planel for interaction a human player.
  93:     */
  94:    public class HumanPlayerControlPanel extends JPanel
  95:	      {
  96:        /** The bet. */
  97:        protected JSpinner betsp;
  98:
  99:        /** The button for placing a bet. */
 100:        protected JButton betbut;
 101:
 102:        /** The bet progress bar. */
 103:        protected JProgressBar betprogress;
 104:
 105:        /** The draw card decision. */
 106:        protected JButton draw;
 107:
 108:        /** The stop button. */
 109:        protected JButton stop;
 110:
 111:        /** The draw card progress bar. */
 112:        protected JProgressBar drawprogress;
 113:
 114:        /** The bet. */
 115:        protected int bet;
 116:
 117:        /** The draw state. */
 118:        protected boolean drawcard;
 119:
 120:        /** The timer. */
 121:        protected Timer timer;
 122:
 123:        /**
 124:         *  Create a new panel.
 125:         */
 126:        public HumanPlayerControlPanel(Player player, GameState gamestate)
 127:	          {
 128:            this.setLayout(new BorderLayout());
 129:
 130:            betsp = new JSpinner(new SpinnerNumberModel(10,1,100,1));
 131:            betbut = new JButton(GUIImageLoader.getImage("bet"));
 132:            betbut.addActionListener(new ActionListener()
 133:	              {
 134:                public void actionPerformed(ActionEvent e)
 135:	                  {
 136:                    makeBet();
 137:                }
 138:            });
 139:            betbut.setEnabled(false);
 140:            betprogress = new JProgressBar(0, 5); // todo: use variable
 141:
 142:            draw = new JButton(GUIImageLoader.getImage("hit"));
 143:            draw.addActionListener(new ActionListener()
 144:	              {
 145:                public void actionPerformed(ActionEvent e)
 146:	                  {
 147:                    //System.out.println("Human player chose to draw another card");
 148:                    setDrawCard(true);
 149:                }
 150:            });
 151:            draw.setEnabled(false);
 152:            stop = new JButton(GUIImageLoader.getImage("stand"));
 153:            stop.addActionListener(new ActionListener()
 154:	              {
 155:                public void actionPerformed(ActionEvent e)
 156:	                  {
 157:                    //System.out.println("Human player chose to stop.");
 158:                    setDrawCard(false);
 159:                }
 160:            });
 161:            stop.setEnabled(false);
 162:            drawprogress = new JProgressBar(0, 5); // todo: use variable
 163:
 164:            JPanel control = new JPanel(new GridBagLayout());
 165:            control.add(betprogress, new GridBagConstraints(0, 0, 1, 1, 1, 0, GridBagConstraints.WEST,
 166:                GridBagConstraints.BOTH, new Insets(2,2,2,2), 0, 0));
 167:            control.add(betsp, new GridBagConstraints(1, 0, 1, 1, 0, 0, GridBagConstraints.WEST,
 168:                GridBagConstraints.HORIZONTAL, new Insets(2,2,2,2), 0, 0));
 169:            control.add(betbut, new GridBagConstraints(2, 0, 1, 1, 0, 0, GridBagConstraints.WEST,
 170:                GridBagConstraints.HORIZONTAL, new Insets(2,2,2,2), 0, 0));
 171:
 172:            control.add(drawprogress, new GridBagConstraints(0, 2, 1, 1, 1, 0, GridBagConstraints.WEST,
 173:                GridBagConstraints.BOTH, new Insets(2,2,2,2), 0, 0));
 174:            control.add(draw, new GridBagConstraints(1, 2, 1, 1, 0, 0, GridBagConstraints.WEST,
 175:                GridBagConstraints.HORIZONTAL, new Insets(2,2,2,2), 0, 0));
 176:            control.add(stop, new GridBagConstraints(2, 2, 1, 1, 0, 0, GridBagConstraints.WEST,
 177:                GridBagConstraints.HORIZONTAL, new Insets(2,2,2,2), 0, 0));
 178:
 179:            /*Dimension d = new Dimension(80,20);
 180:            betprogress.setMaximumSize(d);
 181:            betprogress.setPreferredSize(d);
 182:            drawprogress.setMaximumSize(d);
 183:            drawprogress.setPreferredSize(d);*/
 184:            Dimension d = new Dimension(50,30);
 185:            betsp.setMaximumSize(d);
 186:            betsp.setPreferredSize(d);
 187:            betbut.setMinimumSize(d);
 188:            betbut.setPreferredSize(d);
 189:            draw.setMinimumSize(d);
 190:            draw.setPreferredSize(d);
 191:            stop.setMinimumSize(d);
 192:            stop.setPreferredSize(d);
 193:
 194:            this.add("Center", new PlayerPanel(player));
 195:            this.add("South", control);
 196:        }
 197:
 198:        /**
 199:         *  Make a bet.
 200:         */
 201:        protected void makeBet()
 202:	          {
 203:            try
 204:	              {
 205:                setBet(((Integer)betsp.getValue()).intValue());
 206:                //System.out.println("Human player made his bet: "+bettf.getText());
 207:            }
 208:            catch(NumberFormatException ne)
 209:	              {
 210:                // do nothing.
 211:            }
 212:        }
 213:
 214:        /**
 215:         *  Tell the human that his bet is required.
 216:         */
 217:        public void enableBid()
 218:	          {
 219:            //bettf.setBackground(Color.GREEN);
 220:            betbut.setEnabled(true);
 221:            //bettf.setEditable(true);
 222:            betprogress.setValue(5); // todo: use variable timeout
 223:            betprogress.setString(betprogress.getValue()+" sec left");
 224:            betprogress.setStringPainted(true);
 225:            timer = new Timer(1000, null);
 226:            ActionListener al = new ActionListener()
 227:	              {
 228:                public void actionPerformed(ActionEvent e)
 229:	                  {
 230:                    int time = betprogress.getValue()-1;
 231:                    betprogress.setValue(betprogress.getValue()-1);
 232:                    betprogress.setString(time+" sec left");
 233:                    if(betprogress.getValue()==0)
 234:	                      {
 235:                        timer.stop();
 236:                        makeBet();
 237:                    }
 238:                }
 239:            };
 240:            timer.addActionListener(al);
 241:            timer.start();
 242:        }
 243:
 244:        /**
 245:         *  Disable bid.
 246:         */
 247:        public void disableBid()
 248:	          {
 249:            //bettf.setBackground(Color.RED);
 250:            betbut.setEnabled(false);
 251:            //bettf.setEditable(false);
 252:            timer.stop();
 253:            betprogress.setValue(0);
 254:            betprogress.setStringPainted(false);
 255:        }
 256:
 257:        /**
 258:         *  Enable drawing a card.
 259:         */
 260:        public void enableDrawCard()
 261:	          {
 262:            //draw.setBackground(Color.GREEN);
 263:            //stop.setBackground(Color.GREEN);
 264:            draw.setEnabled(true);
 265:            stop.setEnabled(true);
 266:            drawprogress.setValue(5); // todo: use variable timeout
 267:            drawprogress.setString(drawprogress.getValue()+" sec left");
 268:            drawprogress.setStringPainted(true);
 269:            timer = new Timer(1000, null);
 270:            ActionListener al = new ActionListener()
 271:	              {
 272:                public void actionPerformed(ActionEvent e)
 273:	                  {
 274:                    int time = drawprogress.getValue()-1;
 275:                    drawprogress.setString(time+" sec left");
 276:                    drawprogress.setValue(time);
 277:                    if(drawprogress.getValue()==0)
 278:                        timer.stop();
 279:                }
 280:            };
 281:            timer.addActionListener(al);
 282:            timer.start();
 283:        }
 284:
 285:        /**
 286:         *  Disable to draw a card.
 287:         */
 288:        public void disableDrawCard()
 289:	          {
 290:            //draw.setBackground(Color.RED);
 291:            //stop.setBackground(Color.RED);
 292:            draw.setEnabled(false);
 293:            stop.setEnabled(false);
 294:            timer.stop();
 295:            drawprogress.setValue(0);
 296:            drawprogress.setStringPainted(false);
 297:        }
 298:
 299:        /**
 300:         *  Get the bet.
 301:         */
 302:        public int    getBet()
 303:	          {
 304:            return bet;
 305:        }
 306:
 307:        /**
 308:         *  Set the bet.
 309:         *  @param bet The bet.
 310:         */
 311:        public void    setBet(int bet)
 312:	          {
 313:            this.bet    = bet;
 314:            pcs.firePropertyChange("new_bet", null, new Integer(bet));
 315:        }
 316:
 317:        /**
 318:         *  Does the player want another card.
 319:         *  @return True, if another card.
 320:         */
 321:        public boolean isDrawCard()
 322:	          {
 323:            return drawcard;
 324:        }
 325:
 326:        /**
 327:         *  Set the draw card decision.
 328:         *  @param drawcard
 329:         */
 330:        public void setDrawCard(boolean drawcard)
 331:	          {
 332:            this.drawcard = drawcard;
 333:            pcs.firePropertyChange("new_drawcard", null, new Boolean(drawcard));
 334:        }
 335:
 336:
 337:    }
 338:}