/*
* Application: Hangman (Class: WriteToFile)
* Authour: Deleepa
* Date: 1st May 2013
*/
package gui;
import javax.swing.JOptionPane;
import service.ScoreService;
import utils.WriteToFile;
public class SaveScore extends javax.swing.JFrame {
int userScore;
String path = "scores.txt";
public SaveScore(int score) {
initComponents();
userScore = score;
scoreLabel.setText("Score: " + userScore);
}
/**
* 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() {
jPanel1 = new javax.swing.JPanel();
headerLabel = new javax.swing.JLabel();
saveScoreLabel = new javax.swing.JLabel();
scoreLabel = new javax.swing.JLabel();
nameLabel = new javax.swing.JLabel();
nameInput = new javax.swing.JTextField();
submitButton = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setPreferredSize(new java.awt.Dimension(600, 400));
headerLabel.setFont(new java.awt.Font("Cambria", 1, 36)); // NOI18N
headerLabel.setText("Hangman Game (alpha)");
saveScoreLabel.setFont(new java.awt.Font("Lucida Sans", 0, 24)); // NOI18N
saveScoreLabel.setText("Save Your Score");
scoreLabel.setFont(new java.awt.Font("Lucida Sans", 0, 24)); // NOI18N
scoreLabel.setText("Score:");
nameLabel.setFont(new java.awt.Font("Lucida Sans", 0, 24)); // NOI18N
nameLabel.setText("Name:");
nameInput.setFont(new java.awt.Font("Lucida Sans", 0, 24)); // NOI18N
nameInput.setMinimumSize(new java.awt.Dimension(10, 100));
nameInput.setPreferredSize(new java.awt.Dimension(200, 39));
submitButton.setFont(new java.awt.Font("Lucida Sans", 0, 24)); // NOI18N
submitButton.setText("Submit");
submitButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
submitButtonClicked(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(submitButton)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(headerLabel))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(59, 59, 59)
.addComponent(saveScoreLabel))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(213, 213, 213)
.addComponent(nameInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(106, 106, 106)
.addComponent(scoreLabel))))
.addContainerGap(187, Short.MAX_VALUE))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(107, 107, 107)
.addComponent(nameLabel)
.addContainerGap(308, Short.MAX_VALUE)))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(headerLabel)
.addGap(26, 26, 26)
.addComponent(saveScoreLabel)
.addGap(29, 29, 29)
.addComponent(nameInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(30, 30, 30)
.addComponent(scoreLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 66, Short.MAX_VALUE)
.addComponent(submitButton)
.addGap(61, 61, 61))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(146, 146, 146)
.addComponent(nameLabel)
.addContainerGap(225, Short.MAX_VALUE)))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, 0))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void submitButtonClicked(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_submitButtonClicked
try {
String writeString = userScore + "_" + nameInput.getText();
System.out.println(writeString);
ScoreService serviceCall = new ScoreService();
serviceCall.writeScore(writeString, false);
Highscores highscores = new Highscores();
highscores.setVisible(true);
this.dispose();
} catch(Exception e) {
String message = "The program encountered an internal error. Code: " + e.getMessage();
JOptionPane.showMessageDialog(this, message, "Error", JOptionPane.ERROR_MESSAGE);
}
}//GEN-LAST:event_submitButtonClicked
/**
* @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(SaveScore.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(SaveScore.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(SaveScore.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(SaveScore.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 SaveScore(90).setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel headerLabel;
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField nameInput;
private javax.swing.JLabel nameLabel;
private javax.swing.JLabel saveScoreLabel;
private javax.swing.JLabel scoreLabel;
private javax.swing.JButton submitButton;
// End of variables declaration//GEN-END:variables
}