Package net.yura.blackjack.client

Source Code of net.yura.blackjack.client.BlackJackClient

package net.yura.blackjack.client;

import net.yura.lobby.client.TurnBasedAdapter;
import java.awt.Frame;
import java.awt.GridLayout;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JTextField;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.JLabel;
import javax.swing.JFrame;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.border.EmptyBorder;
import javax.swing.BoxLayout;
import javax.swing.Box;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.BorderLayout;
import javax.swing.JOptionPane;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.awt.Graphics;
import java.awt.Color;
import java.util.HashMap;
import net.yura.lobby.model.Game;

public class BlackJackClient extends TurnBasedAdapter implements ActionListener {

  public static final String version="0.1";

  // game setup
  private JDialog dialog;
  private boolean newgameyes;
  private JTextField namebox;
  private JSpinner humanplayers;
  private JSpinner easybotsplayers;
  private JSpinner numberRounds;
  private JSpinner numberDecks;
  private HashMap icons;
  private static BufferedImage icon;

  // ingame
  private JLabel nameLabel;
  private JFrame gameframe;
  private BlackJackGUI gamepanel;


  // #######################################################################################
  // Game setup
  // #######################################################################################

  public Game newGameDialog(Frame parent, String options,String myname) {

    if (dialog == null) {


      dialog = new JDialog(parent,"New Game Options",true);

      JPanel bcontent = new JPanel();

      JButton okbutton = new JButton("Create Table");
      JButton cancelbutton = new JButton("Cancel");

      okbutton.setActionCommand("ok");
      cancelbutton.setActionCommand("cancel");

      okbutton.addActionListener(this);
      cancelbutton.addActionListener(this);

      bcontent.add(okbutton);
      bcontent.add(cancelbutton);

      dialog.getContentPane().add(bcontent,java.awt.BorderLayout.SOUTH);


      JPanel ocontent = new JPanel();
      ocontent.setLayout(new BoxLayout( ocontent, BoxLayout.X_AXIS ) );

      JPanel left = new JPanel( new GridLayout(0,1,10,10) );
      JPanel right = new JPanel( new GridLayout(0,1,10,10) );

      ocontent.setBorder( new EmptyBorder(20,20,20,20) );

      namebox = new JTextField("BlackJack "+myname,30);

      humanplayers = new JSpinner( new SpinnerNumberModel(2,1,5,1) );
      easybotsplayers = new JSpinner( new SpinnerNumberModel(3,0,4,1) );
      numberRounds = new JSpinner( new SpinnerNumberModel(5,1,100,1) );
      numberDecks = new JSpinner( new SpinnerNumberModel(5,1,10,1) );

      left.add( new JLabel("Game Name") );
      right.add(namebox);

      left.add( new JLabel("Human Players") );
      right.add(humanplayers);

      left.add( new JLabel("Bot Players") );
      right.add(easybotsplayers);

      left.add( new JLabel("Number of rounds") );
      right.add(numberRounds);

      left.add( new JLabel("Number of decks") );
      right.add(numberDecks);

      ocontent.add(left);
      ocontent.add(right);

      dialog.getContentPane().add(ocontent);

      dialog.setResizable(false);
      dialog.pack();

    }

    dialog.setVisible(true);

    if (newgameyes) {

      newgameyes = false;

      String bkoptions = getStartOptions(
        ((Integer)numberDecks.getValue()).intValue(),
        ((Integer)easybotsplayers.getValue()).intValue(),
        ((Integer)numberRounds.getValue()).intValue()
      );

      return new Game( namebox.getText(), bkoptions , ((Integer)humanplayers.getValue()).intValue() );

    }
    return null;

  }

  public ImageIcon getIcon(String options) {

    String str = options.split("\\ ")[2];

    if (icons==null) {
      icons = new HashMap();
    }

    ImageIcon icon = (ImageIcon)icons.get(str);

    if (icon==null) {


      BufferedImage srcimg = getIcon();

      BufferedImage iconi = new BufferedImage(srcimg.getWidth(),srcimg.getHeight(), BufferedImage.TYPE_INT_BGR );

      Graphics g = iconi.getGraphics();
      g.drawImage(srcimg,0,0,null);
      g.setColor( Color.BLACK );
      g.drawString(str,srcimg.getWidth()-g.getFontMetrics().stringWidth(str),srcimg.getHeight());
      g.dispose();

      icon = new ImageIcon(iconi);
      icons.put(str,icon);

    }

    return icon;
  }

  // #######################################################################################
  // ingame
  // #######################################################################################



  public void startNewGame(String name) {

    if (gameframe==null) {

      gameframe = new JFrame();

      BufferedImage srcimg = getIcon();

      gameframe.setIconImage( srcimg.getSubimage(0,0,srcimg.getHeight(),srcimg.getHeight()) );

      gameframe.addWindowListener(
        new java.awt.event.WindowAdapter() {
          public void windowClosing(java.awt.event.WindowEvent evt) {

            closegame();
          }
        }
      );

      gamepanel = new BlackJackGUI();
      gameframe.getContentPane().add(gamepanel);

      gameframe.getContentPane().add( makeSidePanel( gamepanel.getImageHeight(),"About","about",this ), BorderLayout.EAST );

      gameframe.pack();

    }

    gameframe.setTitle("yura.net Blackjack - "+name);
    nameLabel.setText(name);
    gameframe.setVisible(true);
    gameframe.requestFocus();
  }

  // this NEEDS to call leaveGame();
  public void closegame() {

    gameframe.setVisible(false);

    leaveGame();
  }

  public void gameString(String message) {

    System.out.println("gameString: "+message);
    // TODO
  }

  public void blockInput() {

    // TODO

  }

  public void resignPlayer() {

    // TODO

  }

  public void renamePlayer(String oldname,String newname) {

    // TODO

  }

  public void gameObject(Object object) {

    // not used

  }

  // ##########################################################################
  // private methods
  // ##########################################################################

  private static BufferedImage getIcon() {

    if (icon!=null) { return icon; }

    try {

      icon = ImageIO.read(BlackJackClient.class.getResource( "icon.png" ));
      return icon;

    }
    catch (Exception e) {

      throw new RuntimeException("cant find icon images");
      //e.printStackTrace();
      //return null;
    }

  }

  private String getStartOptions(int availableDecks,int numbots,int numberOfRounds) {

    boolean multiplayer=true;
    boolean reusedeck=false;

    return availableDecks + " " + numbots + " " + numberOfRounds +" " + (multiplayer?1:0) +" " + (reusedeck?1:0) ;

  }

  public void actionPerformed(ActionEvent ae) {

    if ("ok".equals(ae.getActionCommand())) {

      newgameyes = true;
      dialog.setVisible(false);

    }
    else if ("cancel".equals(ae.getActionCommand())) {

      dialog.setVisible(false);

    }
    else if ("about".equals(ae.getActionCommand())) {

      JOptionPane.showMessageDialog(gameframe,

      "<html><font size=+1><b>yura.net<br>Tournament Blackjack</b></font>\n \nVersion "+version+"\nCopyright (c) 2007 yura.net\nWritten by Yura Mamyrin (yura@yura.net)"

      , "About", JOptionPane.PLAIN_MESSAGE , new ImageIcon( this.getClass().getResource("bjlogo.png") ) );

    }

  }

  private Component makeSidePanel(int h,String a,String b, ActionListener al) {


    Box sidepanel = new Box(javax.swing.BoxLayout.Y_AXIS);

    JButton aboutButton = new JButton( a );
    aboutButton.addActionListener(al);
    aboutButton.setActionCommand(b);

    JPanel panel1 = new JPanel();
    JPanel panel2 = new JPanel();
    JPanel panel3 = new JPanel( new GridLayout(1,2,5,5) );
    panel3.setBorder( new EmptyBorder(5,5,5,5) );


    nameLabel = new JLabel();

    panel1.add( nameLabel );
    panel2.add( timer );
    panel3.add( startButton );
    panel3.add( aboutButton );

    sidepanel.add( panel1 );
    sidepanel.add( panel2 );
    sidepanel.add( playerListArea );
    sidepanel.add( panel3 );
    sidepanel.add( chatBoxArea );


    Dimension topsize = new Dimension(200,120);

    playerListArea.setPreferredSize( topsize );
    playerListArea.setMaximumSize( topsize );
    playerListArea.setMinimumSize( topsize );

    sidepanel.setPreferredSize( new Dimension(200,h) );

    return sidepanel;
  }


}
TOP

Related Classes of net.yura.blackjack.client.BlackJackClient

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.