Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package cardgametrackercs319; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import javax.swing.JOptionPane; import static org.apache.http.HttpHeaders.USER_AGENT; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.json.JSONObject; /** *This class displays the tracking menu, and take the all information about games * @see MainMenu * @author Group A Spring 2014 */ public class TrackLogMenu extends javax.swing.JFrame { private static String playerID, playerName; private static MainMenu parentMenu; private boolean[] played; private TrackLogMenu itself; /** * Creates new form TrackLogMenu */ public TrackLogMenu(String playerID, String playerName, MainMenu parentMenu) { itself = this; this.playerID = playerID; this.playerName = playerName; this.parentMenu = parentMenu; played = new boolean[4]; played[0] = true; played[1] = true; played[2] = true; played[3] = true; initComponents(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jInternalFrame1 = new javax.swing.JInternalFrame(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); p2id = new javax.swing.JTextField(); p2pwd = new javax.swing.JPasswordField(); jInternalFrame2 = new javax.swing.JInternalFrame(); jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); p3id = new javax.swing.JTextField(); p3pwd = new javax.swing.JPasswordField(); jInternalFrame3 = new javax.swing.JInternalFrame(); jLabel6 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); p4id = new javax.swing.JTextField(); p4pwd = new javax.swing.JPasswordField(); jButton1 = new javax.swing.JButton(); jLabel7 = new javax.swing.JLabel(); numOfGames = new javax.swing.JSlider(); numGames = new javax.swing.JLabel(); jToggleButton1 = new javax.swing.JToggleButton(); jToggleButton2 = new javax.swing.JToggleButton(); jToggleButton3 = new javax.swing.JToggleButton(); jToggleButton4 = new javax.swing.JToggleButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Game Tracking - Player Selection"); setResizable(false); jInternalFrame1.setVisible(true); jLabel1.setText("Player2 ID:"); jLabel2.setText("Player2 Password:"); p2id.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { p2idActionPerformed(evt); } }); javax.swing.GroupLayout jInternalFrame1Layout = new javax.swing.GroupLayout( jInternalFrame1.getContentPane()); jInternalFrame1.getContentPane().setLayout(jInternalFrame1Layout); jInternalFrame1Layout.setHorizontalGroup(jInternalFrame1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jInternalFrame1Layout.createSequentialGroup().addContainerGap() .addGroup(jInternalFrame1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jInternalFrame1Layout.createSequentialGroup() .addGroup(jInternalFrame1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jLabel1) .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(p2id)) .addGap(0, 0, Short.MAX_VALUE)) .addComponent(p2pwd, javax.swing.GroupLayout.Alignment.TRAILING)) .addContainerGap())); jInternalFrame1Layout.setVerticalGroup(jInternalFrame1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jInternalFrame1Layout.createSequentialGroup().addContainerGap().addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(p2id, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(jLabel2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(p2pwd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); jInternalFrame2.setVisible(true); jLabel3.setText("Player3 Password:"); jLabel4.setText("Player3 ID:"); p3id.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { p3idActionPerformed(evt); } }); javax.swing.GroupLayout jInternalFrame2Layout = new javax.swing.GroupLayout( jInternalFrame2.getContentPane()); jInternalFrame2.getContentPane().setLayout(jInternalFrame2Layout); jInternalFrame2Layout.setHorizontalGroup(jInternalFrame2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jInternalFrame2Layout.createSequentialGroup().addContainerGap() .addGroup(jInternalFrame2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jInternalFrame2Layout.createSequentialGroup() .addGroup(jInternalFrame2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel4).addComponent(jLabel3)) .addGap(0, 0, Short.MAX_VALUE)) .addComponent(p3id).addComponent(p3pwd)) .addContainerGap())); jInternalFrame2Layout.setVerticalGroup(jInternalFrame2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jInternalFrame2Layout.createSequentialGroup().addContainerGap().addComponent(jLabel4) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(p3id, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(12, 12, 12).addComponent(jLabel3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(p3pwd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(96, Short.MAX_VALUE))); jInternalFrame3.setVisible(true); jLabel6.setText("Player4 ID:"); jLabel5.setText("Player4 Password:"); p4id.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { p4idActionPerformed(evt); } }); javax.swing.GroupLayout jInternalFrame3Layout = new javax.swing.GroupLayout( jInternalFrame3.getContentPane()); jInternalFrame3.getContentPane().setLayout(jInternalFrame3Layout); jInternalFrame3Layout.setHorizontalGroup(jInternalFrame3Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jInternalFrame3Layout.createSequentialGroup().addContainerGap() .addGroup(jInternalFrame3Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jInternalFrame3Layout.createSequentialGroup() .addGroup(jInternalFrame3Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel6).addComponent(jLabel5)) .addGap(0, 0, Short.MAX_VALUE)) .addComponent(p4id).addComponent(p4pwd, javax.swing.GroupLayout.Alignment.TRAILING)) .addContainerGap())); jInternalFrame3Layout.setVerticalGroup(jInternalFrame3Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jInternalFrame3Layout.createSequentialGroup().addContainerGap().addComponent(jLabel6) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(p4id, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(12, 12, 12).addComponent(jLabel5) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(p4pwd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); jButton1.setText("Take Players"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jLabel7.setFont(new java.awt.Font("Ubuntu", 0, 24)); // NOI18N jLabel7.setText("Number of games to be played"); numOfGames.setToolTipText(numOfGames.getValue() + ""); numOfGames.setValue(1); numOfGames.addChangeListener(new javax.swing.event.ChangeListener() { public void stateChanged(javax.swing.event.ChangeEvent evt) { numOfGamesStateChanged(evt); } }); numGames.setFont(new java.awt.Font("Ubuntu", 0, 24)); // NOI18N numGames.setText("1"); jToggleButton1.setText("MauMau will be played"); jToggleButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jToggleButton1ActionPerformed(evt); } }); jToggleButton2.setText("Batak will be played"); jToggleButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jToggleButton2ActionPerformed(evt); } }); jToggleButton3.setText("King will be played"); jToggleButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jToggleButton3ActionPerformed(evt); } }); jToggleButton4.setText("BlackJack will be played"); jToggleButton4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jToggleButton4ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(20, 20, 20) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(layout.createSequentialGroup() .addComponent(jInternalFrame1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jInternalFrame2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jInternalFrame3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(numOfGames, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup().addGap(12, 12, 12).addComponent(jLabel7) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(numGames).addGap(43, 43, 43)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent( jButton1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jToggleButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jToggleButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jToggleButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jToggleButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE))))) .addContainerGap(24, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jInternalFrame3).addComponent(jInternalFrame1) .addComponent(jInternalFrame2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel7).addComponent(numGames)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(numOfGames, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jToggleButton1).addComponent(jToggleButton2)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jToggleButton3).addComponent(jToggleButton4)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(6, 6, 6))); pack(); }// </editor-fold>//GEN-END:initComponents private void p2idActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_p2idActionPerformed // TODO add your handling code here: }//GEN-LAST:event_p2idActionPerformed private void p4idActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_p4idActionPerformed // TODO add your handling code here: }//GEN-LAST:event_p4idActionPerformed private void p3idActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_p3idActionPerformed // TODO add your handling code here: }//GEN-LAST:event_p3idActionPerformed private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed if (played[0] || played[1] || played[2] || played[3]) { new Thread(new Runnable() { public void run() { try { HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost("http://ozymaxx.net/cs319/take_players.php"); post.setHeader("User-Agent", USER_AGENT); ArrayList<NameValuePair> urlParameters = new ArrayList<NameValuePair>(); urlParameters.add(new BasicNameValuePair("p2id", p2id.getText())); urlParameters.add(new BasicNameValuePair("p2pwd", new String(p2pwd.getPassword()))); urlParameters.add(new BasicNameValuePair("p3id", p3id.getText())); urlParameters.add(new BasicNameValuePair("p3pwd", new String(p3pwd.getPassword()))); urlParameters.add(new BasicNameValuePair("p4id", p4id.getText())); urlParameters.add(new BasicNameValuePair("p4pwd", new String(p4pwd.getPassword()))); post.setEntity(new UrlEncodedFormEntity(urlParameters)); HttpResponse response = client.execute(post); BufferedReader reader = new BufferedReader( new InputStreamReader(response.getEntity().getContent())); StringBuffer result = new StringBuffer(); String resLine = ""; while ((resLine = reader.readLine()) != null) { result.append(resLine); } if (!(result.toString().equals("INVALIDLOGIN")) && !(result.toString().equals("ERRORPARAMETER")) && !(result.toString().equals("SAMEPARAMETERS"))) { JSONObject playersData = new JSONObject(result.toString()); User[] users = new User[4]; users[0] = new User(playerID, playerName); JSONObject user1data = playersData.getJSONObject("player1"); JSONObject user2data = playersData.getJSONObject("player2"); JSONObject user3data = playersData.getJSONObject("player3"); users[1] = new User(user1data.getString("pid"), user1data.getString("pname")); users[2] = new User(user2data.getString("pid"), user2data.getString("pname")); users[3] = new User(user3data.getString("pid"), user3data.getString("pname")); TrackEngine engine = new TrackEngine(numOfGames.getValue(), played, users); TrackScreen tracker = new TrackScreen(0, numOfGames.getValue() * 11, engine, parentMenu); tracker.setVisible(true); itself.dispose(); } else { if (result.toString().equals("INVALIDLOGIN")) { JOptionPane.showMessageDialog(null, "Invalid login credentials for at least one of the players!"); } else { JOptionPane.showMessageDialog(null, "Parameter error in the web services!"); } } } catch (Exception e) { e.printStackTrace(); JOptionPane.showMessageDialog(null, e.toString()); } } }).start(); } else { JOptionPane.showMessageDialog(this, "You must choose at least one game to be played!"); } }//GEN-LAST:event_jButton1ActionPerformed private void numOfGamesStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_numOfGamesStateChanged numGames.setText(numOfGames.getValue() + ""); }//GEN-LAST:event_numOfGamesStateChanged private void jToggleButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton2ActionPerformed played[0] = !(played[0]); if (played[0]) { jToggleButton2.setText("Batak will be played"); } else { jToggleButton2.setText("Batak will NOT be played"); } }//GEN-LAST:event_jToggleButton2ActionPerformed private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton1ActionPerformed played[1] = !(played[1]); if (played[1]) { jToggleButton1.setText("MauMau will be played"); } else { jToggleButton1.setText("MauMau will NOT be played"); } }//GEN-LAST:event_jToggleButton1ActionPerformed private void jToggleButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton3ActionPerformed played[2] = !(played[2]); if (played[2]) { jToggleButton3.setText("King will be played"); } else { jToggleButton3.setText("King will NOT be played"); } }//GEN-LAST:event_jToggleButton3ActionPerformed private void jToggleButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton4ActionPerformed played[3] = !(played[3]); if (played[3]) { jToggleButton4.setText("BlackJack will be played"); } else { jToggleButton4.setText("BlackJack will NOT be played"); } }//GEN-LAST:event_jToggleButton4ActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(TrackLogMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(TrackLogMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(TrackLogMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(TrackLogMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new TrackLogMenu(playerID, playerName, parentMenu).setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JInternalFrame jInternalFrame1; private javax.swing.JInternalFrame jInternalFrame2; private javax.swing.JInternalFrame jInternalFrame3; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JLabel jLabel7; private javax.swing.JToggleButton jToggleButton1; private javax.swing.JToggleButton jToggleButton2; private javax.swing.JToggleButton jToggleButton3; private javax.swing.JToggleButton jToggleButton4; private javax.swing.JLabel numGames; private javax.swing.JSlider numOfGames; private javax.swing.JTextField p2id; private javax.swing.JPasswordField p2pwd; private javax.swing.JTextField p3id; private javax.swing.JPasswordField p3pwd; private javax.swing.JTextField p4id; private javax.swing.JPasswordField p4pwd; // End of variables declaration//GEN-END:variables }